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

本文共 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

你可能感兴趣的文章
CSS 属性篇(二):transform属性
查看>>
Go语言中的Interface
查看>>
谈谈Promise那点事(二)
查看>>
express+request实现-图夫在线爬取网页图片
查看>>
ES6:数组扩展
查看>>
关于同步的一点思考-下
查看>>
ADB原理,Wi-Fi连接,常用命令及拓展
查看>>
Python学习之网络编程
查看>>
Eclipse 如何快速修改工程名及包名
查看>>
spring cloud微服务分布式云架构 - Spring Cloud简介
查看>>
Android之哭笑不得的BUG--RelativeLayout设置的marginbottom失效,马萨卡..
查看>>
Application.onCreate()会造成Service启动ANR么?
查看>>
css-水平居中、垂直居中(初级篇)
查看>>
一线互联网常见的14个Java面试题,你颤抖了吗
查看>>
学习webpack4 - 样式处理
查看>>
nextjs踩坑
查看>>
TiKV 源码解析系列文章(二)raft-rs proposal 示例情景分析
查看>>
【基础】小程序实现聊天气泡样式
查看>>
153. Find Minimum in Rotated Sorted Array
查看>>
Java程序员幽默爆笑锦集
查看>>