Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46061 | Accepted: 19735 |
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote
game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
这个题目是一个比较不错的DFS的题目,作为萌新,刚开始学习DFS,慢慢不断了解DFS,其实感觉它没有自己想想的那么难,更好的理解,它就是一棵树,利用前序遍历,这样来做,不懂树的遍历方法,可以手动百度下。
#include<stdio.h>
#include<string.h>
int step =0x7fffffff;
void dfs(int a,int b,char c[10][10])
{
int i,j,sum=0;
char ch[10][10];
for(i=0;i<4;i++)//每次DFs后都扫描一边是否满足题意
{
for(j=0;j<4;j++)
{
if(c[i][j] == 'b')
sum++;
}
}
if(sum==0 || sum==16)
{
if(b < step)
step=b;
return ;
}
if(a==16)//a可以看过是扫描的棋子的数
return ;
for(i=0;i<4;i++)
strcpy(ch[i],c[i]);
dfs(a+1,b,ch);//当前位置不作处理向下递归
int r=a/4; //行数
int w=a%4;//列数
if(ch[r][w]=='b')//翻转当前的状态
ch[r][w]='w';
else
ch[r][w]='b';
if(r>=1)//行数大于等于一 代表有上界
{
if(ch[r-1][w]=='b')
ch[r-1][w]='w';
else
ch[r-1][w]='b';
}
if(r<=2)//说明拥有下界
{
if(ch[r+1][w]=='b')
ch[r+1][w]='w';
else
ch[r+1][w]='b';
}
if(w>=1)//拥有左边的界限
{
if(ch[r][w-1]=='b')
ch[r][w-1]='w';
else
ch[r][w-1]='b';
}
if(w<=2)//拥有右边的界限
{
if(ch[r][w+1]=='b')
ch[r][w+1]='w';
else
ch[r][w+1]='b';
}
dfs(a+1,b+1,ch);
}
int main()
{
int i;
char a[10][10];
for(i=0;i<4;i++)
scanf("%s",a[i]);
dfs(0,0,a);
if(step == 0x7fffffff)
printf("Impossible\n");
else
printf("%d\n",step);
return 0;
}
就是查询了每种情况,然后找到最短的那种,记录下来~数据量大的话DFS肯定超时,因为它就和暴力差不多了,每种情况都扫描到了。