Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 59468   Accepted: 24750

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

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

 
 
 #include <iostream>
#include <cstdio> using namespace std; char str[];
int a[][];
int ans=; void flip(int x,int y)
{
a[x][y]=!a[x][y];
a[x-][y]=!a[x-][y];
a[x+][y]=!a[x+][y];
a[x][y-]=!a[x][y-];
a[x][y+]=!a[x][y+];
} bool same_color(void)
{
for(int i=;i<=;++i)
{
for(int j=;j<=;++j)
{
if(a[i][j]!=a[][])
{
return false;
}
}
}
return true;
} void dfs(int x,int y,int t)
{
// 判断是否同色,同色满足,返回
if(same_color())
{
if(ans>t)
{
ans=t;
}
return;
}
// 深搜截止条件
if(x>)
{
return;
} if(y==)
{
// 当前不反转
dfs(x+,,t);
// 当前反转
flip(x,y);
dfs(x+,,t+);
// 状态还原
flip(x,y);
}
else
{
dfs(x,y+,t);
flip(x,y);
dfs(x,y+,t+);
flip(x,y);
} } int main()
{
for(int i=;i<=;++i)
{
scanf("%s",str+);
for(int j=;j<=;++j)
{
// a->0,b->1
if(str[j]=='w')
{
a[i][j]=;
}
else
{
a[i][j]=;
}
//a[i][j]=str[j]-'a';
//printf("%d ",a[i][j]);
}
//printf("\n");
} dfs(,,); if(ans==)
{
printf("Impossible\n");
}
else
{
printf("%d\n",ans);
} return ;
}

最新文章

  1. JS判断日期是否在同一个星期内,和同一个月内
  2. navicat 导入sql文件乱码问题解决
  3. 属性文件Plist
  4. SpringMVC03controller中定义多个方法
  5. Python[小甲鱼009了不起的分支和循环3]
  6. AOP及spring AOP的使用
  7. 开发中少不了的Fun -- 微信开发IOS端alert/confirm提示信息,去除网址(URL)的方法
  8. React-安装和配置redux调试工具Redux DevTools
  9. 2018.12.22 bzoj3277: 串(后缀自动机+启发式合并)
  10. Redis笔记(1)数据结构与对象
  11. 案例导入和导出Scott用户
  12. nginx / apache / tomcat /resin等 http server的benchmark性能测试方法
  13. IT零起步-CentOS6.4部署OpenVPN服务器
  14. Linux下MyCat和MyCat_web的安装和配置
  15. Supervisor安装与配置(非守护进程管理工具)
  16. Java 字符串 String
  17. C++异常处理基本句法测试
  18. 使用公钥和私钥实现LINUX下免密登录
  19. mysql数据库优化的几种方法
  20. poj 1595 Prime Cuts

热门文章

  1. [状压DP思路妙题]图
  2. es8对object快速遍历的方法
  3. 【模板整理】Tarjan
  4. idea使用PlantUML画类图教程
  5. docker创建mysql容器,并挂载数据+配置
  6. WeihanLi.Npoi 根据模板导出Excel
  7. 常用API_1
  8. 面试官问你MyBatis SQL是如何执行的?把这篇文章甩给他
  9. linux系统iot平台编程阶段总结
  10. 如何从Serilog请求日志记录中排除健康检查终结点