UVa 167 - The Sultan's Successors

好几天没AC了,晚上AC一下找找感觉,本题是八皇后问题的变种。

/* File: 167.cpp Author: ACboy Date: 2010-3-25 Result: 1A Description: UVa 167 - The Sultan's Successors */ #include <iostream> using namespace std; struct Ans{ int position[8][2]; }; int data[10][10]; int viscolumn[10]; int visT[2][20]; Ans ans[200]; int temp[10][2]; int c = 0; void dfs(int pos) { if (pos == 8) { for (int j = 0; j < 8; j++) { ans[c].position[j][0] = temp[j][0]; ans[c].position[j][1] = temp[j][1]; } c++; } else for (int i = 0; i < 8; i++) { if (!viscolumn[i] && !visT[0][pos - i + 8] && !visT[1][pos + i]) { viscolumn[i] = 1; visT[0][pos - i + 8] = visT[1][pos + i] = 1; temp[pos][0] = pos; temp[pos][1] = i; dfs(pos + 1); viscolumn[i] = 0; visT[0][pos - i + 8] = visT[1][pos + i] = 0; } } } int main() { memset(visT, 0, sizeof(visT)); memset(viscolumn, 0, sizeof(viscolumn)); dfs(0); #ifndef ONLINE_JUDGE freopen("167.txt", "r", stdin); #endif int n; cin >> n; while (n--) { int i,j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { cin >> data[i][j]; } } int res = -1; for (j = 0; j < c; j++) { int sum = 0; for (i = 0; i < 8; i++) { sum += data[ans[j].position[i][0]][ans[j].position[i][1]]; } if (sum > res) res = sum; } printf("%5d/n", res); } return 0; }

你可能感兴趣的:(c,File,2010)