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. html的空格和换行显示【摘自网络】

    一.HTML 代码中的所有连续的空格或空行(换行)都会被显示为一个空格,不管是内容还是标签之间. 二.当我们想让它们在同一行连续显示时,就让所有的代码之间没有空格,也不要换行. 三.当我们想要显示连续 ...

  2. 【服务器环境搭建-Centos】系统分区 待续

    df命令查看,显示/dev/vda,而不是sda或hda ,为什么? 虚拟机为了提升性能,一般使用virtio作为磁盘驱动,在虚拟机里面磁盘会显示成vda而不是传统的sda,有什么办法可以让他显示成s ...

  3. Maven打包遇到的一些问题(zhuan)

    http://qqbwww.iteye.com/blog/2042307 **************************************** #Maven打包遇到的一些问题 ## 1.将 ...

  4. git服务器搭建-new

    http://blog.chinaunix.net/uid-26729093-id-4652536.html 基本命令使用:http://www.cnblogs.com/xdao/p/linux_gi ...

  5. 国内外做MySQL的公司

    MariaDB 基金会   Percona企业级服务   vividx      监控       webyog   监控和管理   springboksql   mysql 一体机      htt ...

  6. 常用SQL语句(交互)

    %-------通配符 select * from [EMoney_Club].[dbo].[GoldIdea_AdviceCollect] where [Content] like '%昵称%' s ...

  7. #Javascript:this用法整理

    常用Javascript的人都知道,[this這個關鍵字在一個函式內究竟指向誰]的這個問題很令人頭大,本人在這裡整理了一下Javascript中this的指向的五種不同情況,其中前三種屬於基本的情況, ...

  8. 5.6 WebDriver API实例讲解(16-30)

    16.操作单选框 被测试的网页为Demo1. Java语言版本的API实例代码: public static void operateRadio(){ driver.get("file:// ...

  9. ubuntu修改更新源为163

    安装ubuntu后为了使下载及更新软件包更快,需要将自带的源更换为国内的源.以下以163为例: 1.首先备份下原始自带源: sudo cp sources.list /etc/apt/sources. ...

  10. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...