题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=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 + 状态压缩)的更多相关文章

  1. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  2. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  4. 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 ...

  5. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  7. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  8. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  9. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

随机推荐

  1. 【POJ1733】【带标记并查集】Parity game

    Description Now and then you play the following game with your friend. Your friend writes down a seq ...

  2. jQuery--引入,基本语法,以及常用事件

    一.初识jQuery jQuery是一个JavaScript函数库.主要包含的功能有:HTML元素的选取.操作,CSS操作,HTML事件函数,JavaScript特效和动画,HTML DOM遍历和修改 ...

  3. WordPress批量修改文章内容、URL链接、文章摘要

    通过SQL语句来批量修改wordpress博客内容,文章中所有语句都使用默认的wp_表前缀,如果您的数据表前缀不是wp_则需要在语句中作相应更改. 方法/步骤   批量修改文章内容 如果您想替换之前写 ...

  4. DIV+CSS外部字体引用

    注意: 由于各个浏览器兼容问题大家还是少用这个,下面是具体的使用方法和效果截图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN&qu ...

  5. centos es2.x安装

    #把下面这个放到es的server路径下,这个是rpm安装改了下. # # init.d / servicectl compatibility (openSUSE) # if [ -f /etc/rc ...

  6. 转 一些shell经验

    http://www.cnblogs.com/xublogs/archive/2010/03/16/2292254.html http://www.cnblogs.com/stephen-liu74/ ...

  7. UpdateLayeredWindow是炫效果的关键

    自绘——是的,输入框每个字都自己绘制,计算行宽,行高,模拟光标闪烁,处理输入法的各种事件,以及选中,拖动等功能. 支持支持一下,实际上无句柄的,就是多行富文本编辑比较麻烦,其他的,都不复杂.很容易实现 ...

  8. c#语句 (随堂练习)

    1. 方程ax²+bx+c=0:一元二次方程.求根    输入a,b,c的值    Δ=b²-4ac:若Δ<0方程无实根    若Δ>0,方程有两个不相同的实根x1 x2gen    若Δ ...

  9. Pots(bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8266   Accepted: 3507   Special Judge D ...

  10. LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)

    一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...