【POJ 3740】 Easy Finding
【题目链接】
http://poj.org/problem?id=3740
【算法】
Dancing Links算法解精确覆盖问题
详见这篇文章 : https://www.cnblogs.com/grenet/p/3145800.html
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 10010 int n,m,i,j,val; struct DancingLinks
{
int n,m,size;
int U[MAXN],D[MAXN],L[MAXN],R[MAXN],Row[MAXN],Col[MAXN];
int H[MAXN],S[MAXN];
inline void init(int _n,int _m)
{
n = _n;
m = _m;
for (i = ; i <= m; i++)
{
S[i] = ;
U[i] = D[i] = i;
L[i] = i - ;
R[i] = i + ;
}
R[m] = ; L[] = m;
size = m;
for (i = ; i <= n; i++) H[i] = -;
}
inline void link(int r,int c)
{
size++;
Row[size] = r;
Col[size] = c;
S[c]++;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if (H[r] < ) L[size] = R[size] = H[r] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
inline void remove(int c)
{
int i,j;
L[R[c]] = L[c];
R[L[c]] = R[c];
for (i = D[c]; i != c; i = D[i])
{
for (j = R[i]; j != i; j = R[j])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
S[Col[j]]--;
}
}
}
inline void resume(int c)
{
int i,j;
for (i = D[c]; i != c; i = D[i])
{
for (j = R[i]; j != i; j = R[j])
{
D[U[j]] = j;
U[D[j]] = j;
S[Col[j]]++;
}
}
L[R[c]] = c;
R[L[c]] = c;
}
inline bool solve()
{
int i,c;
if (R[] == ) return true;
c = R[];
for (i = R[]; i; i = R[i])
{
if (S[i] < S[c])
c = i;
}
remove(c);
for (i = D[c]; i != c; i = D[i])
{
for (j = R[i]; j != i; j = R[j])
remove(Col[j]);
if (solve()) return true;
for (j = R[i]; j != i; j = R[j])
resume(Col[j]);
}
resume(c);
return false;
}
} DLX; int main()
{ while (scanf("%d%d",&n,&m) != EOF)
{
DLX.init(n,m);
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
scanf("%d",&val);
if (val == ) DLX.link(i,j);
}
}
if (DLX.solve()) printf("Yes, I found it\n");
else printf("It is impossible\n");
} return ; }
【POJ 3740】 Easy Finding的更多相关文章
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
随机推荐
- HDU_2212_水
DFS Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HTTP常见状态码(404、400、500)等错误
一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 详细分解: 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 ...
- 【Centos7】Tomcat安装及一个服务器配置多个Tomcat
完成解压 参考 http://www.cnblogs.com/h--d/p/5074800.html https://www.cnblogs.com/tudou-22/p/9330875.html 步 ...
- 最适合初学者的Linux运维学习教程2018版
Linux运维工程师是一个新颖岗位,现在非常吃香,目前从行业的角度分析,随着国内软件行业不断发展壮大,越来越多复杂系统应运而生,为了保证系统稳定运行,必须要有足够多的Linux运维工程师.维护是软件生 ...
- 新建springcloud 找不到请求路径
新建 启动类 controller 都不可以直接放在 java 目录下 否则启动失败
- 无需编写代码,用接口管理工具 eoLinker 高效完成API测试流程相关业务
引言 作为开发人员,有时需要协助市场部门进行开发,比如在市场推广中,经常通过给用户赠送优惠券来提高复购率.这篇文章,将介绍如何使用接口管理工具 eoLinker 的自动化测试 [UI模式] 构建此业务 ...
- Neo4j图数据库从入门到精通
目录 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章:安装 1.环境 2.下载 3.开启远程访问 4.启动 第三章:CQL 1.CQL简介 2.Neo4j CQL命令/条款 ...
- idea和eclipse互相导入方法
1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...
- web前端学习总结--CSS
CSS 什么是CSS? CSS 指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在样式表中 如何使用CSS 内联方式 样式定义在单个的HTML元 ...
- python基础 条件和循环
Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...