Va 321 the new villa, 2B youth angry looking for a bedroom

Source: Internet
Author: User

Question link:

Ultraviolet A: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 257

Poj 1137: http://poj.org/problem? Id = 1137

Zoj 1301: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 1301.

Type: Implicit Graph Search


Original question:

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 specified icians 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 wocould 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 before t 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 should t 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
Light ''and'' switch off a light ''' each count as one step.

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.

Question:

A young man of 2B made money, so he happily went to buy a villa, but found that the electric circuits of the villa were all wrong. The electric switches in each room did not control the electric lights in this room, it is the light of another room ~~ 2b young people were very angry and had serious consequences, so they severely condemned the decoration workers.

One night, young man 2B returned home and stood in the hall. All the lights except the room in the hall were destroyed. 2b young people are afraid of going into the room without lights. So the question is, how should he go from the hall to his bedroom?

Young man 2B was still very smart. He found that he could use the electric switch to catch the wrong light and turn on the light in another room. Then he could enter the room where the light was turned on (young man 2B would not wear the wall, so the two rooms must be adjacent and have doors ).

2B is also a good young man who saves electricity, so when he goes to the bedroom, the other rooms must be wiped out.

Now, when you show your skills, write a program to help 2B young people find a solution with the least steps and output it. Turning on the light and turning off the light is a step towards entering another room. If no solution can be found, the young man 2B will have to go to the sofa hall tonight...


Analysis and Summary:


Recently I have made a lot of questions of this type, and also formed a basic process of thinking about searching questions by myself. Below is a small self-summary:


1. first, you need to find a "status", which can be considered as a global map. In this question, the status indicates the switch-on and switch-on status of each room, and the room in which young people are currently standing. 0 indicates turning off the light, 1 indicates turning on the light, and 2 indicates the position of the 2B youth station (of course, the light must be on ).


2. Then, find the "Action" for "state transfer". There are three actions that will lead to State Transfer: 1) Turn on the light; 2) Turn off the light; 3) walk into another room. That is to say, any action will change the current status.


3. Search. In the search function, these actions are performed to generate a new State, and then the new State is pushed to the queue or stack (BFS or DFS ), then a new State is generated for the new State ...... Generally, there is a "target state". When you perform an action from the initial State until the state is changed to the target State, the search is complete.


4. duplicate status issues, that is, heavy judgment issues. When a new status has been created before, the status will not be expanded. At the beginning of BFS and DFS, there is usually a vis array to mark the passed status. If the status is large, it is difficult to directly open an array to do so, and the hash table must be used for weight determination, encoding or using map and set in STL, the efficiency of using map and set is relatively low, which often leads to timeout. The first two are more commonly used.



I have been wa for several hours, but I still cannot find the cause. Finally, I felt that my logic was correct. So I will try again... Find ....

I finally found the cause: a common sense error!

When saving the path, the output result of the path is saved in the path string array, and the output result is to be output:

Switch ...... Room number, move ...... Room number.

The problem lies in the room number. Because the int number needs to be converted to the char string type, I often do this: Str [0] = V + '0 ', V indicates the room number. If the value of V is smaller than 10, it is okay. If the value of V is greater than or equal to 10, the result will be a character that xxx does not know.

That's why one afternoon is gone...

I will remember this lesson !!

/** Ultraviolet A 321 the new villa * Implicit Graph Search, hash weight * Time: 0.024 S (ultraviolet A), 10 ms (zoj) * Author: d_double **/# include <cstdio> # include <cstring> # define maxn 50000 typedef int State [11]; State que [maxn]; int nroom, ndoor, nswitch, G [20] [20], control [20] [20]; int step [maxn], Father [maxn], ans; // Number of stored steps, father: Char action [3] [35] = {"-move to room", "-switch on light in room", "-switch off light in Room "}; char path [maxn] [35 ]; // Save path const int hashsize = 2000000; // hash table size int head [hashsize], next [maxn]; inline int bkdrhash (State & S) {// hash function int seed = 131; // 31 131 1313 13131 131313 Etc .. int hash = 0; For (INT I = 1; I <= nroom; ++ I) hash = hash * seed + s [I]; Return (hash & 0x7fffffff) % hashsize;} inline int try_to_insert (INT s) {int H = bkdrhash (que [s]); int u = head [H]; while (u) {If (memcmp (que [u], que [s], sizeof (que [S]) = 0) return 0; u = next [u];} next [s] = head [H]; head [H] = s; return 1;} inline bool isok (State & S) {// determine whether the target State is reached for (INT I = 1; I <nroom; ++ I) if (s [I]) return false; If (s [nroom] = 2) return true; return false;} // The executed action inline bool run (State & S, int U, int V, int d) {Switch (d) {Case 0: // enter the room if (G [u] [v] & S [v]) {s [v] = 2; s [u] = 1; return true;} return false; Case 1: // turn on the light if (control [u] [v] &! S [v]) {s [v] = 1; return true;} return false; Case 2: // turn off the light if (control [u] [v] & S [v]) {s [v] = 0; return true;} return false ;}} inline void Init () {memset (Head, 0, sizeof (head); memset (que [0], 0, sizeof (que [0]); que [0] [1] = 2; // 1 indicates that the light is on, 0 indicates that the light is not on, and 2 indicates the person's position (the light is on) Step [0] = 0; father [0] =-1;} void BFS () {Init (); int front = 0, rear = 1; while (front <rear) {State & s = que [Front]; If (isok (s) {ans = front; Return ;} Int U; // obtain the current room for (INT I = 1; I <= nroom; ++ I) if (s [I] = 2) {u = I; break;} For (INT v = 1; v <= nroom; ++ v) if (u! = V) {for (Int J = 0; j <3; ++ J) {State & t = que [rear]; memcpy (t, s, sizeof (s); If (run (t, u, v, j) {If (try_to_insert (rear) {FATHER [rear] = front; step [rear] = step [Front] + 1; char STR [4]; // save path. Note that when the room number is greater than 9, you cannot directly use V + '0' if (v <10) {STR [0] = V + '0', STR [1] = '. ', STR [2] =' \ 0';} else {STR [0] = '1', STR [1] = '0', STR [2] = '. ', STR [3] =' \ 0';} strcpy (path [rear], action [J]); strcat (path [rear], STR ); rear ++ ;}}} front ++;} Ans =-1;} void print_path (INT cur) {If (father [cur]! =-1) {print_path (father [cur]); printf ("% s \ n", path [cur]) ;}} int main () {int U, V, CAS = 1; while (scanf ("% d", & nroom, & ndoor, & nswitch), nroom) {memset (G, 0, sizeof (g); memset (control, 0, sizeof (Control); For (INT I = 0; I <ndoor; ++ I) {scanf ("% d", & U, & V); G [u] [v] = G [v] [u] = 1 ;} for (INT I = 0; I <nswitch; ++ I) {scanf ("% d", & U, & V ); control [u] [v] = 1;} BFS (); printf ("Villa # % d \ n", CAS ++); If (ANS! =-1) {printf ("the problem can be solved in % d steps: \ n", step [ANS]); print_path (ANS );} else printf ("the problem cannot be solved. \ n "); printf (" \ n ");} return 0 ;}

However, the story is not over yet. When I handed the program to the ultraviolet A and zoj was celebrating the AC, the evaluation results sent to the poj hit me heavily again: wa

Then I read The Discuss of poj and found that the test data of poj was first moved into a room, and then switched on and off the light. In this case, you need to first expand the move status during the search. After a slight change, the poj will be successful. AC:


/** Poj 1137 the new villa * Implicit Graph Search, hash weight * Time: 94 Ms (poj) * Author: d_double **/# include <cstdio> # include <cstring> # define maxn 50000 typedef int State [11]; State que [maxn]; int nroom, ndoor, nswitch, G [20] [20], control [20] [20]; int step [maxn], Father [maxn], ans; // Number of stored steps, father: Char action [3] [35] = {"-move to room", "-switch on light in room", "-switch off light in Room "}; char path [maxn] [35]; // save path co NST int hashsize = 2000000; // hash table size int head [hashsize], next [maxn]; inline int bkdrhash (State & S) {// hash function int seed = 131; // 31 131 1313 13131 131313 Etc .. int hash = 0; For (INT I = 1; I <= nroom; ++ I) hash = hash * seed + s [I]; Return (hash & 0x7fffffff) % hashsize;} inline int try_to_insert (INT s) {int H = bkdrhash (que [s]); int u = head [H]; while (u) {If (memcmp (que [u], que [s], sizeof (que [s]) = 0) Re Turn 0; u = next [u];} next [s] = head [H]; head [H] = s; return 1;} inline bool isok (State & S) {// determine whether the target State is met. For (INT I = 1; I <nroom; ++ I) if (s [I]) return false; if (s [nroom] = 2) return true; return false;} // The executed action inline bool run (State & S, int U, int V, int D) {Switch (d) {Case 0: // enter the room if (G [u] [v] & S [v]) {s [v] = 2; s [u] = 1; return true;} return false; Case 1: // turn on the light if (control [u] [v] &! S [v]) {s [v] = 1; return true;} return false; Case 2: // turn off the light if (control [u] [v] & S [v]) {s [v] = 0; return true;} return false ;}} inline void Init () {memset (Head, 0, sizeof (head); memset (que [0], 0, sizeof (que [0]); que [0] [1] = 2; // 1 indicates that the light is on, 0 is not on, and 2 persons are on (the light is on) Step [0] = 0; father [0] =-1;} void BFS () {Init (); int front = 0, rear = 1; while (front <rear) {State & s = que [Front]; If (isok (s) {ans = front; return;} Int U; // obtain the current room for (INT I = 1; I <= nroom; ++ I) if (s [I] = 2) {u = I; break;} For (INT v = 1; v <= nroom; ++ v) if (u! = V) {// first move the State & t = que [rear]; memcpy (t, s, sizeof (s); If (run (t, u, v, 0) {If (try_to_insert (rear) {FATHER [rear] = front; step [rear] = step [Front] + 1; char STR [4]; // save path. When the room number is greater than 9, you cannot directly use V + '0' if (v <10) {STR [0] = V + '0 ', STR [1] = '. ', STR [2] =' \ 0';} else {STR [0] = '1', STR [1] = '0', STR [2] = '. ', STR [3] =' \ 0';} strcpy (path [rear], action [0]); strcat (path [rear], STR ); rear ++ ;}}for (INT v = 1; v <= nR Oom; ++ v) if (u! = V) {// then switch the light State & t = que [rear]; memcpy (t, s, sizeof (s); If (run (t, u, v, 1) {// turn on the light if (try_to_insert (rear) {FATHER [rear] = front; step [rear] = step [Front] + 1; // save path. Note that when the room number is greater than 9, V + '0' char STR [4] cannot be used directly; If (v <10) {STR [0] = V + '0', STR [1] = '. ', STR [2] =' \ 0';} else {STR [0] = '1', STR [1] = '0', STR [2] = '. ', STR [3] =' \ 0';} strcpy (path [rear], action [1]); strcat (path [rear], STR ); rear ++ ;}} else if (run (t, u, V, 2) {// turn off the light if (try_to_insert (rear) {FATHER [rear] = front; step [rear] = step [Front] + 1; // save path. Note that when the room number is greater than 9, V + '0' char STR [4] cannot be used directly; If (v <10) {STR [0] = V + '0', STR [1] = '. ', STR [2] =' \ 0';} else {STR [0] = '1', STR [1] = '0', STR [2] = '. ', STR [3] =' \ 0';} strcpy (path [rear], action [2]); strcat (path [rear], STR ); rear ++ ;}}}front ++ ;}ans =-1 ;}void print_path (INT cur) {If (father [cur]! =-1) {print_path (father [cur]); printf ("% s \ n", path [cur]) ;}} int main () {int U, V, CAS = 1; while (scanf ("% d", & nroom, & ndoor, & nswitch), nroom) {memset (G, 0, sizeof (g); memset (control, 0, sizeof (Control); For (INT I = 0; I <ndoor; ++ I) {scanf ("% d", & U, & V); G [u] [v] = G [v] [u] = 1 ;} for (INT I = 0; I <nswitch; ++ I) {scanf ("% d", & U, & V ); control [u] [v] = 1;} BFS (); printf ("Villa # % d \ n", CAS ++); If (ANS! =-1) {printf ("the problem can be solved in % d steps: \ n", step [ANS]); print_path (ANS );} else printf ("the problem cannot be solved. \ n "); printf (" \ n ");} return 0 ;}

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.