ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息。可是很不幸,他十分怕黑,因此他不会走入任何关着灯的房间,于是请你帮他找出一条路使他既能回到卧室又能关闭除卧室以外的所有灯。如果同时有好几条路线的话,请输出最短的路线。(ZOJ是special judge,poj不是)
写起来有些纠结(实力太弱)...........先对各个房间的灯进行操作,再考虑向其他房间走的问题。
由于要输出路径,所以添加前驱标识,递归输出即可。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int n,m,p;
int ed1[11][11],ed2[11][11];
int vis[11][1 << 10];
struct node {
int s,e,pre,op,step,buff;
void _init() {
pre = -1;
step = 0;
buff = 1;
op = -1;
}
} q[55555];
int head,tail;
void init() {
head = 0;
tail = 0;
memset(vis,0,sizeof(vis));
memset(ed1,0,sizeof(ed1));
memset(ed2,0,sizeof(ed2));
} int bfs(int v0) {
vis[v0][1] = 1;
q[0]._init();
q[0].s = 1;
head ++;
while(head != tail) {
node t = q[tail++];
node tt;
if(t.s == n && t.buff == (1 << (n-1))) { // find it
return tail - 1;
}
for(int i=1; i<=n; i++) {
int move = 1 << (i-1);
tt = t;
if(ed2[t.s][i] == 1 && i != t.s && vis[t.s][t.buff ^ move] == 0) { // 对房间的灯进行操作
vis[t.s][t.buff ^ move] = 1;
tt.buff = t.buff ^ move;
tt.step = t.step + 1;
tt.pre = tail - 1;
tt.e = i;
if(tt.buff & move) tt.op = 1; //开灯
else tt.op = 0; //关灯
q[head++] = tt;
}
}
for(int i=1; i<=n; i++) { //向其他房间前行
tt = t;
tt.step = t.step + 1;
if(ed1[t.s][i] == 1 && vis[i][t.buff] == 0 && (t.buff & (1 << (i-1)))) {
vis[i][t.buff] = 1;
tt.s = i;
tt.e = i;
tt.pre = tail - 1;
tt.op = 2; // 移动
q[head++] = tt;
}
}
}
return -1;
} void print(int v0) {
if(v0 == -1) return ;
print(q[v0].pre);
if(q[v0].op == 0) printf("- Switch off light in room %d.\n",q[v0].e);
if(q[v0].op == 1) printf("- Switch on light in room %d.\n",q[v0].e);
if(q[v0].op == 2) printf("- Move to room %d.\n",q[v0].e);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out2.txt","w",stdout);
#endif
int a,b;
int casee = 1;
while(scanf("%d%d%d",&n,&m,&p) != EOF) {
if(n == 0 && m == 0 && p == 0) break; //之前没看清题目,wa的无语了
init();
for(int i=0; i<m; i++) {
scanf("%d%d",&a,&b);
ed1[a][b] = 1;
ed1[b][a] = 1;
}
for(int i=0; i<p; i++) {
scanf("%d%d",&a,&b);
ed2[a][b] = 1;
}
printf("Villa #%d\n",casee ++);
int final = bfs(1);
if(final >= 0) {
printf("The problem can be solved in %d steps:\n",q[final].step);
print(final);
} else {
printf("The problem cannot be solved.\n");
}
puts("");
}
return 0;
}
ZOJ 1301 The New Villa (BFS + 状态压缩)的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
随机推荐
- Android JIN返回结构体
一.对应类型符号 Java 类型 符号 boolean Z byte B char C short S int I long J float ...
- 【POJ3580】【splay版】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- printf 格式化最常用用法
printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...
- 项目知识点.Part1
1. storyboard中添加scrollview: 先添加scrollView,进行约束 添加View 进行约束 相对于scrollView 如果水平滑动:设置vertically in Cont ...
- 在Linux下写一个线程池以及线程池的一些用法和注意点
-->线程池介绍(大部分来自网络) 在这个部分,详细的介绍一下线程池的作用以及它的技术背景以及他提供的一些服务等.大部分内容来自我日常生活中在网络中学习到的一些概念性的东西. -->代码 ...
- android开发学习笔记:圆角的Button
转自:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在res目录下的drawable-mdpi建立xml文件shape.x ...
- 发送邮件(E-mail)方法整理合集
在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...
- 使用date命令,进行时间戳和日期时间的互转
首先是知道时间转成时间戳 date -d "2014-01-16 12:30:11" +%s - :: - :: 其次是知道时间戳,想要知道当时的时间 date -d '1970- ...
- Windows环境下安装PHPUnit
Windows环境下安装PHPUnit,在此整理一下,以便大家参考. 本人测试安装环境:Windows7(win32) + Apache(2.2.13) + PHP(5.3.6) 1. 以管理员 ...
- Yaroslav and Divisors
Codeforces Round #182 (Div. 1) D:http://codeforces.com/contest/301/problem/D 题意:给一个1-n,n个数的序列,然后查询一个 ...