首页 技术 正文
技术 2022年11月9日
0 收藏 474 点赞 3,495 浏览 2168 个字

Flip Game

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

Description

Flip game is played on a rectangular 4×4 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

Northeastern Europe 2000  

 #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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291