显然这是一道dfs简单题

或许匹配也能做

然而用了dancing links

显然这也是一道模板题

好的吧

调了一上午 终于弄好了模板

Easy Finding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19052   Accepted: 5273

Description

Given a M×N matrix AAij ∈ {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 is MN (M ≤ 16, N ≤ 300). The next M lines every line contains N 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 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0

Sample Output

Yes, I found it
It is impossible

Source

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff; int S[maxn], head[maxn], vis[maxn];
int U[maxn], D[maxn], L[maxn], R[maxn];
int C[maxn], X[maxn];
int n, m, ans, ret; void init()
{
for(int i = ; i <= m; i++)
D[i] = i, U[i] = i, R[i] = i + , L[i] = i - ;
L[] = m, R[m] = ;
mem(S, ), mem(head, -);
ans = m + ;
} void delc(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[C[j]]--; } void resc(int c)
{
for(int i = U[c]; i != c; i = U[i])
for(int j = L[i]; j != i; j = L[j])
U[D[j]] = j, D[U[j]] = j, S[C[j]]++;
L[R[c]] = c, R[L[c]] = c;
} void add(int r, int c)
{
ans++, S[c]++, C[ans] = c, X[ans] = r;
D[ans] = D[c];
U[ans] = c;
U[D[c]] = ans;
D[c] = ans;
if(head[r] < ) head[r] = L[ans] = R[ans] = ans;
else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
} bool dfs(int sh)
{
if(!R[])
{
ret = sh;
return true;
}
int c = R[];
delc(c);
for(int i = D[c]; i != c; i = D[i])
{
vis[sh] = i;
for(int j = R[i]; j != i; j = R[j])
delc(C[j]);
if(dfs(sh + )) return true;
for(int j = L[i]; j != i; j = L[j])
resc(C[j]);
}
resc(c);
return false;
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
init();
int tmp;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
{
rd(tmp);
if(tmp) add(i, j);
}
if(dfs())
printf("Yes, I found it\n");
else
printf("It is impossible\n"); } return ;
}

Easy Finding POJ - 3740 (DLX)的更多相关文章

  1. Sudoku POJ - 2676(DLX)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25356   Accepted: 11849   Specia ...

  2. 舞蹈链(DLX)

    舞蹈链(DLX) Tags:搜索 作业部落 评论地址 一.概述 特别特别感谢这位童鞋His blog 舞蹈链是一种优美的搜索,就像下面这样跳舞- 舞蹈链用于解决精确覆盖或者重复覆盖的问题 你可以想象成 ...

  3. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  4. POJ_3740 Easy Finding ——精确覆盖问题,DLX模版

    Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18790   Accepted: 5184 Des ...

  5. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  6. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  7. NOIP 2009 靶形数独(DLX)

    小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士拿出了他最近发明的“靶形数独”,作 ...

  8. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  9. 【转】 Easy RadControl 之 RadGridView(Silverlight)

    1.不显示第1列即列指示器(Row Indicators) 在 telerik:RadGridView中设置属性   RowIndicatorVisibility="Collapsed&qu ...

随机推荐

  1. Flask发送邮件

    参考:官方文档:https://pythonhosted.org/Flask-Mail/ 1.安装插件  Flask-Mail (pip install Flask-Mail) 2.配置 Flask- ...

  2. BOM基础 计时器 定时器 DOM 基础

    -------------------------------------------滴水穿石,我心永恒. day48 CSSJS 1 ECMA script 2 BOM browser object ...

  3. 01 Django REST Framework 介绍

    01-Django REST Framework的介绍 Django REST框架是一个用于构建Web API的强大而灵活的工具包. 您可能希望使用REST框架的一些原因: 1. Web可浏览API对 ...

  4. Python Revisited Day 02 (数据类型)

    目录 Python 关键字 整数 整数转换函数 整数位逻辑操作符 浮点类型 math模块函数与常量 复数 精确的十进制数字 decimal 字符串 str.format() 格式规约 Python 关 ...

  5. B. Switches and Lamps

    链接 [https://codeforces.com/contest/985/problem/B] 题意 给你n,m,分别是n个开关,m个灯 给一个n*m的字符矩阵aij=1,表示i可以控制j这个灯 ...

  6. Python学习第十二篇——切片的使用

    Python中使用函数切片可以创建副本,保留原本.现在给出如下代码 magicians_list = ['mole','jack','lucy'] new_lists = [] def make_gr ...

  7. CentOS 7.2 yum安装LAMP环境

    https://www.linuxidc.com/Linux/2016-11/136766.htm 详见以上链接,用yum安装方便省事. 尤其注意,mysql数据要设置远程连接.

  8. 【Python3练习题 017】 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比。请编程序找出三队赛手的名单。

    import itertools   for i in itertools.permutations('xyz'):     if i[0] != 'x' and i[2] != 'x' and i[ ...

  9. 【知乎】WinForm 与 WPF的区别

    你想上班 那么针对公司需求学如果只是自己写着玩 那么区分一下1.你的程序运行在 自己机器a.一个工具而已 要的是cooooooool 那么WPFb.一个工具而已 要的是useful easy 那么wi ...

  10. [转帖]Gartner预测:2019年全球公有云规模达2143亿美元

    Gartner预测:2019年全球公有云规模达2143亿美元 https://news.cnblogs.com/n/623341/ 近日,全球领先的信息技术研究和顾问公司 Gartner 发布的最新数 ...