POJ 1753
2015-06-10 16:43
246 查看
#include <iostream> #include <queue> using namespace std; char _m[4][4]; bool mark[100000]; struct node { int step; int state; }; bool BFS(node p); bool boo; node t; void move(int r)//位操作的改变状态~ { t.state^=(1<<(15-r)); if(r>=4) t.state^=(1<<(15-r+4)); if(r<=11) t.state^=(1<<(15-r-4)); if((r%4)) t.state^=(1<<(15-r+1)); if(((r+1)%4)) t.state^=(1<<(15-r-1)); } queue<node> coll; int main() { //freopen("acm.acm","r",stdin); int i; int j; node begin; begin.state = 0; begin.step = 0; for(i = 0; i < 4; ++ i) { for(j = 0; j < 4; ++ j) { cin>>_m[i][j]; if(_m[i][j] == 'b') { begin.state += 1; } begin.state <<= 1; } } begin.state >>= 1; memset(mark,false,sizeof(mark)); if(begin.state == 0 || begin.state == 65535) { cout<<"0"<<endl; } else { coll.push(begin); mark[begin.state] = true; boo = false; while(!coll.empty() && !BFS(coll.front())) { coll.pop(); } if(!boo) { cout<<"Impossible"<<endl; } } } bool BFS(node p) { // cout<<p.state<<endl; if(p.state == 0 || p.state == 65535) { boo = true; cout<<p.step<<endl; return true; } int i; for(i = 0; i < 16; ++ i) { t = p; move(i); // cout<<t.state<<endl; if(!mark[t.state]) { t.step = p.step + 1; coll.push(t); mark[t.state] = true; } } return false; }
相关文章推荐
- 计算机视觉杂谈
- 黑马程序员--冒泡排序法
- linux centOS安装proftpd
- MySQL 日志文件 说明
- NSDate如何获取一个月后的日期
- Threads, Events and QObjects
- hadoop2.x error记录
- CentOS7.0硬盘安装(双系统win8.1+CentOS7)遇到的问题及解决办法
- 排序算法之(5)——归并排序(递归实现)
- sqlserver 自增ID插入指定数据
- LeetCode 57:Insert Interval
- VirtualBox共享文件夹等高级特性
- Android基础教程(六)之----多选项CheckBox的综合应用
- POJ 1731
- POJ 1734
- POJ 1740
- 通过三个实例初步认识云计算
- JAVA 环境变量
- 各种各样的汽车神总结!简直太精辟了
- C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码