题目网址:http://poj.org/problem?id=1753

题目:

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: 
  1. Choose any one of the 16 pieces.
  2. 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

思路:

棋盘只有4*4即16格,我们把这16格的白棋黑棋状态分别用1,0表示,就可以用十进制的0——(2^16-1即65535)表示所有情况。利用0^1=1,1^1=0,0^0=0即无论0,1与0做^运算都等于其本身,无论0,1与1做^运算都等于另一个数的特性,我们将翻转棋子操作,变成将选定棋子和四周的棋子的状态^1,其余棋子状态^0。再用bfs进行搜索,用vis数组标记当前棋盘情况是否出现过,未出现则将当前情况入队,反之则不入。

代码:
 #include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int vis[];
int mp[]={//预处理,事先算出翻转16个棋子 分别对应的"^"操作数
,,,,
,,,,
,,,,
,,,
};
char chess[][];
vector<int>v;
queue<int>q;
int change(){//将棋盘的初始状态压缩
int x=v[];
for (int i=; i<v.size(); i++) {
x=(x<<)+v[i];
}
return x;
}
int bfs(int v){
vis[v]=;
q.push(v);
while (!q.empty()) {
int x=q.front();q.pop();
if(x== || x== ) return vis[x]-;//x==0时,棋子全为黑面朝上,x==65535时,棋子全为白面朝上。vis数组保存的是当前步数+1
for (int i=; i<; i++) {//分别翻转16个棋子
int xt=x^mp[i];
if(vis[xt]) continue;
vis[xt]=vis[x]+;
q.push(xt);
}
}
return -;
}
int main(){
for (int i=; i<; i++) {
gets(chess[i]);
for (int j=; j<; j++) {
if(chess[i][j]=='b') v.push_back();
else v.push_back();
}
}
int x=bfs(change());
if(x!=-) printf("%d\n",x);
else printf("Impossible\n");
return ;
}

POJ 1753 Flip Game(状态压缩+BFS)的更多相关文章

  1. POJ 1753 Flip Game 状态压缩,暴力 难度:1

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

  2. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  3. POJ 3411 Paid Roads (状态压缩+BFS)

    题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...

  4. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  5. 枚举 POJ 1753 Flip Game

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

  6. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. ExpandableListView之BaseExpandableListAdapter

    之前使用的SimpleExpandableListAdapter有较大局限性,样式单一,修改难度大,这里不建议使用,而是利用BaseExpandableListAdapter,其实SimpleExpa ...

  2. 【Unity与Android】01-Unity与Android交互通信的简易实现

    前言 使用Unity也有不短的时间了,安卓包也打过不少,但是对Unity与Android的交互却知之甚少. 因工作需求,需要在Android平台接一些sdk(扩展功能).我就借此机会就了解了下Unit ...

  3. .NetCore 网站DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法

    .netCore网站Delete请求405错误 解决方案 1:在启用或关闭Windows功能 –> Internet Information Services 关闭WebDAV发布(这种方式可能 ...

  4. [C++] C++中的宏定义详解

    转载自:C++中的宏定义 和 C++宏定义详解 一.#define解析     #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率 ...

  5. Mysql 笔记二

    Mysql 笔记二 Mysql 笔记二 Table of Contents 1. 前言 2. Master Thread 工作方式 2.1. 主循环(loop) 2.2. 后台循(backgroup ...

  6. 解决AndroidKiller APK 反编译失败,无法继续下一步源码反编译!

    报错背景 今天使用AndroidKiller V1.3.1,反编译一个APK,遇到如下报错: 当前 Apktool 使用版本:Android Killer Default APKTOOL 正在反编译 ...

  7. CentOS6.x环境通过yum命令在线安装或重装zookeeper-server

    一.环境描述: 在CentOS6.x系统环境下,使用yum命令的形式安装zookeeper-server,由于我这里是重新安装zookeeper-server,所以在正式开始之前我需要将原本的zook ...

  8. Spring boot 梳理 - @SpringBootConfiguration

    @SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类, 并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到sprin ...

  9. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多进程版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 import requests from lxml import etree from multiprocessing imp ...

  10. javascript中字符串对象常用的方法和属性

    前言 字符串是一种非常重要的数据类型,在Java等面向对象编程语言中,它代表对象类型,而在javascript中它却是一种基本数据类型,在开发的领域中,我们经常会碰到,无论是前端还是后台.比如后台验证 ...