SPOJ 1771 NQUEEN (DLX解n皇后问题)
来源:互联网 发布:sql删除重复字段数据 编辑:程序博客网 时间:2024/06/13 00:55
题目大意:
就是现在在一个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;}
0 0
- SPOJ 1771 NQUEEN (DLX解n皇后问题)
- spoj NQUEEN N皇后 DLX
- SPOJ 1771 NQUEEN Yet Another N-Queen Problem
- DLX SPOJ 1771 Yet Another N-Queen Problem (八皇后变形)
- DLX(n皇后)spoj1771
- nqueen问题
- N皇后问题公式解
- N皇后问题公式解!!!
- N*N皇后问题
- 八皇后 n皇后 问题
- 八皇后N皇后问题
- NQueen
- 用位运算解n皇后问题
- 回溯法解N皇后问题
- 遗传算法解N皇后问题
- 回溯法 解 n皇后问题
- 回溯法解n皇后问题
- 输出所有解的n皇后问题
- ASC32题解
- UVA_12108: Extraordinarily Tired Students
- Gym 100685 A Ariel 位统计
- 证券基础--信用交易。。。未完待继续
- java线程池学习(六) —— 线程池的合理配置
- SPOJ 1771 NQUEEN (DLX解n皇后问题)
- 51nod 1090 3个数和为0
- 关于java中流的使用--文件以及socket
- 南阳oj_(77)开灯问题
- LintCode-两个链表的和
- pat1025PAT Ranking (25)
- 程序员写好简历的五大技巧
- Mac OS X 10.9 启用 Web 服务器(默认预装 Apache+PHP 5.4.17)
- 单播、多播和广播