博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1753 Flip Game
阅读量:5125 次
发布时间:2019-06-13

本文共 2451 字,大约阅读时间需要 8 分钟。

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
#include 
#include
#include
char map[6][6],copy[6][6],str[20]; const int INF=666; int ans; int flag[6][6]; bool Check(char copy[][6]){ //检查所有棋子是不是都是一样的颜色 char ch=copy[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(copy[i][j]!=ch) return false; return true; } void Change(int i,int j){ //棋子颜色的翻转 copy[i][j]=(char)('b'+'w'-copy[i][j]); if(i!=0) copy[i-1][j]=(char)('b'+'w'-copy[i-1][j]); if(j!=0) copy[i][j-1]=(char)('b'+'w'-copy[i][j-1]); if(i!=3) copy[i+1][j]=(char)('b'+'w'-copy[i+1][j]); if(j!=3) copy[i][j+1]=(char)('b'+'w'-copy[i][j+1]); } void BFS(){ //暴力搜索加位运算 if(Check(map))//若是本身就已经是满足的情况,就不必翻转 ans=0; else for(int t=1;t<65536;t++){ //枚举所有的翻转情况,并且分析 int num=t; int step=0; for(int coi=0;coi<4;coi++) for(int coj=0;coj<4;coj++) copy[coi][coj]=map[coi][coj];//复制棋子图 for(int i=

转载于:https://www.cnblogs.com/huaixiaohai2015/p/5168698.html

你可能感兴趣的文章
java笔试题
查看>>
mysql 行转列
查看>>
web路径
查看>>
sql
查看>>
mysql 字符串截取
查看>>
sql if else
查看>>
数据库的忘记root密码和导出数据库
查看>>
销售订单计算交期
查看>>
金蝶CLOUD消息队列服务
查看>>
WMS出库单重复
查看>>
自定义字段从BOM带入生产用料清单
查看>>
生产用料清单-在制材料数量
查看>>
物料尾数进位 0.0000001
查看>>
华为鸿蒙系统pk安卓系统
查看>>
采购价目表区间报价
查看>>
SQL Server查看所有表大小,所占空间
查看>>
整单折扣计算
查看>>
K3CLOUD日志目录
查看>>
辅助单位启用
查看>>
付款申请金额对不上的问题
查看>>