poj 3740 Easy Finding(Dancing Links 精确覆盖)
来源:互联网 发布:88gbgb的新域名 编辑:程序博客网 时间:2024/05/16 15:58
Easy Finding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16128 Accepted: 4321
Description
Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.
Input
There are multiple cases ended by EOF. Test case up to 500.The first line of input isM, N (M ≤ 16, N ≤ 300). The next M lines every line containsN integers separated by space.
Output
For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.
Sample Input
3 30 1 00 0 11 0 04 40 0 0 11 0 0 01 1 0 10 1 0 0
Sample Output
Yes, I found itIt is impossible
题意:给出一个矩阵,问是否存在一些行使得每一列有且只有一个1。
思路:Dancing Links。详见http://www.cnblogs.com/grenet/p/3145800.html
AC代码:
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>#include <bitset>#include <queue>#define ll long longusing namespace std;const int maxn = 6005;const int INF = 1e9;int n, m, cnt, head;int L[maxn], R[maxn], U[maxn], D[maxn], S[maxn], C[maxn], H[maxn];inline void add_link(int i, int j){ C[++cnt] = j; S[j]++; D[cnt] = j; U[cnt] = U[j]; if(H[i]) R[cnt] = H[i], L[cnt] = L[H[i]]; else R[cnt] = L[cnt] = cnt; H[i] = cnt; U[D[cnt]] = cnt; D[U[cnt]] = cnt; R[L[cnt]] = cnt; L[R[cnt]] = cnt;}void remove(int c){ R[L[c]] = R[c]; L[R[c]] = L[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[C[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]) { D[U[j]] = j; U[D[j]] = j; S[C[j]]++; } L[R[c]] = R[L[c]] = c;}bool dance(){ if(R[head] == head) { puts("Yes, I found it"); return true; } int s = INF, c; for(int i = R[head]; i != head; i = R[i]) if(S[i] < s) s = S[c = i]; remove(c); for(int i = D[c]; i != c; i = D[i]) { for(int j = R[i]; j != i; j = R[j]) remove(C[j]); if(dance()) return true; for(int j = L[i]; j != i; j = L[j]) resume(C[j]); } resume(c); return false;}int main(){ int c; head = 0; while(~scanf("%d%d", &n, &m)) { cnt = m; for(int i =- 0; i <= m; i++) { C[i] = U[i] = D[i] = i; L[i + 1] = i; R[i] = i + 1; S[i] = 0; } L[0] = m, R[m] = 0; for(int i = 1; i <= n; i++) { H[i] = 0; for(int j = 1; j <= m; j++) { c = getchar(); while(!isdigit(c)) c = getchar(); if(c == '1') add_link(i, j); } } if (!dance()) puts("It is impossible"); } return 0;}
0 0
- poj 3740 Easy Finding(Dancing Links 精确覆盖)
- 【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题
- POJ 3740 - Easy Finding (Dancing links)
- poj 3740 Easy Finding(Dancing Links)
- poj 3740 Easy Finding//Dancing Links 或 状态压缩Dp
- POJ-3740-Easy Finding【DLX精确覆盖】
- poj - 3740 - Easy Finding(精确覆盖DLX)
- POJ 3740 Easy Finding(DLX精确覆盖裸题)
- POJ 3074 Sudoku(数独|Dancing Links精确覆盖)
- POJ3740 Easy Finding(Dancing Links)
- POJ3740--Easy Finding(Dancing Links)
- 【POJ】3740 Easy Finding 精确覆盖入门题
- POJ 3740 Easy Finding 位运算压缩+DFS or Dancing Links
- dancing links精确覆盖模版
- hdu 3663 Power Stations(精确覆盖 Dancing Links 模版)
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
- LeetCode----Sudoku Solver+精确覆盖问题解法(Dancing Links)
- HUST 1017 Exact cover(Dancing Links 精确覆盖)
- 在安卓和linux设备上添加event设备
- 思软软件协同oa办公软件标准版产品功能明细介绍
- git gitlab私服搭建
- Android 应用启动速度优化
- Qt控件父对象的确认
- poj 3740 Easy Finding(Dancing Links 精确覆盖)
- android 在Activity下获取getWindowManager()的屏幕尺寸和自定义View下的获取尺寸
- 蛇形数组
- 超漂亮的前台页面模板——html+css+js+框架
- 一种巧妙的删除程序自己的方法
- Cocos2d-X Box2D内容讲解
- Ubuntu下将Sublime Text设置为默认编辑器
- 使用随机生成的密码输入框,防止浏览器记住密码
- js获取前一天时间