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: 看到这题通过率出奇的高然后愉快的进 ...
随机推荐
- PostBack
PostBack 字面意义 Post提交 Back回来. 提交回来. 1. AutoPostBack 服务器控件需要设置 AutoPostBack="true" 后才会提交服务器. ...
- .net Stream篇(六)
BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...
- SynchronizationContext应用
这个类的应用,官方的说明并不是很多,主要原因是因为微软又出了一些基于SynchronizationContext的类.比如:BackgroundWorker 大家写程序时经常碰到子线程调用UI线程的方 ...
- Struts2学习第一天--Struts2的概述、Struts2的入门、Struts2常见的配置、Struts2的Action的编写
action的name要与访问路径对应.hello.action. 加到tomcat启动 访问:http://localhost:8080/struts2-1/demo1/demo1.jsp 改为su ...
- poi将图片导入excel(Java代码)
package com.fh.util;import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; imp ...
- 堆排序工具类(适用于top k问题,java泛型实现)
代码如下,作用如标题所述 public class HeapSort { //方法作用:取出list里面的最小的 k 个值 public static <T extends Comparable ...
- spring+hibernate中的事务
上下文: 从数据库服务器上获取数据可以,保存的时候增加了事务提交,即em.flush方法,报错no transaction in progress 报错信息: no transaction in pr ...
- P4854 MloVtry的咸鱼树 状压+最短路
$ \color{#0066ff}{ 题目描述 }$ 俗话说种瓜得瓜,种豆得豆,MloVtry把自己砍掉一半埋进了土里,于是它得到了一颗n个点的咸鱼树. 但是问题是由于MloVtry只舍得埋下一半的自 ...
- go的三个常用命令go run go build go install
go的三个常用命令 go run go build go install 命令源码文件:含有 main函数 的文件 库源码文件:不包含 main函数 的文件, 主要用于编译成静态文件.a供其他包调用 ...
- Node.js的mysql执行多表联合查询
数据库(test)中的表结构(admin.user) //执行多表结合查询 var mysql = require('mysql'); var connection = mysql.createCon ...