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: 看到这题通过率出奇的高然后愉快的进 ...
随机推荐
- angular 守卫路由
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; im ...
- 在一个java类里,private int a; 什么时候要使用integer
private Integer index; if(index == null) index = 0; else this.index = index; Integer有一个明显的好处,就是它能比in ...
- Windowns DOS For 循环实例
update_all.bat代码示例: @echo off echo ***************************************************************** ...
- Java面向对象之关键字super 入门实例
一.基础概念 (一)super关键字 super关键字的用法和this相似.this代表的是当前对象.super代表的是父类中内存空间. 子父类中是不会出现同名属性的情况. (二)继承中.成员变量问题 ...
- 《 V I M 教 程 》 —— 版本 1.7
=============================================================================== = 欢 迎 阅 读 < V I M ...
- Error creating bean with name 'dateSource' defined in file 错误信息
问题的原因: 在web项目中搭建SSM框架,启动Tomcat时出现错误信息 有配置文件:applicationContext-mybatis.xml (Spring配置) spring-servlet ...
- Day 4 上午
内容提要 进制转换 高精度 数论:筛法,gcd/exgcd,逆元 进制转换 10=2^3+2^0=1010(2)10=3^2+3^0=101(3) 10进制x-->k进制:短除法 k进制x--& ...
- shell-003:用for循环统计内存使用量
shell-100主要是用于练习! #!/bin/bash # 统计内存的使用量(这里用ps统计) # 第一步:不打印第一行,这里的sed ‘1d’ 去掉 for n in `ps aux |sed ...
- Hibernate学习笔记(三)—— Hibernate的事务控制
Hibernate是对JDBC的轻量级封装,其主要功能是操作数据库.在操作数据库过程中,经常会遇到事务处理的问题,接下来就来介绍Hibernate中的事务管理. 在学习Hibernate中的事务处理之 ...
- Kibana6.x.x源码分析--ngReact使用
ngReact GitHub地址:https://github.com/ngReact/ngReact