【BFS】The New Villa
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1481 | Accepted: 485 |
Description
One night, Mr. Black came home late. While standing in the hallway, he noted that the lights in all other rooms were switched off. Unfortunately, Mr. Black was afraid of the dark, so he never dared to enter a room that had its lights out and would never switch off the lights of the room he was in.
After some thought, Mr. Black was able to use the incorrectly wired light switches to his advantage. He managed to get to his bedroom and to switch off all lights except for the one in the bedroom.
You are to write a program that, given a description of a villa, determines how to get from the hallway to the bedroom if only the hallway light is initially switched on. You may never enter a dark room, and after the last move, all lights except for the one in the bedroom must be switched off. If there are several paths to the bedroom, you have to find the one which uses the smallest number of steps, where "move from one room to another", "switch on a light" and "switch off a light" each count as one step.
Input
This line is followed by d lines containing two integers i and j each, specifying that room i is connected to room j by a door. Then follow s lines containing two integers k and l each, indicating that there is a light switch in room k that controls the light in room l.
A blank line separates the villa description from the next one. The input file ends with a villa having r = d = s = 0, which should not be processed.
Output
If there is a solution to Mr. Black's problem, output the shortest possible sequence of steps that leads him to his bedroom and only leaves the bedroom light switched on. (Output only one shortest sequence if you find more than one.) Adhere to the output format shown in the sample below.
If there is no solution, output a line containing the statement `The problem cannot be solved.'
Output a blank line after each test case.
Sample Input
3 3 4
1 2
1 3
3 2
1 2
1 3
2 1
3 2 2 1 2
2 1
1 1
1 2 0 0 0
Sample Output
Villa #1
The problem can be solved in 6 steps:
- Switch on light in room 2.
- Switch on light in room 3.
- Move to room 2.
- Switch off light in room 1.
- Move to room 3.
- Switch off light in room 2. Villa #2
The problem cannot be solved.
Source
输入:
10 31 31
9 7
6 8
8 5
6 10
2 9
7 3
9 1
2 10
1 8
10 9
4 1
7 10
2 6
5 4
10 5
7 5
2 3
6 7
2 8
9 4
4 7
5 1
1 3
9 8
10 8
4 8
3 6
8 7
1 2
5 6
3 9
4 9
7 6
3 6
8 2
2 6
7 3
2 8
3 1
3 7
1 2
2 10
9 10
7 8
5 7
6 10
9 7
4 5
9 3
8 6
5 3
6 7
9 8
6 8
3 2
10 5
1 10
2 7
7 1
6 9
10 7
3 8
0 0 0
输出:
Villa #1
The problem can be solved in 12 steps:
- Switch on light in room 2.
- Move to room 2.
- Switch on light in room 7.
- Switch on light in room 8.
- Switch on light in room 10.
- Move to room 8.
- Switch off light in room 2.
- Move to room 7.
- Switch off light in room 1.
- Switch off light in room 8.
- Move to room 10.
- Switch off light in room 7.
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int R,D,S;//room door switch
bool vis[10024][13];
int a,b;
struct data{
int seq,st,ro;//状态,步数,现在所在的房间
bool flag;//0为走路,1为开关灯
int to;//链表的上一个,前驱
int turn;//记录当前开关了几号
}Que[100001];
int l=1,r=1;
bool alflag=false; bool print(data k){
if(k.to!=-1){//如果不是末尾
if(!print(Que[k.to])) return 1;//这是防止输出move to 1的
if(Que[k.to].flag==1){
if(Que[k.to].turn>0) printf("- Switch on light in room %d.\n",Que[k.to].turn);//注意on与off的区分输出
else printf("- Switch off light in room %d.\n",-Que[k.to].turn);
}
else{
printf("- Move to room %d.\n",Que[k.to].ro);
}
return 1;
}
else return 0;
}
vector<int> pat[13];
vector<int> swa[13];
int cnt; void BFS(){
Que[l].ro=1,Que[l].seq=2,Que[l].st=0;
Que[l].turn=0;
Que[l].to=-1;
if(Que[r].seq==(1<<R)&&Que[r].ro==R){//处理卧室就是走廊的
alflag=true;
printf("Villa #%d\n",cnt);
printf("The problem can be solved in %d steps:\n",Que[r].st);
return ;
}
int room,Seq,step;
while(l<=r){
step=Que[l].st,Seq=Que[l].seq,room=Que[l].ro;
for(int i=0;i<pat[room].size();i++){//走到另一个房间
if(((Seq>>pat[room][i])&1)==1&&!vis[Seq][pat[room][i]]){//如果此房间的灯是亮着的且此状态没有出现过
vis[Seq][pat[room][i]]=true;//标记此状态出现过
Que[++r].ro=pat[room][i];
Que[r].st=step+1;
Que[r].seq=Seq;
Que[r].to=l;
Que[r].flag=0;//flag=0表示走路
if(Que[r].seq==(1<<R)&&Que[r].ro==R){
alflag=true;
printf("Villa #%d\n",cnt);
printf("The problem can be solved in %d steps:\n",Que[r].st);
print(Que[r]);
printf("- Move to room %d.\n",Que[r].ro);
return ;
}
}
}
for(int i=0;i<swa[room].size();i++){//开/关灯
if(!vis[Seq^(1<<swa[room][i])][room]&&swa[room][i]!=room){//如果不是此房间的灯且此状态没有出现过的
vis[Seq^(1<<swa[room][i])][room]=true;
Que[++r].ro=room;
Que[r].st=step+1;
Que[r].seq=Seq^(1<<swa[room][i]);//更改灯亮的状态
Que[r].to=l;
Que[r].flag=1; //flag=1表示开关灯
if(!((Seq>>swa[room][i])&1)) Que[r].turn=swa[room][i];
else Que[r].turn=-swa[room][i];//负的代表turn off
if(Que[r].seq==(1<<R)&&Que[r].ro==R){
alflag=true;
printf("Villa #%d\n",cnt);
printf("The problem can be solved in %d steps:\n",Que[r].st);
print(Que[r]);//递归输出
if(Que[r].turn>0)
printf("- Switch on light in room %d.\n",Que[r].turn);
else printf("- Switch off light in room %d.\n",-Que[r].turn);
return ;
}
}
}
l++;//不要忘了这个
}
} int main(){
R=read(),D=read(),S=read();
while(R!=0||D!=0||S!=0){
cnt++;
l=1,r=1;
for(int i=1;i<=11;i++) pat[i].clear(),swa[i].clear();//注意清空
memset(vis,0,sizeof(vis));
for(int i=1;i<=D;i++){
a=read(),b=read();
pat[a].push_back(b);//路是双向的,可能走到下一个房间调了一下状态又回来了
pat[b].push_back(a);
}
for(int i=1;i<=R;i++) sort(pat[i].begin(),pat[i].end(),less<int>());//vector排序,因为没有spj
for(int i=1;i<=S;i++){
a=read(),b=read();
swa[a].push_back(b);
}
for(int i=1;i<=R;i++) sort(swa[i].begin(),swa[i].end(),less<int>());
BFS();
if(!alflag){
printf("Villa #%d\n",cnt);
puts("The problem cannot be solved.");
}
alflag=false;
printf("\n");
R=read(),D=read(),S=read();
}
return 0;
}
【BFS】The New Villa的更多相关文章
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【bfs】迷宫问题
[题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- 使用.net core abp framework
abp是一个有用的框架,包含许多功能,可以用来作为脚手架. 直接在官方网站上输入相应的工程名称,选择对应的版本就会下载对应的版本..net core 版本的可以使用后端框架部分来做api,包含了常用框 ...
- Bazinga(HDU5510+KMP)
t题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题目: 题意:找到一个编号最大的字符串满足:存在一个编号比它小的字符串不是它的字串. 思路:K ...
- vue 点击选中改变样式
data里isActive:-1,method里 checkedItem(index){ this.isActive=index;},页面里 <div v-for="(item,ind ...
- aircrack加reaver破解带有wps的wifi
最近心血来潮,想把小区里的无线信号测试个遍.基于目前大多数路由器都支持wps,想必各位基友们都知道aircrack和reaver这 两个工具,实属破解pin码,杀人越货,居家旅行之必备良药.像以前跑r ...
- java===java基础学习(14)---封装
package dog; public class Demo4 { public static void main(String []args) { Worker w1= new Worker(&qu ...
- iptables 操作
iptables --list 查看列表 iptables删除规则 iptables -nL --line-number Chain INPUT (policy ACCEPT)num target p ...
- 获取file中字段,写入到TXT文件中
一下代码省略了很多,哈哈哈 a.txt文件 uid,type,pointx,pointy,name1,9,911233763,543857286,区间测速起点3,9,906371086,5453354 ...
- [New learn]讲解Objective-c的block知识
1.简介 OC的Block感觉就是C中饿函数指针,提供回调功能,但是OC中的block比C的函数指针要更加强大,甚至可以访问本地变量和修改本地变量. block在oc中是一个对象,它可以像一般的对象那 ...
- echo常用操作
echo -n 不换行输出 [root@C ~]# echo -n "peter" ; echo "linux" peterlinux echo -e 输出转义 ...
- 《Java编程思想》阅读笔记二
Java编程思想 这是一个通过对<Java编程思想>(Think in java)进行阅读同时对java内容查漏补缺的系列.一些基础的知识不会被罗列出来,这里只会列出一些程序员经常会忽略或 ...