poj 1753
来源:互联网 发布:淘宝app首页流量来源 编辑:程序博客网 时间:2024/06/02 02:23
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 45559 Accepted: 19519
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:
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.
- 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
bwwbbbwbbwwbbwww
Sample Output
4
题意:给你16个棋子,b代表黑色,w代表白色。这些棋子按照 变换一粒颜色,那么那粒棋子的前后左右都要变换棋子颜色的规则变化;
输入一组数据 问 最少要经过多少步可以将他们都全部变为黑色或者白色;
思路:这里用的是状态压缩,压缩到一个int变量。我们将w看成1,b看成0,那么一共有16位0或1;所以整个棋盘表示的状态一共有2^16次方种状态。知道^(异或的规则是相同为0,不同为1) 所以 1^1=0,1^0=1,在这里刚好起到一个取反的作用,刚好对应棋盘的变化。
我们用整形的低16位表示棋盘的状态。| 1 按位或的作用是给某位 置1 例如 x|1<<16位,这里就是将2进制x的第16位置1, & 0 给某位置0。&也是同样。
所以开始 我们计算用 |将棋盘的初始状态保存到一个变量cnt里。然后在对他进行操作。 棋盘有16个棋子所以我们能对他进行16种操作,然后广度优先搜索下去,就ok。我用yh数组保存了 16种操作;
具体看代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;struct node{ int step; int k;};int yh[16]={51200,58368,29184,12544,35968,20032,10016,4880,2248,1252,626,305,140,78,39,19,};char map[5][5];bool vis[1<<16];int cnt;int ans;void bfs(){ queue<node>q; node a={0,cnt}; vis[cnt]=1; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.k==0||a.k==(1<<16)-1) { ans=a.step; return ; } for(int i=0;i<=15;i++) { node b=a; b.step=a.step+1; b.k=b.k^yh[i]; if(vis[b.k]) continue; q.push(b); vis[b.k]=1; } } ans=-1;}int main(){ cnt=0; int count=15; while(scanf("%d",map[0])!=EOF) { for(int i=0;i<4;i++) { scanf("%s",map[i]); for(int j=0;j<4;j++) { if(map[i][j]=='w') { cnt=cnt|1<<count; } count--; } } bfs(); if(ans!=-1) printf("%d\n",ans); else printf("Impossible\n"); }}
阅读全文
0 0
- poj 3279 poj 1753
- POJ 1753
- poj&&1753
- poj 1753
- poj 1753
- POJ-1753
- poj-1753
- poj 1753
- poj 1753
- poj 1753
- POJ 1753
- POJ 1753
- poj 1753
- poj-1753
- POJ 1753
- POJ 1753
- POJ 1753
- poj 1753
- centos 6.8 安装cmake 2.8.8 报错
- 人工智能: 自动寻路算法实现(二、深度优先搜索)
- 条件查询的时候,查询完成之后,不用在将选择的数据回传到页面,用于判断,让选择的option处于SELECT状态。
- leetcode 541. Reverse String II
- opencv图像处理之在手机上实现背景虚化
- poj 1753
- parse_url的用法,以及用途
- arcgis for js动态加载图层方法
- Java 相关问题汇总
- SpringMVC框架介绍
- Redundant Paths(POJ3177)
- 前后端分离springmvc和RESTful理解
- source insight设置Courier new字体
- PG锁浅析