HDOJ 2828 Lamp DLX反复覆盖
DLX反复覆盖模版题:
每一个开关两个状态。但仅仅能选一个,建2m×n的矩阵跑DLX模版。。
。。
Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 855 Accepted Submission(s): 265
Special Judge
so it’s possible that some lamp is lighted when its corresponding switch is “OFF”!
To make things easier, we number all the lamps from 1 to N, and all the switches 1 to M. For each lamps, we give a list of switches controlling it. For example, for Lamp 1, the list is “1 ON 3 OFF 9 ON”, that means Lamp 1 will be lighted if the Switch 1 is
at the “ON” state OR the Switch 3 is “OFF” OR the Switch 9 is “ON”.
Now you are requested to turn on or off the switches to make all the lamps lighted.
pairs of “x ON” or “x OFF” follow.
2 2
2 1 ON 2 ON
1 1 OFF
2 1
1 1 ON
1 1 OFF
OFF ON
-1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=1010,maxm=510;
const int maxnode=maxn*maxm;
const int INF=0x3f3f3f3f; int n,m;
bool flag; 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];
int LAMP[maxn];
bool vis[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++)
{
vis[i]=false;
LAMP[i]=0;
H[i]=-1;
}
flag=false;
}
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)
{
for(int i=D[c];i!=c;i=D[i])
L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
L[R[i]]=R[L[i]]=i;
}
void Dance(int d)
{
if(flag) return ;
if(R[0]==0)
{
///find ans
for(int i=0;i<d;i++)
{
int lamp=(ans[i]+1)/2;
if(ans[i]%2) LAMP[lamp]=1;
}
for(int i=1;i<=n/2;i++)
{
if(LAMP[i]==1) printf("ON");
else printf("OFF");
if(i!=n/2) putchar(32); else putchar(10);
}
flag=true;
return ;
}
int c=R[0];
for(int i=R[0];i!=0;i=R[i])
{
if(S[i]<S[c]) c=i;
}
for(int i=D[c];i!=c;i=D[i])
{
if(vis[Row[i]]) continue;
int r1=Row[i],r2=Row[i];
if(r1%2==0) r2--;else r2++;
vis[r1]=true; vis[r2]=true;
remove(i);
for(int j=R[i];j!=i;j=R[j]) remove(j);
ans[d]=Row[i];
Dance(d+1);
for(int j=L[i];j!=i;j=L[j]) resume(j);
resume(i);
vis[r1]=false; vis[r2]=false;
}
}
}; DLX dlx; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
dlx.init(2*m,n);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int p; char sw[20];
scanf("%d%s",&p,sw);
if(sw[1]=='N') dlx.Link(2*p-1,i);
else if(sw[1]=='F') dlx.Link(2*p,i);
}
}
dlx.Dance(0);
if(flag==false) puts("-1");
}
return 0;
}
HDOJ 2828 Lamp DLX反复覆盖的更多相关文章
- [DLX反复覆盖] hdu 2828 Lamp
题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...
- FZU 1686 神龙的难题(DLX反复覆盖)
FZU 1686 神龙的难题 pid=1686" target="_blank" style="">题目链接 题意:中文题 思路:每个1看成列, ...
- FZU 1686 神龙的难题 DLX反复覆盖
DLX反复覆盖: 须要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462 Submit: 1401 Time Limit: 1000 mSec Memory L ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- HDU 4735 Little Wish~ lyrical step~(DLX , 反复覆盖)
解题思路: DLX 的模板题.反复覆盖. #include <stdio.h> #include <string.h> #include <iostream> #i ...
- [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)
Problem 1686 神龙的难题 Accept: 444 Submit: 1365 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
- [DLX反复覆盖] hdu 3656 Fire station
题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...
- [ACM] HDU 2295 Radar (二分法+DLX 重复覆盖)
Radar Problem Description N cities of the Java Kingdom need to be covered by radars for being in a s ...
随机推荐
- python 面向对象 类的内置方法
判断是不是类cls的对象 class A: pass a = A() print(isinstance(a,A)) 判断类sub是不是super的子类 class A: pass class B(A) ...
- 小学生绞尽脑汁也学不会的python(面对对象-----成员)
小学生绞尽脑汁也学不会的python(面对对象-----成员) 成员 class Person: def __init__(self, name, num, gender, birthday): # ...
- Unity的Json解析<二>–写Json文件
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50378805 作者:car ...
- Shiro:整合swagger2时需要放行的资源
filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/swagger-resource ...
- Redis-server在windows下闪退
在win7下使用Redis(windows版)很简单,只需要去Git上下载一个压缩包,解压运行即可.但是前段时间发现win10下双击redis-server既然闪退.非常不解... 在观察了错误日志才 ...
- 【codeforces 794A】Bank Robbery
[题目链接]:http://codeforces.com/contest/794/problem/A [题意] 每个位置上可能有物品(>=1)或是没物品 你一开始在某一个位置b; 然后你最左可以 ...
- 第十一章 Servlet MVC模式
内包含案例,基于jsp+servlet的:MVC模式计算器:MVC模式登陆 第十一章 Servlet MVC模式 模型-视图-控制器(model-view-controller),简称MVC.MVC是 ...
- angular-HTTP
AngularJS $http 是一个用于读取web服务器上数据的服务. $http.get(url) 是用于读取服务器数据的函数. <div ng-app="myApp" ...
- Android 混淆后的代码调试
ProGuard的输出文件及用处 混淆之后,会给我们输出一些文件,在gradle方式下是在<project_dir>/build/proguard/目录下,ant是在<project ...
- Android Touch事件传递机制全面解析(从WMS到View树)
转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...