UVA 321 The New Villa (隐式图+BFS +判重)

来源:互联网 发布:java tomcat 部署 编辑:程序博客网 时间:2024/06/11 13:39

 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.

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 rd, and sr 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 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个房间,d个房间连接方式,s个开关连接方式。。要求从1号房间走到r号房间。起初只有1号房间是亮着的。并且人走到的房间必须是亮着的。。

思路:BFS,用一个字符串记录状态。首字母对应所在房间。之后每一个字母为‘0’,‘1’对应房间的关灯开灯。

然后我用的是map判重。。直到状态为首字母为r房间。其余房间为关灯。r房间为开灯时结束。

#include <stdio.h>#include <string.h>#include <string>#include <queue>#include <map>using namespace std;int r, d, s;int judge;char end[20];int mapp[15][15], sw[15][15];struct QUE {    char status[20];    int step[10005];    int stepnum;    int room;} q, p;map<string, int> vis;queue<QUE> Q;void bfs() {    judge = 0;    vis.clear();    while (!Q.empty()) {Q.pop();}    q.room = 1;    q.status[0] = '1'; q.status[1] = '1';    for (int i = 2; i <= r; i ++) {q.status[i] = '0';}    q.status[r + 1] = '\0';    q.stepnum = 0;    vis[q.status] = 1;    Q.push(q);    while (!Q.empty()) {p = Q.front();if (strcmp(p.status, end) == 0) {    judge = 1;    return;}Q.pop();for (int i = 1; i <= r; i ++) {    q = p;    if (mapp[q.room][i] && q.status[i] == '1') {q.status[0] = i + '0';if (!vis[q.status]) {    vis[q.status] = 1;    q.room = i;    q.step[q.stepnum ++] = 3;    q.step[q.stepnum ++] = i;    Q.push(q);}    }    q = p;    if (sw[q.room][i] && i != q.room) {if (q.status[i] == '1') {    q.status[i] = '0';    q.step[q.stepnum ++] = 2;    q.step[q.stepnum ++] = i;}else {    q.status[i] = '1';    q.step[q.stepnum ++] = 1;    q.step[q.stepnum ++] = i;}if (!vis[q.status]) {    vis[q.status] = 1;    Q.push(q);}    }}    }}int main() {    int t = 1;    while (scanf("%d%d%d", &r, &d, &s) != EOF && (r || d || s)) {int a, b;memset(mapp, 0, sizeof(mapp));memset(sw, 0, sizeof(sw));end[0] = r + '0'; end[r] = '1'; end[r + 1] = '\0';for (int i = 1; i < r; i ++)    end[i] = '0';for (int i = 0; i < d; i ++) {    scanf("%d%d", &a, &b);    mapp[a][b] = mapp[b][a] = 1;}for (int i = 0; i < s; i ++) {    scanf("%d%d", &a, &b);    sw[a][b] = 1;}bfs();printf("Villa #%d\n", t ++);if (judge) {    printf("The problem can be solved in %d steps:\n", (p.stepnum + 1) / 2);    for (int i = 0; i < p.stepnum; i ++) {if (i % 2 == 0) {    if (p.step[i] == 1)printf("- Switch on light in room ");    if (p.step[i] == 2)printf("- Switch off light in room ");    if (p.step[i] == 3)printf("- Move to room ");}if (i % 2) {    printf("%d.\n", p.step[i]);}    }}else    printf("The problem cannot be solved.\n");printf("\n");    }    return 0;}



原创粉丝点击