Flip Game
2015-08-13 17:25
274 查看
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
4000
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
Sample Output
题解:枚举翻转次数再暴力搜索。其实是简单题。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char map[5][5];
int d[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int ans;
bool judge() //全部同色
{
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(map[i][j] != map[0][0])
{
return false;
}
}
}
return true;
}
void flip(int n) //翻转
{
int x;
int y;
if(n % 4 == 0) //把n看做是格子是第几个,就是把格子看成第1-16个(编号),
{ //此时可以得到坐标和编号的关系
x = n / 4 - 1;
}
else
{
x = n / 4;
}
y = n - 4 * x - 1;
map[x][y] = (map[x][y] == 'b' ? 'w' : 'b');
for(int i = 0;i < 4;i++)
{
int xx = x + d[i][0];
int yy = y + d[i][1];
if(xx >= 0 && xx < 4 && yy >= 0 && yy < 4)
{
map[xx][yy] = (map[xx][yy] == 'b' ? 'w' : 'b');
}
}
}
bool dfs(int t,int s,int n)
{
if(n > 16) //只能翻转16个
{
return false;
}
if(s == t) //翻转次数到了
{
if(judge()) //全部颜色一致??
{
return true;
}
return false;
}
flip(n + 1); //翻转
if(dfs(t,s + 1,n + 1)) //表示翻转
{
return true;
}
flip(n + 1); //翻转回去,下面表示不翻转该点
if(dfs(t,s,n + 1))
{
return true;
}
return false;
}
int main()
{
while(scanf("%s",map[0]) != EOF)
{
for(int i = 1;i < 4;i++)
{
scanf("%s",map[i]);
}
bool flag = true;
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(map[0][0] != map[i][j])
{
flag = false;
break;
}
}
}
if(flag)
{
printf("0\n");
continue;
}
flag = true;
for(int i = 1;i <= 16;i++)
{
if(dfs(i,0,0)) //i表示翻转次数,0表示翻转了几次,0表示翻转的是第几个
{
printf("%d\n",i);
break;
}
if(i == 16)
{
flag = false;
}
}
if(!flag)
{
printf("Impossible\n");
}
}
return 0;
}
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
4000
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
题解:枚举翻转次数再暴力搜索。其实是简单题。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char map[5][5];
int d[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int ans;
bool judge() //全部同色
{
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(map[i][j] != map[0][0])
{
return false;
}
}
}
return true;
}
void flip(int n) //翻转
{
int x;
int y;
if(n % 4 == 0) //把n看做是格子是第几个,就是把格子看成第1-16个(编号),
{ //此时可以得到坐标和编号的关系
x = n / 4 - 1;
}
else
{
x = n / 4;
}
y = n - 4 * x - 1;
map[x][y] = (map[x][y] == 'b' ? 'w' : 'b');
for(int i = 0;i < 4;i++)
{
int xx = x + d[i][0];
int yy = y + d[i][1];
if(xx >= 0 && xx < 4 && yy >= 0 && yy < 4)
{
map[xx][yy] = (map[xx][yy] == 'b' ? 'w' : 'b');
}
}
}
bool dfs(int t,int s,int n)
{
if(n > 16) //只能翻转16个
{
return false;
}
if(s == t) //翻转次数到了
{
if(judge()) //全部颜色一致??
{
return true;
}
return false;
}
flip(n + 1); //翻转
if(dfs(t,s + 1,n + 1)) //表示翻转
{
return true;
}
flip(n + 1); //翻转回去,下面表示不翻转该点
if(dfs(t,s,n + 1))
{
return true;
}
return false;
}
int main()
{
while(scanf("%s",map[0]) != EOF)
{
for(int i = 1;i < 4;i++)
{
scanf("%s",map[i]);
}
bool flag = true;
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(map[0][0] != map[i][j])
{
flag = false;
break;
}
}
}
if(flag)
{
printf("0\n");
continue;
}
flag = true;
for(int i = 1;i <= 16;i++)
{
if(dfs(i,0,0)) //i表示翻转次数,0表示翻转了几次,0表示翻转的是第几个
{
printf("%d\n",i);
break;
}
if(i == 16)
{
flag = false;
}
}
if(!flag)
{
printf("Impossible\n");
}
}
return 0;
}
相关文章推荐
- poj 2823(单调队列)
- 2440启动代码分析
- C++ primer 【笔记】关联容器 set
- Linux学习笔记---文件解压缩命令---gzip ==
- iOS每日一记之---------改变Btn点击时的背景颜色
- 不同.net版本实现单点登录
- Web开发基础知识
- HDU 1251 统计难题 trie树
- Centos7安装配置Mongodb3
- 一个计时器, 点击按钮 让他 停一会, 5s后继续自动运行
- iNOC产品部-杨辉三角的变形
- Prime Path(POJ--3126
- 按返回键的时候实现按home键的效果
- KMP算法模板
- 在本地机器上能访问tomcat,远程机器访问不了的解决方法
- sql字符串累加
- GridView控件实现数据的修改(第9节)
- java GUI(UI创建与监听机制)
- HttpClient
- Consistent Hashing算法