// // 11th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/5/24. // //
// // 13th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/5/13. // //
// // 13th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/5/13. // //
/* 设计思路: 这里在常规回溯的基础上进行了一定的剪枝。也就是在试探的时候先判断形状能不能满足,如果 形状不能满足的话就直接continue,无需再遍历颜色,减小时间复杂度 */ structstone { int shape; int color; bool exist; };
structmat { int x; int y; }; mat mp[30][30]; stone a[30][30]; int n,ans;
boolcheck_shape(int x, int y) { for (int i=1;i<=x-1;i++) if (a[mp[i][y].x][mp[i][y].y].shape == a[mp[x][y].x][1].shape) returnfalse; for (int i=1;i<=y-1;i++) if (a[mp[x][i].x][mp[x][i].y].shape == a[mp[x][y].x][1].shape) returnfalse; returntrue; }
boolcheck_color(int x, int y) { for (int i=1;i<=x-1;i++) if (a[mp[i][y].x][mp[i][y].y].color == a[mp[x][y].x][mp[x][y].y].color) returnfalse; for (int i=1;i<=y-1;i++) if (a[mp[x][i].x][mp[x][i].y].color == a[mp[x][y].x][mp[x][y].y].color) returnfalse; returntrue; }
voiddfs(int x,int y) { for (int i=1;i<=n;i++) { mp[x][y].x = i; if (!check_shape(x, y)) continue; for (int j=1;j<=n;j++) { mp[x][y].y = j; if (!a[i][j].exist) continue; if (!check_color(x, y)) continue; cout << x << y << " " <<i <<j <<endl; if (y<n) { a[i][j].exist = false; dfs(x, y+1); a[i][j].exist = true; } elseif (x==n && y==n) ans++; else { a[i][j].exist = false; dfs(x+1,1); a[i][j].exist = true; } } } }
intmain() { cin >> n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { a[i][j].shape = i; a[i][j].color = j; a[i][j].exist = true; } ans = 0; dfs(1, 1); cout << ans << endl; }
// // 14th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/5/27. // //