首页 技术 正文
技术 2022年11月14日
0 收藏 831 点赞 2,841 浏览 2233 个字

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

题目:

Flip Game

Description

Flip game is played on a rectangular 4×4 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).

POJ 1753 Flip Game(状态压缩+BFS)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数组标记当前棋盘情况是否出现过,未出现则将当前情况入队,反之则不入。

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

代码:
 #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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,114
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,586
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,432
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922