Hdu-1885-Key Task [状态压缩][bfs]
来源:互联网 发布:比较有名的网络女歌手 编辑:程序博客网 时间:2024/05/16 09:29
题目传送门
只有四把钥匙, 使用2^4中状态可以表示,碰到门的时候,跟当前的key与一下就知道是否可以开门, 剩下的就是个简单的bfs, 注意不止有一个出口。
PS:代码写得真的挫。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>#include <queue>using namespace std;struct node{ int x, y,step; int key;};int vis[110][110][20]; // b 1 y 2 r 4 g 8char Map[110][110];int N[4][2]= {1,0,-1,0,0,1,0,-1};int R,C,ex,ey;void bfs(int sx,int sy){ queue<struct node>q; struct node a; a.x=sx; a.y=sy; a.step=0; q.push(a); int i = 0; while (!q.empty()) { a = q.front(); q.pop(); for (int i = 0; i < 4; i++) { struct node b; b.x = a.x + N[i][0]; b.y = a.y + N[i][1]; b.key = a.key; if (b.x<0||b.x>=R||b.y<0||b.y>=C) continue; if (Map[b.x][b.y]=='#') continue; b.step = a.step + 1; if (Map[b.x][b.y]=='X') { printf("Escape possible in %d steps.\n", b.step); return; } if (Map[b.x][b.y]=='B') { int nk = a.key&1; if (nk&&!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='b') { b.key = a.key|1; if (!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='Y') { int nk = a.key&2; if (nk&&!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='y') { b.key = a.key|2; if (!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='R') { int nk = a.key&4; if (nk&&!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='r') { b.key = a.key|4; if (!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='G') { int nk = a.key&8; if (nk&&!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else if (Map[b.x][b.y]=='g') { b.key = a.key|8; if (!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } else { if (!vis[b.x][b.y][b.key]) { vis[b.x][b.y][b.key]=1; q.push(b); } } } } printf("The poor student is trapped!\n"); return;}int main(void){ while (scanf("%d %d", &R, &C)) { int sx, sy; if (R==0&&C==0) break; memset(vis,0,sizeof(vis)); for (int i = 0; i < R; i++) { scanf(" %s", Map[i]); } for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { if (Map[i][j]=='*') { sx=i; sy=j; Map[i][j]='.'; } } } bfs(sx,sy); } return 0;}
0 0
- BFS+状态压缩 hdu-1885-Key Task
- HDU 1885Key Task(bfs + 状态压缩)
- hdu 1885 Key Task bfs + 状态压缩
- Hdu-1885-Key Task [状态压缩][bfs]
- HDU 1885 Key Task 状态压缩+BFS
- HDU 1885 Key Task(状态压缩+BFS)
- hdu 1885 Key Task(bfs+状态压缩)
- hdu 1885 Key Task(状态压缩+bfs)
- hdu 1885 Key Task(bfs+状态压缩)
- hdu 1885 Key Task(bfs+状态压缩)
- HDU 1885 Key Task (状态压缩+BFS)
- hdoj 1885 Key Task 【BFS+状态压缩】
- HDU 1885 Key Task 状态压缩+搜索
- HDU1429胜利大逃亡(续)&&HDU 1885 Key Task BFS+状态压缩+水
- hdu1885 Key Task (bfs + 状态压缩)
- hdu1885 Key Task(BFS+状态压缩)
- HDU1885 Key Task(BFS+状态压缩)
- hdu 1885 Key Task(bfs)
- webpack学习笔记(代码分割,按需加载)
- POJ2762-Going from u to v or from v to u?(有向图强连通分量缩点)
- 2010 水仙花数
- 如何监听Android手机物理返回键
- Git 使用备忘
- Hdu-1885-Key Task [状态压缩][bfs]
- js数组中出现次数最多和第二多的元素
- python ----pyCharm(IDE)------2017.1安装
- 自定义UICollectionViewController之后如何设置布局方式
- 读取本地json数据实现省市区三级联动PickerView
- 移动支付
- iOS Bundle (1)
- 从UDP的”连接性”说起–告知你不为人知的UDP
- Android进程保活的一般套路