TOJ 3248 Flip Game
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
因为地图是4*4,每个格子只有2种情况,所以最多的情况只有2^16种。
可以把地图看成:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
从0开始搜索,下面的结点要么翻,要么不翻。记录每次翻完后的黑白棋的数量。如果(黑棋=16 或者 黑棋=0)表示已经达到目的。
#include <stdio.h>
#define inf 0x3f3f3f3f char g[][];
int f[][];
int dir[][]={
{,},{,-},{,},{-,}
};
int ans;
int sum; void change(int x , int y){
f[x][y]=!f[x][y];
for(int i=; i<; i++){
int tx=x+dir[i][];
int ty=y+dir[i][];
if( <=tx && tx< && <=ty && ty<){
f[tx][ty]=!f[tx][ty];
}
}
} void dfs(int h , int step){
if(sum== || sum==){
if(step<ans)
ans=step;
}
if(h>=)return;
//不翻
dfs(h+,step);
//翻
int x=h%;
int y=h/;
int sum1=;
if(f[x][y]){
sum1--;
}else{
sum1++;
}
for(int i=; i<; i++){
int tx=x+dir[i][];
int ty=y+dir[i][];
if( <=tx && tx< && <=ty && ty<){
if(f[tx][ty]){
sum1--;
}else{
sum1++;
}
}
}
change(x,y);
sum+=sum1;
dfs(h+,step+);
sum-=sum1;
change(x,y);
} int main()
{
sum=;
for(int i=; i<; i++){
scanf("%s",g[i]);
}
for(int i=; i<; i++){
for(int j=; j<; j++){
if(g[i][j]=='b'){
f[i][j]=;
sum++;
}
else{
f[i][j]=;
}
}
}
ans=inf;
dfs(,);
if(ans==inf){
printf("Impossible\n");
}else{
printf("%d\n",ans);
}
return ;
}
TOJ 3248 Flip Game的更多相关文章
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- [LeetCode] Flip Game 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- poj Flip Game 1753 (枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27005 Accepted: 11694 Descr ...
- POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- poj1753 Flip Game
题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...
- java.nio.ByteBuffer中flip,rewind,clear方法的区别
对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...
- NYOJ:题目529 flip
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=529 由于此题槽点太多,所以没忍住...吐槽Time: 看到这题通过率出奇的高然后愉快的进 ...
随机推荐
- 服务器控件数据回发实现IPostBackDataHandler需注意的
我写的服务器控件(未完,模型如此) using System; using System.Collections.Generic; using System.Collections.Specializ ...
- Android 学习笔记 文本文件的读写操作
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- NFS4 挂载同主机多个目录
写在前面的话 事情是酱婶儿的,前两天实在是帮他们查日志查的心里交瘁了,而且有些时候下班了,也就不想再接到这样的需求,于是想基于 Nginx 做一个文件下载中心,在这个文件下载中心里面存有各个服务的日志 ...
- 新增扩展程序功能打包提交新版 WARNING ITMS-90473 警告问题
1.问题描述 自从在主应用中加入SiriShortCut功能之后,打包程序上传至 iTunes Connect 就会出现警告,看其原因描述是CFBundleVersion主应用与子应用的不一致导致的 ...
- 初学python-字符串中引号的使用、input简介、强制类型转换、加减乘除简写、条件判断
一.字符串中单引号和双引号的使用: 1.字符串以英文单引号' '引用.eg:'Hello World' 2.若字符串中含有单引号,则以英文双引号" "引用.eg:"I'm ...
- The server of Apache (四)——配置防盗链和隐藏版本信息
一.防盗链 防盗链就是防止别人的网站代码里面盗用我们服务器的图片.文件.视频等相关资源,比如我们的网页的图片有链接,别人把链接复制粘贴到他们的服务器页面里,图片不在他们自己的网站里,每次打开他们的网站 ...
- (转)2-SAT小结
2-sat小结 原文作者:老K 原文传送门 2-sat是什么 一类问题是这样的: (两个符号的意思 \(\lor \ or,\land \ and\)) 有n个布尔变量,现在对它们做出限制,比如\(a ...
- vue 子页面,向父页面 传值...
子组件 通过 事件 向父组件传值..... 父组件 方法: methods: { appendData: function (list) { console.log(list); for (var i ...
- LOJ #6432. 「PKUSC2018」真实排名
题目在这里...... 对于这道题,现场我写炸了......谁跟我说组合数O(n)的求是最快的?(~!@#¥¥%……& #include <cstdio> #include < ...
- Flask之flask-script 指定端口
简介 Flask-Scropt插件为在Flask里编写额外的脚本提供了支持.这包括运行一个开发服务器,一个定制的Python命令行,用于执行初始化数据库.定时任务和其他属于web应用之外的命令行任务的 ...