题目链接:http://poj.org/problem?id=1753

Flip Game

Time Limit: 1000MS Memory Limit: 65536K

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

bwwb

bbwb

bwwb

bwww

Sample Output

4


  • 题意就是给你一个4*4的图,要求你通过一定的翻转将图全改为b或者w。每次翻转一个点这个点的上下左右也会跟着翻转。问最少需要翻转多少次。

  • 其实是一个很有意思的搜索题,因为每个点只有两种情况,所以可以把一个图看成一个二进制的数字,就可以使用位运算来模拟翻转, 然后bfs,看到达目标数字需要的最少步骤。


#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e5;
bool vis[maxn];
struct node
{
int va,step;
} now,Next;
char s[10][10]; void init()
{
memset(vis,0,sizeof(vis));
for(int i=1; i<4; i++)
scanf("%s",s[i]);
now.step = 0;
now.va = 0;
int t = 1;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
int k;
if(s[i][j] == 'b')
k = 1;
else
k = 0;
now.va += k*t;
t *= 2;
}
}
} void deal(int pos)
{
int num[20],x = now.va;
Next.step = now.step + 1;
//先转化成二进制
for(int i=0; i<=15; i++)
{
num[i] = x % 2;
x /= 2;
}
num[pos] ^= 1;
if(pos > 4)
num[pos-4] ^= 1;
if(pos%4 != 3)
num[pos+1] ^= 1;
if(pos%4 != 0)
num[pos-1] ^= 1;
if(pos < 12)
num[pos+4] ^= 1;
int t= 1,ans = 0;
//操作之后转化为10进制,10进制更好标记状态
for(int i=0;i<16;i++)
{
ans += t*num[i];
t*= 2;
}
Next.va = ans;
} int bfs()
{
if(now.va == 0 || now.va == 65535)
return now.step;
queue<node> qu;
qu.push(now);
vis[now.va] = true;
while(!qu.empty())
{
now = qu.front();
qu.pop();
for(int i=0; i<16; i++)
{
deal(i);
if(!vis[Next.va])
{
if(Next.va == 0 || Next.va == 65535)//最终的情况代表的数字就是0或者65535
return Next.step;
vis[Next.va] = true;
qu.push(Next);
}
}
}
return -1;
} int main()
{
while(scanf("%s",s[0])!=EOF)
{
init();
int ans = bfs();
if(ans != -1)
printf("%d\n",ans);
else
printf("Impossible\n");
}
return 0;
}

最新文章

  1. jdk源码分析红黑树——插入篇
  2. IntelliJ工程导入
  3. &lt;&lt;&lt; struts 的一系列介绍
  4. js中的this,call及apply
  5. 几个有用的JavaScript/jQuery代码片段(转)
  6. iOS之04-方法的声明和实现
  7. 单片机上的发光二极管(LED灯)
  8. css之选择器篇
  9. bash feature
  10. NoSQL选型
  11. ubuntu14.04 安装
  12. rsyslog 同时发生nginx 访问日志和错误日志
  13. SecureCRT-转换密钥-Xshell-配置服务-使用xshell登录远程linux服务器
  14. OI中常犯的傻逼错误总结
  15. linux install ftp server
  16. django post和get 比较
  17. Struts2(三)
  18. node中express的中间件之cookieParser
  19. js面向对象2
  20. 6.1(java学习笔记)File类

热门文章

  1. 批量插入,update
  2. (转)Linxu磁盘体系知识介绍及磁盘介绍
  3. 动态时间规整DTW
  4. 六,IO系统
  5. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(3):添加一个视图
  6. dedecms会员中心编辑器无法上传图片
  7. static 关键字用法
  8. class类型重定义,防止头文件重复加载
  9. mysql-单表操作
  10. Android(java)学习笔记125:保存数据到SD卡 (附加:保存数据到内存)