标签:
Description
Input
Output
Sample Input
| input | output |
|---|---|
4 0011 0110 |
22121112 |
4 1100 1100 |
Impossible |
大意:有两组牌,每组只能从前往后拿,1表示红,0表示黑,要求拿出来的牌的顺序不能使得两种相同颜色的在一起,dfs大发好~~是时候去学一波了。感觉用来遍历所有的情况好方便 原文题解
就是不知道怎么多组输入。。最后一个在哪里结束
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool dp[1010][1010];
char a[1010],b[1010];
bool dfs(int i,int j)
{
if(!i&&!j)
return true;
if(dp[i][j] == false)//如果没有访问过
dp[i][j] = true;
else return false;
if(i >= 2 && a[i]!=a[i-1]&&dfs(i-2,j)){
printf("11");
return true;
}
if(j >= 2 && b[j]!=b[j-1] && dfs(i,j-2)){
printf("22");
return true;
}
if(i&&j&&a[i]!=b[j]&&dfs(i-1,j-1)){
printf("12");
return true;
}
return false ;
}
int main()
{
int n;
scanf("%d%s%s",&n,a+1,b+1);
memset(dp,false,sizeof(dp));
if(!dfs(n,n))
printf("Impossible\n");
return 0;
}
URAL1501——DFS——Sense of Beauty
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4497245.html