The New Villa (Uva 321 bfs)
来源:互联网 发布:历史书推荐 知乎 编辑:程序博客网 时间:2024/05/21 03:26
Description
Mr. Black recently bought a villa in the countryside. Only one thing bothers him: although there are light switches in most rooms, the lights they control are often in other rooms than the switches themselves. While his estate agent saw this as a feature, Mr. Black has come to believe that the electricians were a bit absent-minded (to put it mildly) when they connected the switches to the outlets.
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
The input file contains several villa descriptions. Each villa starts with a line containing three integers r, d, and s. r is the number of rooms in the villa, which will be at most 10. d is the number of doors/connections between the rooms and s is the number of light switches in the villa. The rooms are numbered from 1 to r; room number 1 is the hallway, room number r is the bedroom.
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 slines 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
For each villa, first output the number of the test case (`Villa #1', `Villa #2', etc.) in a line of its own.
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 41 21 33 21 21 32 13 22 1 22 11 11 20 0 0
Sample Output
Villa #1The 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 #2The problem cannot be solved.
题意:有r个房间,一个人开始在1号房间并且1号房间灯亮着其他熄灭,现在要去r号房间,告诉d对房间之间有门,s个开关,每个房间i内开关控制的不是本房间的灯,而是另外一个房间j的灯,人每次可以开或关自己房间的开关,或移动到亮着的房间,不是亮着的不能进去,问最终到r号房间最少需要几个步骤,输出步骤。到r号房间后r号房间亮着其他熄灭。
思路:把r个房间灯的状态用01串表示,1表示亮着,0表示熄灭,人当前在的房间为2,这样初始状态为2000000,终态为0000002,map判重,bfs即可。
代码:
#include <iostream>#include <functional>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 2005;const int MAXM = 200010;const int N = 1005;struct Edge{ int u,v,next;}edge[MAXN];struct Node{ int light[11]; int on,off,pos,step,pre;}que[MAXM];Node st,now;map<string,int>Hash;int r,d,s;vector<int>swi[11];int head[15],num,flag=0;void init(){ num=0; memset(head,-1,sizeof(head));}void addedge(int u,int v){ edge[num].u=u; edge[num].v=v; edge[num].next=head[u]; head[u]=num++;}bool isok(int a[]){ for (int i=1;i<r;i++) if (a[i]!=0) return false; if (a[r]!=2) return false; return true;}string change(int a[]){ string s=""; for (int i=1;i<=r;i++) s=s+(char)(a[i]-'0'); return s;}void out(int pos){ if (que[pos].pre==-1) return ; out(que[pos].pre); if (que[pos].on) printf("- Switch on light in room %d.\n",que[pos].on); else if (que[pos].off) printf("- Switch off light in room %d.\n",que[pos].off); else printf("- Move to room %d.\n",que[pos].pos); return ;}void bfs(){ string s; Hash.clear(); st.pre=-1; st.step=0; st.pos=1; st.on=st.off=0; Hash[change(st.light)]=1; int rear=0,front=0; que[rear++]=st; while (front<rear) { st=que[front];// for (int i=1;i<=r;i++)// printf("%d ",st.light[i]);// printf("\n"); if (isok(st.light)) { flag=1; printf("The problem can be solved in %d steps:\n",st.step); out(front); return ; } for (int i=0;i<swi[st.pos].size();i++) //开关灯 { int room=swi[st.pos][i]; now=st; now.on=now.off=0; if (now.light[room]==0) //开 { now.light[room]=1; now.on=room; s=change(now.light); if (!Hash[s]) { Hash[s]=1; now.pre=front; now.step=st.step+1; que[rear++]=now; } } else //关 { now.light[room]=0; now.off=room; s=change(now.light); if (!Hash[s]) { Hash[s]=1; now.pre=front; now.step=st.step+1; que[rear++]=now; } } } for (int i=head[st.pos];~i;i=edge[i].next) //移动 { int v=edge[i].v; now=st; now.on=now.off=0; if (now.light[v]==1) { now.light[st.pos]=1; now.light[v]=2; s=change(now.light); if (!Hash[s]) { Hash[s]=1; now.pre=front; now.pos=v; now.step=st.step+1; que[rear++]=now; } } } front++; } return ;}int main(){//#ifndef ONLINE_JUDGE// freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);//#endif int i,j,u,v,cas=0; while (scanf("%d%d%d",&r,&d,&s)) { if (r==0&&d==0&&s==0) break; init(); flag=0; for (i=0;i<d;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } for (i=0;i<11;i++) swi[i].clear(); for (i=0;i<s;i++) { scanf("%d%d",&u,&v); swi[u].push_back(v); } memset(st.light,0,sizeof(st.light)); st.light[1]=2; printf("Villa #%d\n",++cas); bfs(); if (!flag) printf("The problem cannot be solved.\n"); printf("\n"); } return 0;}
- The New Villa (Uva 321 bfs)
- uva 321 - The New Villa bfs+递归
- uva:321 - The New Villa(bfs + 哈希判重)
- UVA - 321 The New Villa(bfs+hash)
- UVA 321 - The New Villa BFS+位运算
- uva 321 - The New Villa
- UVa 321 - The New Villa
- UVA 321 - The New Villa
- uva 321 - The New Villa
- uva 321 The New Villa
- UVa 321 - The New Villa
- UVA - 321 The New Villa
- UVa 321 The New Villa ( BFS + 状态压缩判重)
- UVA 321 The New Villa (隐式图+BFS +判重)
- uva 321 The New Villa(隐式图搜索)
- UVa 321 & ZOJ 1301 - The New Villa
- UVA - 321 The New Villa 隐式图搜索
- 321 - The New Villa
- 网络编程TCP(三)
- 达内学习日志Day06:方法
- linux下tar命令解压到指定的目录
- linux 修改时间和日期date
- 牛人林达华推荐有关机器学习的数学书籍
- The New Villa (Uva 321 bfs)
- 可变参数使用
- hdu 5391 Zball in Tina Town
- C语言内存分布图
- Android Studio系列教程三--快捷键
- random7->random10
- 2015年面试准备(1)-----c/c++的区别
- docker管理的命令(1)
- 浅谈聚类&层次聚类