painting

#include<iostream> #include<time.h> using namespace std; int a[30][30], dinh[30] = {-1}; int T, n; int cnt = 0; bool kt(int x) { for(int j = 0 ; j < n; j++) if(a[x][j] == 1 && dinh[j] == dinh[x]) return false; return true; } void paint(int k) { if(k == n) { cnt ++; return; } for(int i = 0; i< 4 ; i++) { dinh[k] = i; if(kt(k)) { paint(k+1); } dinh[k] = -1; } } int main() { clock_t begin = clock(); freopen("input.txt","r",stdin); cin >> T; for(int tc = 1; tc <= T; tc++) { cin >> n; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> a[i][j]; for(int i = 0; i < n; i++) dinh[i] = -1; cnt = 0; paint(0); cout <<"Case #"<< tc << endl << cnt << endl; clock_t end = clock(); //ghi lại thời gian lúc sau cout<<"Time run: "<<(float)(end-begin)/CLOCKS_PER_SEC<<" s"<<endl; } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.