题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=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. zTree异步生成数据时无法获取到子节点的选中状态

    最近在项目中遇到一个问题,需求如下: 根据选中不同的人员(ID)向后台发送ajax请求,通过返回的数据来生成该人员的权限访问树,该树目录最少为3级目录,在生成的时候会自动勾选上次保存过的选中状态,点击 ...

  2. JQuery解析XML数据的几个例子

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...

  3. 商务通代码API

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...

  4. java高精度数组

    POJ1205 递推公式为a[i] = 3*a[i-1] - a[i-2], a[1] = 1,a[2] = 3 , i 最高为100; 搞懂了使用BigInteger开数组. import java ...

  5. IOS--UISwitch的使用方法

    IOS--UISwitch的使用方法详细 (2013-08-24 11:09:38) 转载▼ 标签: uiswitch switch 选择控件 ios it 分类: iOS--UI // UISwit ...

  6. 大神眼中的React Native--备用

    当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...

  7. 董的博客 hadoop

    董的博客 https://issues.apache.org/jira/browse/MAPREDUCE 很重要,把MAPREDUCE改为YARN即可 直接下载patch即可 http://horto ...

  8. Web.config总结

    %windir%/Microsoft.NET/Framework/v2.0.50727/CONFIG目录下machine.config定义了针对当前机器的WinForm程序和asp.net应用程序的配 ...

  9. stm32 smartcard调试--不用st8024

    关于stm32 smartcard功能调试,官方提供的例程是配合8024芯片进行控制的.程序可从地址:http://www.pudn.com/downloads420/sourcecode/embed ...

  10. SpringMvc配置 导致实事务失效

    SpringMVC回归MVC本质,简简单单的Restful式函数,没有任何基类之后,应该是传统Request-Response框架中最好用的了. Tips 1.事务失效的惨案 Spring MVC最打 ...