HDU2828--Lamp(Dancing Links)



Problem Description
There are several switches and lamps in the room, however, the connections between them are very complicated. One lamp may be controlled by several switches, and one switch may controls at most two lamps. And what’s more, some connections are reversed by mistake, 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. 
 

Input
There are several test cases in the input. The first line of each test case contains N and M (1 <= N,M <= 500), then N lines follow, each indicating one lamp. Each line begins with a number K, indicating the number of switches controlling this lamp, then K pairs of “x ON” or “x OFF” follow.
 

Output
Output one line for each test case, each contains M strings “ON” or “OFF”, indicating the corresponding state of the switches. For the solution may be not unique, any correct answer will be OK. If there are no solutions, output “-1” instead.
 

Sample Input
  
  
2 2 2 1 ON 2 ON 1 1 OFF 2 1 1 1 ON 1 1 OFF
 

Sample Output
  
  
OFF ON -1
思路:要满足灯全亮DLX重复覆盖就可以搞定。但是每个开关只可以有一种状态。所以多个vis数组标记。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MaxM = 2080;
const int MaxN = 2080;
const int maxnode = MaxN * MaxM;
const int INF = 0x3f3f3f3f;
#define eps 1e-8
int N,M;
bool vis[2080];
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;  
    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++)H[i] = -1;  
    }  
    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;  
    }  
    
    bool Dance(int d)  
    {  
        if(R[0] == 0)  
        {  
            return 1;  
        }  
        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((Row[i]&1) && (vis[Row[i]+1]))	continue;
			if(((Row[i]&1) == 0) && vis[Row[i]-1])	continue;
            remove(i);  
			vis[Row[i]] = 1;
            for(int j = R[i];j != i;j = R[j])remove(j);  
            if(Dance(d+1))	return 1;
            for(int j = L[i];j != i;j = L[j])resume(j);  
            resume(i);  
			vis[Row[i]] = 0;
        }
		return 0;
    }  
};  
DLX g;  

int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d%d",&N,&M)==2)
	{
		memset(vis,0,sizeof(vis));
		g.init(2*M,N);
		for(int i = 1;i <= N;i++)
		{
			int k;	scanf("%d",&k);
			for(int j = 1;j <= k;j++)
			{
				int x;	scanf("%d",&x);
				char ope[4];	scanf("%s",ope);
				if(ope[1] == 'N')
					g.Link(2*x-1,i);
				else g.Link(2*x,i);
			}
		}
		if(!g.Dance(0))	
		{
			printf("-1\n");
			continue;
		}
		else 
		{
			for(int i = 1;i <= M;i++)
			{
				if(vis[i<<1])
					cout << "OFF";
				else cout << "ON";
				if(i == M)	cout << endl;
				else cout << " ";
			}
		}
	}
}



Problem Description
There are several switches and lamps in the room, however, the connections between them are very complicated. One lamp may be controlled by several switches, and one switch may controls at most two lamps. And what’s more, some connections are reversed by mistake, 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. 
 

Input
There are several test cases in the input. The first line of each test case contains N and M (1 <= N,M <= 500), then N lines follow, each indicating one lamp. Each line begins with a number K, indicating the number of switches controlling this lamp, then K pairs of “x ON” or “x OFF” follow.
 

Output
Output one line for each test case, each contains M strings “ON” or “OFF”, indicating the corresponding state of the switches. For the solution may be not unique, any correct answer will be OK. If there are no solutions, output “-1” instead.
 

Sample Input
   
   
2 2 2 1 ON 2 ON 1 1 OFF 2 1 1 1 ON 1 1 OFF
 

Sample Output
   
   
OFF ON -1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值