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把,所以把这个作为题目的突破口, ...
随机推荐
- gvim 常用命令
插入: insert 强退: :q! 退出: :q 保存: :w 保存退出::wq 复制: yy(单行) 多行:8yy 删除: dd(单行) 多行:8dd 或者 :4,8d 执行脚本: :! ...
- Mvvm绑定datagrid或listview的selectItems的方法[转]
单选,很简单,将SelectedItem与ViewModel的属性进行双向绑定就OK了 多选,由于ListView的SelectedItems不能进行绑定,需要将ListView的SelectionC ...
- centos postfix 邮箱安装记录
---恢复内容开始--- #wget http://nchc.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.9 ...
- PHP第一课笔记
打算以后学习PHP,花3个月时间学会它,自己为自己加油.每天坚持学习,第一天感觉良好,没开始写,所以不敢觉难,在难也学,加油,me!! PHP笔记记录(2014.7.27) ★web开发的介绍 1.动 ...
- python模块之socket
43.python模块之socket: Python在网络通讯方面功能强大,学习一下Socket通讯的基本方式 UDP通讯: Server: import socket port=8081 ...
- S5PV210开发板刷机(SD卡uboot、串口+USB-OTG刷机方法)
一.介绍 九鼎的S5PV210开发板,在出厂前已经默认刷了Android4.0系统.如果需要刷其它的系统或者是由于系统问题无法启动时,就需要对板子刷机. 其实,刷机是对210开发板的一个基础学习,目的 ...
- SharePoint 设置Lookup 字段的值
如何设置Lookup字段的值, 首先我们同样需要了解SPFieldLookupValueCollection和SPFieldLookupValue, 这2个类的原理和之前所讲解到SPFieldUser ...
- oracle 对象权限 系统权限 角色权限
系统权限: 允许用户执行特定的数据库动作,如创建表.创建索引.连接实例等 对象权限: 允许用户操纵一些特定的对象,如读取视图,可更新某些列.执行存储过程等 select * from user_sys ...
- MC中间件WCCS
一.问题描述 在大访问量的Web服务中,MC集群作为缓解后端数据源访问压力的中间层已经成为了不可缺少的一部分,机器数量越来越大,维护成本也变得越来越高了,其中的问题有: 故障机器自动剔除.后端某台MC ...
- Qt 文件处理(readLine可以读取char[],并且有qSetFieldWidth qSetPadChar 等全局函数)
Qt 文件处理 Qt提供了QFile类来进行文件处理,为了更方便地处理文本文件或二进制文件,Qt还提了QTextStream类和QDataStream类,处理临时文件可以使用QTemporaryFil ...