标签:des blog http io color ar os sp for
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 31623 | Accepted: 13758 |
Description
Consider the following position as an example: Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
/*
ID :KvpMrank1
status:AC
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
char c;
int chess[20],ans=0;
bool flag;
void solve(int l)
{
chess[l]^=1;//位运算
if(l%4!=1)
chess[l-1]^=1;
if(l%4!=0)
chess[l+1]^=1;
if(l>4)
chess[l-4]^=1;
if(l<13)
chess[l+4]^=1;
}
bool check()
{
for(int i=2;i<=16;i++)
if(chess[i]!=chess[i-1])
return false;
return true;
}
void dfs(int num,int ci,int sum)//num为第几个棋子,ci填第几次,sum一共填几次;
{
if(ci==sum)
{
flag=check();
return ;
}
for(int i=num+1;i<=16;i++)
{
solve(i);
dfs(i,ci+1,sum);
if(flag) return ;
solve(i);//回溯
}
}
int main()
{
memset(chess,0,sizeof(chess));
for(int i=1;i<=16;i++)
{
cin>>c;
if(c==‘b‘)
chess[i]=1;
else
chess[i]=0;
}
flag=check();
if(flag) printf("0\n");
else
{
for(int i=1;i<=16;i++)
{
flag=false;
dfs(0,0,i);
if(flag)
{
ans=i;
break;
}
}
if(flag)
printf("%d\n",ans);
else
printf("Impossible\n");
}
//system("pause");
return 0;
}
标签:des blog http io color ar os sp for
原文地址:http://www.cnblogs.com/a972290869/p/4101050.html