poj1753--flipgame---位运算 + bfs

来源:互联网 发布:php pack 字符串 编辑:程序博客网 时间:2024/05/22 11:30

Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 27064 Accepted: 11725

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

bwwbbbwbbwwbbwww

Sample Output

4

思路:位运算 + bfs。用0表示'w', 用1表示'b',把十六个棋子的状态用十六位二进制数表示,用位运算移位操作完成从地图到十六位二进制数的转换。

状态转换,用位运算异或操作实现(1与任何数异或得相反数,0与任何数异或得本身)---比如翻第一个棋子时,怎么实现翻呢?

翻第一个,要使1左移15位,那么第一个位置变为1,其余全为0,这个状态和棋盘当前状态异或,只有第一个棋子翻转,而要求把它周围四个位置也翻转,故还要把周围四个位置用相同的移位操作表示出来,它周围四个状态变为1,其余全为0,

然后和当前棋盘异或,即实现了翻第一个棋子。往后依此类推。。代码中的change数组就表示每翻一次的十六种翻法,转换成了十进制数。故每次状态转换,只需将这

十六种状态和当前棋盘异或,就得到了十六种翻转之后的状态。

得到change数组的代码:

#include<stdio.h>int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};int main(){int i, j, k, temp, x, y;for(i = 0; i < 4; i++){for(j = 0; j < 4; j++){temp = 0;temp ^= (1 << (15 - (4 * i + j)));for(k = 0; k < 4; k++){x = i + dir[k][0];y = j + dir[k][1];if(x >= 0 && x < 4 && y >= 0 && y < 4){temp ^= (1 << (15 - (4 * x + y)));printf("%d\n", temp);}}}}return 0;}主程序代码:#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;int change[16] = {51200, 58368, 29184, 12544,35968, 20032, 10016, 4880,2248,  1252,  626,   305,140,   78,    39,    19}, record[65536];typedef struct node{int time;//当前棋盘是翻了多少次得到的int state;///用一个十进制数表示一个十六位二进制数,即棋盘当前摆放状态}node;int bfs(int s){int i;queue <node> Q;node l, p, q;l.time = 0;l.state = s;record[s] = 1;Q.push(l);while(!Q.empty()){p = Q.front();Q.pop();if(p.state == 0 || p.state == 0xffff)//当到达全黑或全白时,返回return p.time;for(i = 0; i < 16; i++){q.state = p.state ^ change[i];if(!record[q.state]){//若翻转后的状态没存在过,入队q.time = p.time + 1;record[q.state] = 1;Q.push(q);}}}return -1;}int main(){int i, j, s, ans;char map[5][5];for(i = 0; i < 4; i++){scanf("%s", map[i]);}s = 0;for(i = 0; i < 4; i++){//移位实现把棋盘转换成数for(j = 0; j < 4; j++){s <<= 1;if(map[i][j] == 'b')s += 1;}}ans = bfs(s);if(ans >= 0)printf("%d\n", ans);elseprintf("Impossible\n");return 0;}


0 0