POJ 1753一道状态压缩题。
来源:互联网 发布:tmplate可以写js 编辑:程序博客网 时间:2024/05/29 15:04
这道题让我知道了算法的美丽之处。
感谢:http://blog.csdn.net/ccDLlyy/article/details/55677065?locationNum=4&fps=1位运算啊。。。总之说就是用一个 数表示一个棋盘的状态。 黑色为1白色为0;
然后进行BFS,别忘了讨论界限的情况,取反然后用book数组记录下来。就可以了
#include <iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct node{ int date; int step;};bool book[65536],flag=0;int bfs(int st){ memset(book,0,sizeof(book)); struct node head,tail; head.date=st; head.step=0; book[st]=1; queue<node> que; que.push(head); while(!que.empty()) { head=que.front(); que.pop(); if(head.date==0||head.date==65535) { printf("%d\n",head.step); return 1; } for(int i=0;i<16;i++) { tail.date=head.date^(1<<i); if(i%4) { tail.date=tail.date^(1<<(i-1)); } if(i>3) { tail.date=tail.date^(1<<(i-4)); } if(i<12) { tail.date=tail.date^(1<<(i+4)); } if((i+1)%4) { tail.date=tail.date^(1<<(i+1)); } if(!book[tail.date]) { tail.step=head.step+1; book[tail.date]=1; que.push(tail); } } } return 0;}int main(){ int st=0; for(int i=0;i<16;i++) { char c; scanf(" %c",&c); if(c=='b') { st+=1<<i; } } flag=bfs(st); if(flag==0) printf("Impossible\n"); return 0;}
一定要复习一下位运算,太神奇。
0 0
- POJ 1753一道状态压缩题。
- 一道状态压缩DP思想题
- poj 1753状态压缩水题一发
- POJ-1753-BFS+状态压缩
- poj 1753 状态压缩+bfs
- 第一道 状态压缩dp
- poj 1753 Flip Game 二进制状态压缩
- POJ 1753 Flip Game 状态压缩 枚举
- Poj 1753 Flip Game 状态压缩 + DFS
- POJ 1753 Flip Game(状态压缩)
- 【BFS+状态压缩】-POJ-1753-Flip Game
- POJ 1753 Flip Game(状态压缩BFS)
- POJ 1753 Flip Game(BFS+状态压缩)
- poj 1753 Flip Game 状态压缩
- poj 1753 Flip Game(状态压缩枚举)
- POJ 1753(用到了状态压缩)
- POJ 3254 状态压缩DP简单题
- POJ 3311 状态压缩DP简单题
- 基于Win7、Python2.7安装一系列机器学习工具
- .NET中使用Redis (二)
- b-tree、b+-tree、2-3-4树个人总结
- Oracle 高级查询
- Vue-组件props属性再回顾
- POJ 1753一道状态压缩题。
- seleniumWebDriver的API操作杀进程、截屏、执行js脚本_10_4
- 71:Construct Binary Tree from Preorder and Inorder Traversal
- RxAndroid学习之旅
- 微信公众平台开发-access_token获取及应用(含源码)
- 简单选择排序
- 【Logistic回归】原理及Python代码示例
- JavaScricp简介
- spring data 接口之 CrudRepository