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

位运算,速度还将就,代码比较短

Posted by justryit at 2013-07-26 11:55:57 on Problem 1753 and last updated at 2013-07-26 12:01:05
#include<cstdio>
#include<cstdlib>
using namespace std;
int ans=999999;
int mi[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
void search(int x,int f,int st)/*x表当前第几位,f表棋盘状态,st代表翻转次数*/
{
	if((f==65535)or(f==0))
	{
		if(st<ans) ans=st;
		return;
	}
	if(x==17) return;
	if(st>=ans) return;
	search(x+1,f,st);
	f^=(1<<((17-x)-1));//改变自己
	if(x%4!=0) f^=(1<<((17-x-1)-1));//改变右边的
	if(x%4!=1) f^=(1<<((17-x+1)-1));//改变左边的
	if(x>4) f^=(1<<((17-x+4)-1));//改变上面的
	if(x<=12) f^=(1<<((17-x-4)-1));//改变下面的
	search(x+1,f,st+1);
}
int main()
{
	int s=0;
	for(int i=1;i<=4;i++)
	{
		for(int j=1;j<=4;j++)
		{
			char ch;
			scanf("%c",&ch);
			if(ch=='b') s+=mi[16-4*i+4-j];/*把矩阵变成一个数,第i位为1代表第(i/4)行第(i%4)(0代表4)列是Black;*/
		}
		scanf("\n");
	}
	search(1,s,0);
	if (ans==999999) printf("Impossible\n");
	else printf("%d\n",ans);
	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