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