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. [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)

    本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...

  2. bootstrap 图片轮播效果

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="http: ...

  3. linux之od命令

    od [OPTION]... [FILE]...  把文件用8进制或者其他的格式显示出来.通常用于查看特殊格式文件的内容.  这个命令默认把文件的内容用八进制的形式清晰地写在标准输出上.如果是多个文件 ...

  4. JavaSE复习_7 异常

    △子父类涉及的异常问题:      1.子类在覆盖方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的子类,且只能抛出异常的子集      2.如果父类抛出了多个异常,子类只 ...

  5. unsigned 整型实现无溢出运算

    普通的 int 整型能表示的范围很有限,所以刷题时很多时候不得不用 long long 来存更大的数据.或者找出数列中某个只出现一次(或奇数次)的数(其余的数均出现两次 / 偶数次),用异或运算的经典 ...

  6. HashMap遍历

    package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...

  7. 转:The Knuth-Morris-Pratt Algorithm in my own words

    The Knuth-Morris-Pratt Algorithm in my own words For the past few days, I’ve been reading various ex ...

  8. Spring Boot修改内置Tomcat端口号 (zhuan)

    http://blog.csdn.net/argel_lj/article/details/49851625 ********************************************* ...

  9. Sqlserver_sql注入

    随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进 ...

  10. gcj_2016_Round1_B

    题目 一个NxN的矩阵,矩阵中每个方格中都有一个数值,且每一行的数值严格单调递增,每一列的数值严格单调递增.分别取出N行和N列,形成2N个长度为N的数组,现在有一个数组丢失,已知剩下的2N-1个长度为 ...