Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

不用DFS,直接二进制枚举就可以!附代码

Posted by proverbs at 2012-09-24 11:08:50 on Problem 1176
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#define N 2000
using namespace std;
int son,soff,cnt,n,c,off[N],on[N];
string str[N];
bool lig[N],ans[N][N];
void read()
{
	son=0; soff=0; cnt=0;
	int a;
	for(int i=1;i<=N-10;i++) str[i].clear();
	while(scanf("%d",&a),~a) on[++son]=a;
	while(scanf("%d",&a),~a) off[++soff]=a;
}
int getjo(int zt)
{
	int rt=0;
	for(int i=0;i<4;i++)
		if((1<<i)&zt) rt++;
	return rt;
}
void prt(int x)
{
	cout<<x<<endl;
	for(int i=1;i<=n;i++) printf("%d",lig[i]);
	printf("\n");
}
void go()
{
	for(int i=0;i<(1<<4);i++)
	{
		bool fg=true;
		memset(lig,1,sizeof lig);
		if((1<<0)&i) 
			for(int j=1;j<=n;j++) lig[j]=!lig[j];//全部翻转 
		if((1<<1)&i)
			for(int j=1;j<=n;j+=2) lig[j]=!lig[j];//奇数灯翻转
		if((1<<2)&i)
			for(int j=2;j<=n;j+=2) lig[j]=!lig[j];//偶数灯翻转
		if((1<<3)&i)
			for(int j=1;j<=n;j+=3) lig[j]=!lig[j];//3n+1翻转
		
		for(int j=1;j<=son;j++)
			if(lig[on[j]]==false)
				{fg=false;break;}
		if(!fg) continue;
		for(int j=1;j<=soff;j++)
			if(lig[off[j]]==true)
				{fg=false;break;}
		if(!fg) continue;
		
		if(getjo(i)<=c&&((getjo(i)&1)==(c&1)))
		{
			cnt++;
			for(int j=1;j<=n;j++) ans[cnt][j]=lig[j];
		}
	}
	for(int i=1;i<=cnt;i++)
	{
		for(int j=1;j<=n;j++)
			str[i].push_back(ans[i][j]+'0');
	}
	sort(str+1,str+1+cnt);
	for(int i=1;i<=cnt;i++) cout<<str[i]<<endl;
}
int main()
{
	while(scanf("%d%d",&n,&c)!=EOF)
	{
		read();
		go();
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator