SPOJNQUEEN-Yet Another N-Queen Problem
来源:互联网 发布:淘宝找人代付安全吗 编辑:程序博客网 时间:2024/05/20 14:15
NQUEEN - Yet Another N-Queen Problem
After solving Solution to the n Queens Puzzle by constructing, LoadingTime wants to solve a harder version of the N-Queen Problem. Some queens have been set on particular locations on the board in this problem. Can you help him??
Input
The input contains multiple test cases. Every line begins with an integer N (N<=50), then N integers followed, representing the column number of the queen in each rows. If the number is 0, it means no queen has been set on this row. You can assume there is at least one solution.
Output
For each test case, print a line consists of N numbers separated by spaces, representing the column number of the queen in each row. If there are more than one answer, print any one of them.
Example
Input:4 0 0 0 08 2 0 0 0 4 0 0 0Output:2 4 1 32 6 1 7 4 8 3 5
题意:n皇后问题,告诉你部分行皇后在哪,求出任意一种合法的摆放方法
解题思路:舞蹈链精确覆盖,不过它只需要保证行和列都被覆盖到了就好,斜线不需要所有的斜线都被覆盖到
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cctype> #include <map> #include <cmath> #include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional> using namespace std;#define LL long long const int INF = 0x3f3f3f3f;const int maxn = 500005;int n, m, x, y, tot;int p[10900][3], vis[maxn], a[100];struct DLX{int L[maxn], R[maxn], U[maxn], D[maxn];int row[maxn], col[maxn], sum[maxn], ans[maxn];int n, m, num, cnt;void add(int k, int l, int r, int u, int d, int x, int y){L[k] = l; R[k] = r; U[k] = u;D[k] = d; row[k] = x; col[k] = y;}void reset(int n, int m){num = 0x7FFFFFF;this->n = n; this->m = m;for (int i = 0; i <= m; i++){add(i, i - 1, i + 1, i, i, 0, i);sum[i] = 0;}L[0] = m, R[m] = 0, cnt = m + 1;}void insert(int x, int y){int temp = cnt - 1;if (row[temp] != x){add(cnt, cnt, cnt, U[y], y, x, y);U[D[cnt]] = cnt; D[U[cnt]] = cnt;}else{add(cnt, temp, R[temp], U[y], y, x, y);R[L[cnt]] = cnt; L[R[cnt]] = cnt;U[D[cnt]] = cnt; D[U[cnt]] = cnt;}sum[y]++, cnt++;}void remove(int k){R[L[k]] = R[k], L[R[k]] = L[k];for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = D[j];U[D[j]] = U[j];sum[col[j]]--;}}void resume(int k){R[L[k]] = k, L[R[k]] = k;for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = j;U[D[j]] = j;sum[col[j]]++;}}bool dfs(int k, int p){if (k == p) { num = min(k, num); return true; }if (!R[0]) return false;int now = R[0];for (int i = now; i <= p; i = R[i])if (sum[now] > sum[i]) now = i;remove(now);for (int i = D[now]; i != now; i = D[i]){ans[k] = row[i];for (int j = R[i]; j != i; j = R[j]) remove(col[j]);if (dfs(k + 1, p)) return true;for (int j = L[i]; j != i; j = L[j]) resume(col[j]);}resume(now);return false;}void display(int k){for (int i = 0; i < num; i++) a[p[ans[i]][0]] = p[ans[i]][1];for (int i = 1; i <= k; i++) printf("%d%s", a[i], i == k ? "\n" : " ");}}dlx;void insert(int x, int y, int z){int a = x, b = n + y, c = n + n + x + y - 1, d = 4 * n - 1 + (n - x) + y;if (vis[a] || vis[b] || vis[c] || vis[d]) return;tot++;p[tot][0] = x, p[tot][1] = y;dlx.insert(tot, a);dlx.insert(tot, b);dlx.insert(tot, c);dlx.insert(tot, d);if (z) vis[a] = vis[b] = vis[c] = vis[d] = 1;}int main(){while (~scanf("%d", &n)){tot = 0;dlx.reset(n * n, n * 6 - 2);memset(vis, 0, sizeof vis);for (int i = 1; i <= n; i++){scanf("%d", &x);if (x) insert(i, x, 1);}for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) insert(i, j, 0);if (dlx.dfs(0, n)) dlx.display(n);}return 0;}
- SPOJNQUEEN-Yet Another N-Queen Problem
- spoj 1771 Yet Another N-Queen Problem
- SPOJ 1771 NQUEEN Yet Another N-Queen Problem
- DLX SPOJ 1771 Yet Another N-Queen Problem (八皇后变形)
- SPOJ 1771 Yet Another N-Queen Problem 解题报告(Dancing Link)
- UVa 11195 - Another n-Queen Problem
- UVA - 11195 Another n-Queen Problem 位运算符+dfs
- Yet Another Multiple Problem
- N-Queen Problem
- hdu4474-Yet Another Multiple Problem
- hdu4474 Yet Another Multiple Problem
- HDU4474 Yet Another Multiple Problem
- Hackerrank:Yet Another KMP Problem
- Yet Another Multiple Problem-----BFS
- [codeforces903G]Yet Another Maxflow Problem
- AI-N Queen Problem(dfs)
- AI-N Queen Problem(bfs)
- Yet Another Multiple Problem (hdu 4474)
- 【SSM框架整合】配置文件的配置
- Android --- 布局结构属性(二)
- hive的安装和操作笔记-day1
- 聊聊网络中的三层路由
- 深入理解+图解 java转发与重定向
- SPOJNQUEEN-Yet Another N-Queen Problem
- ubuntu 下查看caj文件
- 使用pthread-win32工程编译静态库
- VBA 弹框选择文件路径
- 循环语句
- POJ2478 Farey Sequence
- OpenCV学习笔记(32)图像载入、显示、保存、变换灰度图 总结
- 新华三面试总结(二)
- 2017年10月30日提高组T1 数论