博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flip Game poj1753
阅读量:6029 次
发布时间:2019-06-20

本文共 3250 字,大约阅读时间需要 10 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32961   Accepted: 14407

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: 
  1. Choose any one of the 16 pieces. 
  2. 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

bwwbbbwbbwwbbwww

Sample Output

4

Source

dfs枚举

1 #include
2 #include
3 #include
4 using namespace std; 5 6 int Map[6][6],a[5]={
0,0,1,0,-1},b[5]={
0,1,0,-1,0}; 7 int flg,step; 8 9 int judge(int Map[6][6])10 {11 int i,j;12 for(int i=1;i<=4;i++)13 for(j=1;j<=4;j++)14 {15 if(Map[i][j]!=Map[1][1])16 return 0;17 }18 return 1;19 }20 21 int flip(int r,int c)22 {23 for(int i=0;i<5;i++)24 {25 if(Map[r+a[i]][c+b[i]]==1)26 Map[r+a[i]][c+b[i]]=0;27 else28 Map[r+a[i]][c+b[i]]=1;29 }30 }31 32 void dfs(int r,int c,int deep)33 {34 int i,j;35 if(deep==step)36 {37 flg=judge(Map);38 return;39 }40 if(flg==1 || r>4)41 {42 return;43 }44 flip(r,c);45 if(c<4)46 dfs(r,c+1,deep+1);47 else48 dfs(r+1,1,deep+1);49 flip(r,c);50 if(c<4)51 dfs(r,c+1,deep);52 else53 dfs(r+1,1,deep);54 return;55 }56 57 58 int main()59 {60 int i,j;61 char k;62 memset(Map,0,sizeof(Map));63 for(i=1;i<=4;i++)64 {65 for(j=1;j<=4;j++)66 {67 scanf("%c",&k);68 if(k=='b')69 Map[i][j]=1;70 }71 getchar();72 }73 for(step=0;step<=16;step++)74 {75 dfs(1,1,0);76 if(flg)77 break;78 }79 if(flg)80 printf("%d\n",step);81 else82 printf("Impossible\n");83 return 0;84 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4444453.html

你可能感兴趣的文章
c++中,bool与int 的区别
查看>>
cout输出控制——位数和精度控制
查看>>
Parse 构建移动APP后台服务(一)
查看>>
ZT 过你想要的生活:需要追问自己的50个问题
查看>>
使用OpenSSL生成非对称密钥
查看>>
0.11.1版本docker安装与使用
查看>>
脖子和腰围的关系
查看>>
转一个权限设计
查看>>
【R】R语言使用命令行参数 - [编程技巧(Program Skill)]
查看>>
mysql优化总结
查看>>
经典算法题每日演练——第二题 五家共井
查看>>
存储过程中拼接的变量和点的问题
查看>>
ASP.NET那点不为人知的事(一)
查看>>
实战 SQL Server 2008 数据库误删除数据的恢复 (转)
查看>>
Windows Phone 独立存储查看器
查看>>
js与php转换时间戳
查看>>
NET使用NPOI组件将数据导出Excel-通用方法 【推荐】
查看>>
数学笔记--积分
查看>>
北京和硅谷在创新方面的区别
查看>>
技术人生:自卑
查看>>