POJ1753 搜索
2018-06-17 23:22:23来源:未知 阅读 ()
Flip Game
Description
- 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
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
wwww wwww
wwww bwww wwbw bbbw wwww bwww
目标数字为0或65535.
#include "cstdio" #include "algorithm" #include "queue" #include "cmath" #include "cstring" #define inf 0x3f3f3f using namespace std; int g[16]; char s[4][4]; bool use[65536]; typedef struct { int st,s; }N; int bfs(int s){ queue<N>q; N no,ne; memset(use, false, sizeof(use)); no.st=s,no.s=0; q.push(no); use[s]=true; while (q.size()){ no=q.front(); q.pop(); if(no.st==0||no.st==65535){ return no.s; } for(int i=0;i<16;i++){ ne.st=no.st^g[i]; ne.s=no.s+1; if(use[ne.st]){ continue; } if(ne.st==0||ne.st==65535){ return ne.s; } use[ne.st]= true; q.push(ne); } } return -1; } int main() { memset(g,0, sizeof(g)); for(int i=0;i<16;i++){//存储16个翻子操作 int n=15-i; g[i]=pow(2,n); if(n+1<=n/4*4+3){ g[i]+=pow(2,n+1); } if(n-1>=n/4*4){ g[i]+=pow(2,n-1); } if(n+4<=15){ g[i]+=pow(2,n+4); } if(n-4>=0){ g[i]+=pow(2,n-4); } } while (scanf("%s",s[0])!=EOF){ for(int i=1;i<4;i++){ scanf("%s",s[i]); } int st=0; for(int i=0;i<4;i++){//将初始状态转为数字 for(int j=0;j<4;j++){ st<<=1; if(s[i][j]=='b'){ st++; } } } int x=bfs(st); if(x==-1){ printf("Impossible\n"); }else{ printf("%d\n",x); } } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 二叉搜索树_BST 2020-05-30
- AtCoder Grand Contest 043--A - Range Flip Find Route 2020-03-22
- 二叉搜索树3 2020-02-06
- 数据结构---二叉搜索树 2020-02-06
- BFS和队列 2020-01-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash