Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> neigh[10];
- int _x[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
- int _y[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
- long long soma, bagagem;
- int limit, cnt, atual, especial;
- vector<string> seq;
- bitset<10> vis;
- bool check(int x, int y){
- return x >= 0 && x < 3 && y >= 1 && y <= 3;
- }
- void build(){
- neigh[0].push_back(7);
- neigh[0].push_back(8);
- neigh[0].push_back(9);
- neigh[7].push_back(0);
- neigh[8].push_back(0);
- neigh[9].push_back(0);
- for(int i = 0; i < 3; i++){
- for(int j = 1; j <= 3; j++){
- //cout << "Dad = " << i << " " << j << endl;
- for(int k = 0; k < 8; k++){
- int x = i + _x[k];
- int y = j + _y[k];
- //cout << "Son = " << x << " " << y << endl;
- if(check(x, y)) {
- neigh[3*i + j].push_back(3*x + y);
- }
- }
- }
- }
- }
- string to_string(char valor){
- string aux = "";
- aux += valor;
- return aux;
- }
- void digit(int no, int size, long long val, string form);
- void contem(int val, string form){
- for(int i = 0; i < seq.size(); i++){
- if(seq[i] == form) {
- seq.erase(seq.begin()+i);
- bagagem -= val;
- break;
- }
- }
- }
- void forNeigh(int no, long long valor, string form){
- if(form.size() == limit){
- vis[no] = true;
- for(int i = 0; i < neigh[no].size(); i++){
- int viz = neigh[no][i];
- if(!vis[viz]) {
- vis[viz] = true;
- digit(viz, 1, viz, to_string('0'+viz));
- vis[viz] = false;
- }
- }
- vis[no] = false;
- contem(valor, form);
- }
- }
- int freq[10];
- bool verify(){
- memset(freq,0, sizeof freq);
- //for(int i = 0; i < seq.size(); i++) cout << seq[i] << " \n"[i == seq.size()-1];
- for(int i = 0; i < seq.size(); i++){
- if(seq[i].size() < limit && i != seq.size()-1) {
- //cout << i << endl;
- return false;
- }
- for(int j = 0; j < seq[i].size(); j++){
- freq[seq[i][j]-'0']++;
- if(freq[seq[i][j]-'0'] > 1) {
- //cout << i << " " << j << endl;
- return false;
- }
- }
- }
- return true;
- }
- void adiciona(long long valor, int cont, string form){
- if(valor + bagagem < soma){
- bagagem += valor;
- seq.push_back(form);
- forNeigh(cont, valor, form);
- }else if(valor + bagagem == soma){
- bagagem += valor;
- seq.push_back(form);
- for(int i = 0; i < seq.size(); i++) {
- cout << seq[i] << " \n"[i == seq.size()-1];
- }
- cnt++;
- if(form[form.size()-1] == '7' ||
- form[form.size()-1] == '8' ||
- form[form.size()-1] == '9') if(!vis[0]) digit(0, 1, 0, "0")
- contem(valor, form);
- forNeigh(cont, valor, form);
- }
- }
- void digit(int no, int size, long long val, string form){
- //cout << "--> " << val << endl;
- if(size == limit) {
- //cout << "limit\n";
- vis[no] = true;
- adiciona(val, no, form);
- vis[no] = false;
- }else if(val == soma-bagagem) {
- //cout << "equal\n";
- vis[no] = true;
- adiciona(val, no, form);
- vis[no] = false;
- }else if(val+bagagem < soma){
- vis[no] = true;
- for(int i = 0; i < neigh[no].size(); i++){
- int viz = neigh[no][i];
- if(!vis[viz]) {
- vis[viz] = true;
- digit(viz, size + 1, val*10 + viz, form + to_string('0'+viz));
- vis[viz] = false;
- }
- }
- vis[no] = false;
- contem(val, form);
- }
- }
- void print(){
- for(int i = 0; i < 10; i++){
- cout << "#" << i << " = ";
- for(int j = 0; j < neigh[i].size(); j++) cout << neigh[i][j] << " \n"[j == neigh[i].size()-1];
- }
- }
- int main(){
- build();
- //print();
- int cases = 1;
- while(true){
- scanf("%lld %d", &soma, &limit);
- if(soma == -1 && limit == -1) break;
- printf("#%d\n", cases++);
- cnt = 0;
- vis.reset();
- for(atual = 0; atual <= 9;atual++){
- bagagem = 0;
- seq.clear();
- digit(atual, 1, atual, to_string('0'+atual));
- }
- if(cnt == 0) printf("impossivel\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement