Flip Game
Time Limit: 1000MS  Memory Limit: 65536K 
Total Submissions: 4863  Accepted: 1983

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

Northeastern Europe 2000

大意:翻转棋,每次必须翻转相邻的所有和本身棋子,也就是说如果存在就是上下左右本身,棋盘4*4,全白全黑都算赢

输入:初始状态,4*4行

输出:单行输出需要多少步,如果无解是“Impossible”

#include <cstdio>
using namespace std;
int vis[65536];//就一组数据所以不初始化
int heap[65536];//16<32,用int和0,1存储整个棋盘
int index;//heap里存了多少组数据
int inver(int l,int i,int j){
int t=l;
if(i)l^=1<<(19-j-4*i);//按照左上角i=j=0;
if(i!=3)l^=1<<(11-j-4*i);
if(j)l^=1<<(16-i*4-j);
if(j!=3)l^=1<<(14-i*4-j);
l^=1<<(15-i*4-j);
if(vis[l]==0){
vis[l]=vis[t]+1;
heap[index++]=l;
}
return l;//没用上
}
int bfs(){
for(int k=0;k<index;k++){
if(heap[k]==0||heap[k]==65535)return vis[heap[k]];/*这题输入数据少*/
for(int i=0;i<4;i++){/*错误思路,循环顺序i,j,k,如果是对每一个翻转位置都尝试所有状态,
那么可能在翻转到某个位置之前,输出非最小步数*/
for(int j=0;j<4;j++){
inver(heap[k],i,j);
}
}
}
return -1;
}
int main(){
int l=0;
char ch;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
ch=getchar();
if(ch=='b')l^=1<<(15-4*i-j);//输入数据小,直接翻转
}
getchar();
}
vis[l]=1;
heap[index++]=l;
int ans=bfs()-1;//为了区别有无填充,vis[i]=1,但实际翻转次数为0
if(ans>-1)printf("%d\n",ans);
else printf("Impossible\n");
return 0;
}

  

POJ 1753 Flip Game 状态压缩,暴力 难度:1的更多相关文章

  1. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  2. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  3. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  4. POJ 1753 Flip Game (状态压缩 bfs+位运算)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 1753 Flip Game (状压+暴力)

    题目链接:http://poj.org/problem?id=1753 题意: 给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之) ...

  7. POJ - 1753 Flip Game(状压枚举)

    https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...

  8. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  9. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. [转载] 构建微服务:使用API Gateway

    原文: http://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=206889381&idx=1&sn=478ccb35294c ...

  2. thinkphp自动验证方法的使用

    建一个表单: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  3. Centos升级Python及pip

    因为CentOS系统中旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装. 1.从官网下载: wget https://www.python.org/ftp/python/ ...

  4. Css_2跟3

    #css2中的选择器 1.元素选择器     *通配符选择符.html类型选择符.id选择符.class选择符 2.关系选择器     E F(包含选择器).E>F(子选择器).E+F(E元素后 ...

  5. SciTE 文本编辑器

    个人一直使用,强大,轻型基于 Scintilla. http://www.scintilla.org/SciTEDownload.html 解压缩后,能右键文件打开 修改注册表, 使能资源管理器 右键 ...

  6. hiho_1058_combination_lock

    题目大意 给定N个字符,范围为A-Z,编号为1-N,对该字符序列进行M个操作,操作有4中类型: (1)CMD 1 i j X     将[i, j]区间内的字符均变为X (2)CMD 2 i j K  ...

  7. LWIP协议中tcp_seg结构相关指针的个人理解

    我曾经写在新浪博客上面,后来复制到这,图片就不行了. 原文地址转载  LWIP协议中tcp_seg结构相关指针的个人理解(http://blog.sina.com.cn/s/blog_7e586985 ...

  8. u盘安装ubuntu server 14.04 以及No CD-ROM drive was detected 错误

    u盘安装ubuntu server 14.04 1:下载ubuntu server14的 iso镜像文件 2:下载 UltraISO U盘镜像制作工具 : 3:使用Ultra iOS 将下载好的 is ...

  9. Linux下安装最新的Eclipse

    欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...

  10. JS预解析

    1.在逐行读js代码前,解析器会先提取所有声明的var变量和函数 js解析器会先把脚本里所有var变量声明读一遍,但是它只读变量名字,不读变量值,一开始它会赋给所有读到的var变量一个[未定义]的值. ...