POJ 1176 dfs

原创 2012年03月22日 10:52:47

      这道题真恶心,,写出来dfs后提交,,wa了,仔细看题,才发现还要对二进制升序排序后输出。我去,,真够麻烦的。接下来就是转化成字符串,然后在用字符串比较,最后输出。。。值得注意的是,如果按开关的次数大于4,则和c%4+4次的效果是一样的,题目:

Party Lamps
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3315   Accepted: 1127

Description

To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered from 
1 to N. The lamps are connected to four buttons: 
button 1 -- when this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON. 
button 2 -- changes the state of all the odd numbered lamps. 
button 3 -- changes the state of all the even numbered lamps. 
button 4 -- changes the state of the lamps whose number is of the form 3K+1 (with K >= 0), i.e., 1,4,7,... 
There is a counter C which records the total number of button presses. 
When the party starts, all the lamps are ON and the counter C is set to zero. 

You are given the value of counter C and information on the final state of some of the lamps. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.

Input

Your program is to read from standard input. The input contains four lines, describing the number N of lamps available, the number C of button presses, and the state of some of the lamps in the final configuration. 
The first line contains the number N and the second line the final value of counter C. The third line lists the lamp numbers you are informed to be ON in the final configuration, separated by one space and terminated by the integer -1. The fourth line lists the lamp numbers you are informed to be OFF in the final configuration, separated by one space and terminated by the integer -1. 

The parameters N and C are constrained by: 
10 <= N <= 100 
1 <= C <= 10000 
The number of lamps you are informed to be ON, in the final configuration, is less than or equal to 2.The number of lamps you are informed to be OFF, in the final configuration, is less than or equal to 2.

Output

Your program is to write to standard output. The output must contain all the possible final configurations (without repetitions) of all the lamps. There is at least one possible final configuration. Each possible configuration must be written on a different line. Each line has N characters, where the first character represents the state of lamp 1 and the last character represents the state of lamp N. A 0 (zero) stands for a lamp that is OFF, and a 1 (one) stands for a lamp that is ON. Configurations should be listed in binary ascending order.

Sample Input

10
1
-1
7 -1

Sample Output

0000000000
0101010101
0110110110
ac代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
int num[110];
int on[2],off[2];
int n,c,k,kk,total;//total记录的是结果共有多少,k记录的是最后开着的灯有几个,kk记录的是最后关着的灯有几个
struct ss{
  char ch[110];
}aa[1010];
void dfs(int x){
	if(x==c+1){
		int flag=1;
		for(int i=0;i<k;++i){
			if(num[on[i]]!=1){
			  flag=0;break;
			}
		}
		for(int i=0;i<kk;++i){
			if(num[off[i]]!=0){
			  flag=0;break;
			}
		}
		if(flag==1){
		  char str[105];
		  int newflag=1;
		  for(int i=1;i<=n;++i)
		  {str[i-1]='0'+num[i];}
		  str[n]='\0';
		  for(int i=0;i<total;++i){
			  if(strcmp(str,aa[i].ch)==0)
			  {flag=0;break;}
		  }
		  if(flag==1){
		  strcpy(aa[total].ch,str);
		  total++;
		  }
		  return;
		}
	}
	else{
		for(int i=1;i<=4;++i){
			if(i==1){
				for(int j=1;j<=n;++j){
				  num[j]=!num[j];
				}
				dfs(x+1);
				for(int j=1;j<=n;++j)
					num[j]=!num[j];
			}
			else if(i==2){
				for(int j=1;j<=n;++j){
				  if(j%2)
					  num[j]=!num[j];
				}
				dfs(x+1);
				for(int j=1;j<=n;++j)
					if(j%2)
						num[j]=!num[j];
			}
			else if(i==3){
				for(int j=1;j<=n;++j){
				  if(j%2==0)
					  num[j]=!num[j];
				}
				dfs(x+1);
				for(int j=1;j<=n;++j)
					if(j%2==0)
						num[j]=!num[j];
			}
			else{
				for(int j=1;j<=n;++j){
				  if((j-1)%3==0)
					  num[j]=!num[j];
				}
				dfs(x+1);
				for(int j=1;j<=n;++j)
					if((j-1)%3==0)
						num[j]=!num[j];
			}
		}
	}
}
int cmp(ss a,ss b){
	return strcmp(a.ch,b.ch)<0;
}
int main(){
	//freopen("11.txt","r",stdin);
	while(~scanf("%d%d",&n,&c)){
		if(c>4){
	      c=c%4+4;
		}
	  int x,y;
	  for(int i=1;i<=n;++i)
	  {num[i]=1;}
	  k=0,kk=0,total=0;
	  while(scanf("%d",&x)&&x!=-1){
	    on[k++]=x;
	  }
	  while(scanf("%d",&y)&&y!=-1)
		 off[kk++]=y;
	  dfs(1);
	  sort(aa,aa+total,cmp);//字符串排序
	  for(int i=0;i<total;++i)
		  printf("%s\n",aa[i].ch);
	}
  return 0;
}


POJ 1176 Party Lamps (DFS)

POJ 1176

poj1176 Party Lamps dfs优化+规律枚举

To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered from 1 to N...
  • ly59782
  • ly59782
  • 2016年08月19日 16:45
  • 439

POJ 1176 Party Lamps 稍有玄机

题意: 对于一串彩灯,提供四种改变彩灯状态(ONOFF)的操作:a.改变所有彩灯状态;b.改变奇数彩灯状态;c.改变偶数彩灯状态;d.改变3k+1号彩灯状态(1,4,7,10...)。 给定彩灯数目,...

poj1176 枚举

/** * poj1176 枚举 * 我想这个题的本意应该是DFS,但因题义有限制,所以一共就8种情况,枚举就行了,实在不必费力DFS * 首先,我们只看前6个灯的状态就可以了,剩下的都是以6为...

POJ3373-Changing Digits【DFS+强剪枝】

  • 2011年08月19日 00:44
  • 12KB
  • 下载

poj2488——dfs深度优先遍历

  • 2013年07月24日 10:21
  • 2KB
  • 下载

POJ 1176 - Party Lamps

Description To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered...
  • Sureina
  • Sureina
  • 2016年08月23日 14:56
  • 157

poj 3009 dfs暴力解决最短路

题目就不粘了,题意是这样的:一个带网格的方格板,有两种方格,一种是白色的,一种是网状的。白色表示可以通过,用数字0表示,网状的表示障碍物不能通过,用数字1表示。起点和终点分别在两个白色的上面,分别用数...
  • vita_xu
  • vita_xu
  • 2015年02月06日 13:08
  • 742

再次理解DFS(POJ1014)

原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两...
  • li4951
  • li4951
  • 2012年04月07日 10:33
  • 3870

poj 2157 Maze 枚举排列+dfs搜迷宫

题意: 给一个迷宫,求是否可从S走到G,迷宫中有门课钥匙,要获得相应种类的所有钥匙才能打开该种门(比如获得所有a才能打开A)。 分析: 枚举打开门的顺序,搜路径即可。 代码: //poj 2157 /...
  • sepNINE
  • sepNINE
  • 2015年09月11日 17:27
  • 677
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1176 dfs
举报原因:
原因补充:

(最多只允许输入30个字)