UVA - 321 The New Villa
来源:互联网 发布:软件测试报告模板 编辑:程序博客网 时间:2024/06/01 09:39
题目大意:别墅有 r 个房间,d 扇门, s 个开关,判断是否能从编号 1 的房间走到编号 r 的房间,若能打印步骤。两个房间之间有门的才可移动,在移动时注意不能进入没有亮灯的房间。
解题思路:二进制表示房间状态,bfs 查找递归输出。
#include<iostream> #include<cstdio>#include<cmath>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<map>using namespace std;int r, d, s;int cnt = 0;int vis[15][2000];int pre[15][2000];struct node { int step, now, state;};queue<node> q;int door[15][15], light[15][15];node now, nt;void bfs() { int n, sta, t; while (!q.empty() && !vis[r][1]) { now = q.front(); q.pop(); n = now.now; sta = now.state * 100 + n; nt.step = now.step + 1; for (int i = 1; i <= r; i++) { if (!light[n][i] || i == n) continue; t = now.state ^ (1 << (r-i)); if (vis[n][t]) continue; vis[n][t] = nt.step; pre[n][t] = sta; nt.state = t; nt.now = n; q.push(nt); } for (int i = 1; i <= r; i++) { if (!door[n][i]) continue; if (!((1<<(r-i)) & now.state) || vis[i][now.state]) continue; vis[i][now.state] = nt.step; pre[i][now.state] = sta; nt.state = now.state; nt.now = i; q.push(nt); } }}void output(int n, int sta) { int ln, ls, t = pre[n][sta]; if (!t) return; ln = t % 100; ls = t / 100; output(ln, ls); if(ln != n)printf("- Move to room %d.\n", n); else { int t=ls^sta, i, flag; for(i = 0; i < r&&!(t&1); i++, t>>=1); flag = ls&(1<<i); printf("- Switch %s light in room %d.\n",flag?"off":"on",r-i); } }int main() { while (scanf("%d%d%d", &r, &d, &s) != EOF && r+d+s) { memset(door, 0, sizeof(door)); memset(light, 0, sizeof(light)); memset(vis, 0, sizeof(vis)); int a, b; for (int i = 0; i < d; i++) { scanf("%d%d", &a, &b); door[a][b] = door[b][a] = 1; } for (int i = 0; i < s; i++) { scanf("%d%d", &a, &b); light[a][b] = 1; } while (!q.empty()) q.pop(); int i = 1 << (r - 1); vis[1][i] = 1; pre[1][i] = 0; now.state = i; now.now = 1; now.step = 1; q.push(now); printf("Villa #%d\n", ++cnt); bfs(); if(vis[r][1]) { printf("The problem can be solved in %d steps:\n",vis[r][1]-1); output(r,1); } else printf("The problem cannot be solved.\n"); printf("\n"); }return 0;}
0 0
- uva 321 - The New Villa
- UVa 321 - The New Villa
- UVA 321 - The New Villa
- uva 321 - The New Villa
- uva 321 The New Villa
- UVa 321 - The New Villa
- UVA - 321 The New Villa
- uva 321 - The New Villa bfs+递归
- uva:321 - The New Villa(bfs + 哈希判重)
- UVa 321 & ZOJ 1301 - The New Villa
- UVA - 321 The New Villa 隐式图搜索
- The New Villa (Uva 321 bfs)
- 321 - The New Villa
- UVA 321 - The New Villa BFS+位运算
- UVA 321 - The New Villa
- uva 321 The New Villa(隐式图搜索)
- UVA - 321 The New Villa(bfs+hash)
- uvaoj 321 - The New Villa
- C++11实现自旋锁
- 用javascript实现多表单提交同一信息
- 心好,一切都好
- eclipse:修改JVM运行内存
- Android 全局异常处理之UncaughtExceptionHandler
- UVA - 321 The New Villa
- Android网络操作和优化相关
- window.open不被拦截
- oracle中行转列语句参考
- 透明度和zoom属性
- 一些小知识点2
- 设计模式之观察者模式
- 互斥锁,自旋锁与自适应自旋锁
- tarjan相关算法 学习笔记