题目大意:
就是现在在一个n*n的棋盘上已经摆放了一些皇后, n <= 50
现在要求找到对应的一种方案使得棋盘上有n个皇后互不攻击
大致思路:
就是舞蹈链解决的一类问题....用列表示对应行, 列, 左斜线和右斜线分别对应的占用情况
在Dance的时候注意走到N步停止, 每次消除代表着行或者列的那些列的1, 而不是优先考虑对角线
因为对角线方向是不能刚好选出N个互补相交的, 那样搜会导致无解, 因为这本身并不要求所有对角都填满
注意以上几点就可以了...
代码如下:
Result : Accepted Memory : 13312 KB Time : 50 ms
/* * Author: Gatevin * Created Time: 2015/10/4 18:10:46 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxnode 360400 #define maxn 90100 #define maxm 2000 /* * 第1~n列是对应行被占领, n + 1 ~ 2n列是列被占领 * 第2n + 1 ~ 2n + (2*n - 1)是从右上到左下斜线占领 * 第4*n ~ 4n - 1 + (2*n - 1)是左上到右下斜线被占领 * 每一行代表的位置要填充4个1 */ struct DLX { int n, m, size; int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode]; int H[maxn], S[maxm]; int ansd, ans[maxn]; struct State { int x, y, val; State(int _x, int _y, int _val) { x = _x, y = _y, val = _val; } State(){} }; State s[maxn]; void init(int _n, int _m) { n = _n; m = _m; for(int i = 0; i <= m; i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i - 1; R[i] = i + 1; } R[m] = 0; L[0] = m; size = m; for(int i = 1; i <= n; i++) H[i] = -1; } void Link(int r, int c) { ++S[Col[++size] = c]; Row[size] = r; D[size] = D[c]; U[D[c]] = size; U[size] = c; D[c] = size; if(H[r] < 0) H[r] = L[size] = R[size] = size; else { R[size] = R[H[r]]; L[R[H[r]]] = size; L[size] = H[r]; R[H[r]] = size; } } void remove(int c) { L[R[c]] = L[c]; R[L[c]] = R[c]; for(int i = D[c]; i != c; i = D[i]) for(int j = R[i]; j != i; j = R[j]) { U[D[j]] = U[j]; D[U[j]] = D[j]; --S[Col[j]]; } } void resume(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) ++S[Col[U[D[j]] = D[U[j]] = j]]; L[R[c]] = R[L[c]] = c; } bool Dance(int dep, int num) { if(dep >= num)//只要找到num个位置即可 { ansd = dep; return true; } if(R[0] == 0) { return false; } int c = R[0]; for(int i = R[0]; i != 0; i = R[i]) { if(i <= num)//因为最终只会有行和列充满, 而不是对角线, 这里把i控制在行和列的范围内 { if(S[i] < S[c]) c = i; } else break; //如果不对这歌i进行控制的话, 会优先考虑左上往右下的对角线中选num个精确覆盖对角线, 这是不可能的 } remove(c); for(int i = D[c]; i != c; i = D[i]) { ans[dep] = Row[i]; for(int j = R[i]; j != i; j = R[j]) remove(Col[j]); if(Dance(dep + 1, num)) return true; for(int j = L[i]; j != i; j = L[j]) resume(Col[j]); } resume(c); return false; } int res[310]; bool vis[maxm]; pair<int, int> pos[maxn]; void solve(int num) { init(num*num, num*6 - 2); int x, cnt = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= num; i++) { scanf("%d", &x); if(x == 0) continue; int p1 = i, p2 = x + num, p3 = num + (i - x) + 2*num, p4 = i + x + 4*num - 2; vis[p1] = vis[p2] = vis[p3] = vis[p4] = 1; cnt++; Link(cnt, p1); Link(cnt, p2); Link(cnt, p3); Link(cnt, p4); pos[cnt] = make_pair(i, x); } for(int i = 1; i <= num; i++) for(int j = 1; j <= num; j++)//第i行第j列的位置 { if(!vis[i] && !vis[j + num] && !vis[num + (i - j) + 2*num] && !vis[i + j + 4*num - 2]) { cnt++; Link(cnt, i);//第i行 Link(cnt, j + num);//第j列 Link(cnt, num + (i - j) + 2*num); Link(cnt, num + i - (num + 1 - j) + 4*num - 1); pos[cnt] = make_pair(i, j); } } if(!Dance(0, num)) puts("No answer find"); else { for(int i = 0; i < ansd; i++) res[pos[ans[i]].first] = pos[ans[i]].second; for(int i = 1; i <= num; i++) printf("%d%c", res[i], i == num ? '\n' : ' '); } return; } }; DLX dlx; int main() { int n; while(~scanf("%d", &n)) dlx.solve(n); return 0; }