Advertisement
Manioc

tecla&some

Sep 6th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> neigh[10];
  6.  
  7. int _x[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
  8. int _y[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  9.  
  10. long long soma, bagagem;
  11. int limit, cnt, atual, especial;
  12. vector<string> seq;
  13. bitset<10> vis;
  14.  
  15. bool check(int x, int  y){
  16.     return x >= 0 && x < 3 && y >= 1 && y <= 3;
  17. }
  18.  
  19. void build(){
  20.     neigh[0].push_back(7);
  21.     neigh[0].push_back(8);
  22.     neigh[0].push_back(9);
  23.     neigh[7].push_back(0);
  24.     neigh[8].push_back(0);
  25.     neigh[9].push_back(0);
  26.     for(int i = 0; i < 3; i++){
  27.         for(int j = 1; j <= 3; j++){
  28.             //cout << "Dad = " <<  i << " " << j << endl;
  29.             for(int k = 0; k < 8; k++){
  30.                 int x = i + _x[k];
  31.                 int y = j + _y[k];
  32.                 //cout << "Son = " << x << " " << y << endl;
  33.                 if(check(x, y)) {
  34.                     neigh[3*i + j].push_back(3*x + y);
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. string to_string(char valor){
  42.     string aux = "";
  43.     aux += valor;
  44.     return aux;
  45. }
  46. void digit(int no, int size, long long val, string form);
  47.  
  48. void contem(int val, string form){
  49.     for(int i = 0; i < seq.size(); i++){
  50.         if(seq[i] == form) {
  51.             seq.erase(seq.begin()+i);
  52.             bagagem -= val;
  53.             break;
  54.         }
  55.     }
  56. }
  57. void forNeigh(int no, long long valor, string form){
  58.     if(form.size() == limit){
  59.         vis[no] = true;
  60.         for(int i = 0; i < neigh[no].size(); i++){
  61.             int viz = neigh[no][i];
  62.             if(!vis[viz]) {
  63.                 vis[viz] = true;
  64.                 digit(viz, 1, viz, to_string('0'+viz));
  65.                 vis[viz] = false;
  66.             }
  67.         }
  68.         vis[no] = false;
  69.         contem(valor, form);
  70.     }
  71. }
  72.  
  73. int freq[10];
  74. bool verify(){
  75.     memset(freq,0, sizeof freq);
  76.  
  77.     //for(int i = 0; i < seq.size(); i++) cout << seq[i] << " \n"[i == seq.size()-1];
  78.     for(int i = 0; i < seq.size(); i++){
  79.         if(seq[i].size() < limit && i != seq.size()-1) {
  80.             //cout << i << endl;
  81.             return false;
  82.         }
  83.         for(int j = 0; j < seq[i].size(); j++){
  84.             freq[seq[i][j]-'0']++;
  85.             if(freq[seq[i][j]-'0'] > 1) {
  86.                 //cout << i << " " << j << endl;
  87.                 return false;
  88.             }
  89.         }
  90.     }
  91.  
  92.     return true;
  93. }
  94. void adiciona(long long valor, int cont, string form){
  95.     if(valor + bagagem < soma){
  96.         bagagem += valor;
  97.         seq.push_back(form);
  98.         forNeigh(cont, valor, form);
  99.     }else if(valor + bagagem == soma){
  100.         bagagem += valor;
  101.         seq.push_back(form);
  102.         for(int i = 0; i < seq.size(); i++) {
  103.             cout << seq[i] << " \n"[i == seq.size()-1];
  104.         }
  105.         cnt++;
  106.         if(form[form.size()-1] == '7' ||
  107.            form[form.size()-1] == '8' ||
  108.            form[form.size()-1] == '9') if(!vis[0]) digit(0, 1, 0, "0")
  109.         contem(valor, form);
  110.         forNeigh(cont, valor, form);
  111.     }
  112. }
  113. void digit(int no, int size, long long val, string form){
  114.     //cout << "--> " << val << endl;
  115.     if(size == limit) {
  116.         //cout << "limit\n";
  117.         vis[no] = true;
  118.         adiciona(val, no, form);
  119.         vis[no] = false;
  120.     }else if(val == soma-bagagem) {
  121.         //cout << "equal\n";
  122.         vis[no] = true;
  123.         adiciona(val, no, form);
  124.         vis[no] = false;
  125.     }else if(val+bagagem < soma){
  126.         vis[no] = true;
  127.         for(int i = 0; i < neigh[no].size(); i++){
  128.             int viz = neigh[no][i];
  129.             if(!vis[viz]) {
  130.                 vis[viz] = true;
  131.                 digit(viz, size + 1, val*10 + viz, form + to_string('0'+viz));
  132.                 vis[viz] = false;
  133.             }
  134.         }
  135.         vis[no] = false;
  136.         contem(val, form);
  137.     }
  138. }
  139.  
  140. void print(){
  141.     for(int i = 0; i < 10; i++){
  142.         cout << "#" << i << " = ";
  143.         for(int j = 0; j < neigh[i].size(); j++) cout << neigh[i][j] << " \n"[j == neigh[i].size()-1];
  144.     }
  145. }
  146. int main(){
  147.     build();
  148.     //print();
  149.     int cases = 1;
  150.     while(true){
  151.         scanf("%lld %d", &soma, &limit);
  152.         if(soma == -1 && limit == -1) break;
  153.         printf("#%d\n", cases++);
  154.         cnt = 0;
  155.         vis.reset();
  156.         for(atual = 0; atual <= 9;atual++){
  157.             bagagem = 0;
  158.             seq.clear();
  159.             digit(atual, 1, atual, to_string('0'+atual));
  160.         }
  161.    
  162.         if(cnt == 0) printf("impossivel\n");
  163.     }
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement