The New Villa
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.The New Villa |
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 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
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 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.
shortest path + bitmask
#include<stdio.h>
#include<cstring>
#include<queue>
#include<vector>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAXN 10
#define DBG 0
using namespace std;
int N,M,cases=1;
int pre[MAXN][1024][2],child[MAXN][1024][2];
bool on[MAXN],isc[MAXN][MAXN],dps[MAXN][1024];
vector<int>eg[MAXN],ctl[MAXN],seq[MAXN];
struct Node{
Node(int a,int b,int c):status(a),v(b),steps(c){}
int status,v,steps;
};
void setSP(int &s,int pos,int v){
if(v)s|=1<<(N-1-pos); //set to 1
else s&=~(1<<(N-1-pos)); //set to 0
}
bool getSP(int s,int pos){
return s&1<<(N-1-pos);
}
void init(){
memset(isc,0,sizeof(isc));
rep(i,MAXN)seq[i].clear();
rep(i,N)rep(j,ctl[i].size())isc[i][ctl[i][j]]=1;
}
int getLight(int a,int b){
int c=a^b,d=0,e=0;
while(c>0){if(c%2)return N-1-e;e++,c/=2;}
}
void print(int s,int steps){
int c=N-1,pc=-1,c2,s2,d=steps,cnt=0,l;
bool used[MAXN],on[MAXN];
memset(on,0,sizeof(on));on[0]=1;
while(d){
if(DBG)printf("%d %d\n",c,s);
c2=pre[c][s][0],s2=pre[c][s][1];
child[c2][s2][0]=c,child[c2][s2][1]=s;
c=c2,s=s2,d--;
}
c=0,s=1<<N-1,d=steps-1;
printf("The problem can be solved in %d steps:\n",steps-1);
while(d){
if(DBG)printf("%d %d\n",c,s);
if(d<steps-1&&pc!=c)printf("- Move to room %d.\n",c+1);
c2=child[c][s][0],s2=child[c][s][1];
l=getLight(s,s2);
if(c==c2) //switch
if(!on[l])printf("- Switch on light in room %d.\n",l+1),on[l]=1;
else printf("- Switch off light in room %d.\n",l+1),on[l]=0;
pc=c,c=c2,s=s2,d--;
}
}
void ans(){
int a,b,c,d,e,t,s,dis=0;
bool ok=0;
queue<Node>q;
memset(dps,0,sizeof(dps));
t=1<<N-1; //turn on 1
q.push(Node(t,0,1));
while(q.size()){
b=q.front().v,t=q.front().status,dis=q.front().steps,q.pop();
if(DBG)printf("bfs %d %d %d\n",b,t,dis);
if(b==N-1&&t==1){if(DBG)printf("ok\n");ok=1;break;}
dps[b][t]=1;
rep(i,N){
s=t;
if(isc[b][i]){ //turn on/off
a=b;
setSP(s,i,1);
if((getSP(s,a)&&getSP(s,b))&&!dps[a][s]){ //two room must be turned on
dps[a][s]=1,pre[a][s][0]=b,pre[a][s][1]=t;
q.push(Node(s,a,dis+1));}
setSP(s,i,0);
if((getSP(s,a)&&getSP(s,b))&&!dps[a][s]){ //two room must be turned on
dps[a][s]=1,pre[a][s][0]=b,pre[a][s][1]=t;
q.push(Node(s,a,dis+1));}
}
}
s=t;
rep(j,eg[b].size()){ //change room
a=eg[b][j];
if((getSP(s,a)&&getSP(s,b))&&!dps[a][s]){
dps[a][s]=1,pre[a][s][0]=b,pre[a][s][1]=t;
q.push(Node(s,a,dis+1));}
}
}
printf("Villa #%d\n",cases++);
if(N==1)printf("The problem can be solved in 0 steps:\n");
else if(ok)print(t,dis);
else printf("The problem cannot be solved.\n");
}
int main(){
int a,b,c;
while(scanf("%d%d%d",&N,&M,&c)==3){
if(N+M+c==0)break;
rep(i,MAXN)eg[i].clear(),ctl[i].clear();
rep(i,N)eg[i].push_back(i); //self room
rep(i,M)scanf("%d%d",&a,&b),a--,b--,eg[a].push_back(b),eg[b].push_back(a);
rep(i,c)scanf("%d%d",&a,&b),a--,b--,ctl[a].push_back(b);
init();
ans();
printf("\n");
}
return 0;
}
沒有留言:
張貼留言