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

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

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24957   Accepted: 10755

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

开始时直接枚举16种状态,用的bfs不过超时了,后来看了一下网上的思路,大多是bfs加位运算,不过表示不怎么懂位运算,
试着用dfs也没搞出来,看了一下别人的代码,表示很神奇的说
 
1 #include
2 char map[4][4]; 3 int vis[4][4]; 4 int step,flag; 5 int judge() 6 { 7 int i,j; 8 for(i=0;i<4;i++) 9 for(j=0;j<4;j++)10 if(vis[i][j]!=vis[0][0])11 return 0;12 13 return 1;14 }15 void turn(int i,int j)16 {17 vis[i][j]=!vis[i][j];18 if(i>0) vis[i-1][j]=!vis[i-1][j];19 if(i<3) vis[i+1][j]=!vis[i+1][j];20 if(j>0) vis[i][j-1]=!vis[i][j-1];21 if(j<3) vis[i][j+1]=!vis[i][j+1];22 }23 void dfs(int i,int j,int step1)24 {25 26 if(step==step1)27 {28 if(judge())29 flag=1;30 31 return ;32 }33 if(flag ||i==4) return ;34 turn(i,j);35 if(j<3)36 dfs(i,j+1,step1+1);37 else38 dfs(i+1,0,step1+1);39 turn(i,j);40 if(j<3)41 dfs(i,j+1,step1);42 else43 dfs(i+1,0,step1);44 return ;45 }46 int main()47 {48 int i,j;49 for(i=0;i<4;i++)50 scanf("%s",map[i]);51 for(i=0;i<4;i++)52 {53 for(j=0;j<4;j++)54 {55 if(map[i][j]=='b')56 vis[i][j]=1;57 else58 vis[i][j]=0;59 60 }61 62 }63 flag=0;64 for(step=0;step<=16;step++)65 {66 dfs(0,0,0);67 if(flag)68 break;69 }70 if(flag)71 printf("%d\n",step);72 else73 printf("Impossible\n");74 return 0;75 }
View Code

 

 

转载于:https://www.cnblogs.com/llei1573/p/3204704.html

你可能感兴趣的文章
高德室内地图解析
查看>>
python学习5
查看>>
面试经验谈(经典)
查看>>
Linux上面缺少rz和sz命令
查看>>
ArcGIS Python编程 二
查看>>
rmmod出现的can't unload 'xxx': Device or resource busy错误
查看>>
[离散时间信号处理学习笔记] 5. 离散时间信号与系统的频域表示
查看>>
python文件基本操作
查看>>
git遇到的问题记录2019.05.07
查看>>
DOM, DOCUMENT, BOM, WINDOW 有什么区别?
查看>>
0612学习进度条
查看>>
vim 删除多列
查看>>
BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
查看>>
Vue 学习笔记 -- inline-template
查看>>
贪吃蛇修改
查看>>
LeetCode "Interleaving String"
查看>>
把类的析构函数写成虚函数的用意
查看>>
如何在个人博客首页中添加访问计数器
查看>>
Swift # GET&POST请求 网络缓存的简单处理
查看>>
【错误笔记——Swing】关于让panel监听键盘不成功的错误
查看>>