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

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

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:

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

bwwbbbwbbwwbbwww

Sample Output

4

Source

 

因为地图是4*4,每个格子只有2种情况,所以最多的情况只有2^16种。

可以把地图看成:

 0   1   2   3

 4   5   6   7

 8   9  10 11

12 13 14 15

从0开始搜索,下面的结点要么翻,要么不翻。记录每次翻完后的黑白棋的数量。如果(黑棋=16 或者 黑棋=0)表示已经达到目的。

 

1 #include 
2 #define inf 0x3f3f3f3f 3 4 char g[4][4]; 5 int f[4][4]; 6 int dir[4][2]={ 7 {
0,1},{
0,-1},{
1,0},{-1,0} 8 }; 9 int ans;10 int sum;11 12 void change(int x , int y){13 f[x][y]=!f[x][y];14 for(int i=0; i<4; i++){15 int tx=x+dir[i][0];16 int ty=y+dir[i][1];17 if( 0<=tx && tx<4 && 0<=ty && ty<4){18 f[tx][ty]=!f[tx][ty];19 }20 }21 }22 23 void dfs(int h , int step){24 if(sum==0 || sum==16){25 if(step
=16)return;29 //不翻 30 dfs(h+1,step);31 //翻32 int x=h%4;33 int y=h/4;34 int sum1=0;35 if(f[x][y]){36 sum1--; 37 }else{38 sum1++;39 }40 for(int i=0; i<4; i++){41 int tx=x+dir[i][0];42 int ty=y+dir[i][1];43 if( 0<=tx && tx<4 && 0<=ty && ty<4){44 if(f[tx][ty]){45 sum1--;46 }else{47 sum1++;48 } 49 }50 }51 change(x,y);52 sum+=sum1;53 dfs(h+1,step+1);54 sum-=sum1;55 change(x,y);56 }57 58 int main()59 {60 sum=0;61 for(int i=0; i<4; i++){62 scanf("%s",g[i]);63 }64 for(int i=0; i<4; i++){65 for(int j=0; j<4; j++){66 if(g[i][j]=='b'){67 f[i][j]=1;68 sum++; 69 }70 else{71 f[i][j]=0;72 }73 }74 }75 ans=inf;76 dfs(0,0);77 if(ans==inf){78 printf("Impossible\n");79 }else{80 printf("%d\n",ans);81 }82 return 0;83 }

 

转载于:https://www.cnblogs.com/chenjianxiang/p/3565415.html

你可能感兴趣的文章
《集体智慧编程》第12章:算法总结
查看>>
Hbase配置运行
查看>>
【转载】"30年---我与赛灵思FPGA的故事”:ZYNQ-7000使用总结(6)——AXI接口简述...
查看>>
Jenkins系列-Jenkins通过Publish over SSH插件实现远程部署
查看>>
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
查看>>
Java 中int、String的类型转换
查看>>
Oracle 查看正在执行的SQL语句
查看>>
HDU 1069 Monkey and Banana
查看>>
一个类有两个方法,其中一个是同步的,另一个是非同步的; 现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法?...
查看>>
consonant combination
查看>>
堆排序
查看>>
elk报错解决
查看>>
centos6更改时区
查看>>
struts中请求数据自动封装
查看>>
C# 高斯消元项目运用
查看>>
WUST 设计模式 实验一 单例模式的应用
查看>>
Web service(一)
查看>>
Github为什么没有记录你的Contributions
查看>>
<php>Ajax基本格式
查看>>
mybatis中的多条件查询
查看>>