Nyist 82 maze Treasure Hunt (1) (BFS)

Source: Internet
Author: User
Document directory
  • This topic includes the door and key on the basic BFs. You need to find all the keys to open the door.
  • 1. First count the number of keys of each type, and then write down the location of the door.
  • 3. In case of a door, check whether the key is correct. If yes, enter the team and Mark accessed. If not, Mark accessed.
  • 4. Follow the normal BFS for others.

Question connection: http://acm.nyist.net/JudgeOnline/problem.php? PID = 82

This topic includes the door and key on the basic BFs. You need to find all the keys to open the door. 1. First count the number of keys of each type, and then write down the location of the door. 2. in BFs, when a key is encountered, enter the team, count all types of keys found, and then determine whether the key is correct. If yes, find the corresponding door. If the door has been accessed before, then the door enters the team. (This door may be blocked and cannot be entered. Therefore, you cannot join the door directly. You can only join the door that you have previously accessed ). 3. In case of a door, check whether the key is correct. If yes, enter the team and Mark accessed. If not, Mark accessed. 4. Follow the normal BFS for others.
# Include <iostream> # include <cstring> # include <queue> using namespace STD; struct point {int X, Y;} s, E, men [6]; int N, m; int H [4] [2] = {-, 0,-1}; int yaosum [6], findyao [6], BZ [30] [30]; char map [30] [30]; queue <point> q; int BFS (point S) {int I; point temp, T; while (! Q. Empty () Q. Pop (); q. Push (s); BZ [S. x] [S. Y] = 1; while (! Q. empty () {temp = Q. front (); q. pop (); If (temp. X = E. X & temp. y = E. y) return 1; for (I = 0; I <4; I ++) {T. X = temp. X + H [I] [0]; T. y = temp. Y + H [I] [1]; If (T. x> = 0 & T. x <n & T. y> = 0 & T. Y <M & map [T. x] [T. y]! = 'X') {If (Map [T. x] [T. y]> = 'A' & map [T. x] [T. y] <= 'E '&&! BZ [T. x] [T. y]) // yes. Check whether the {If (findyao [map [T. x] [T. y]-'a'] = yaosum [map [T. x] [T. y]-'a']) // you can open {q. push (t); BZ [T. x] [T. y] = 1;} else BZ [T. x] [T. y] = 1; // cannot be opened. Mark accessed} else if (Map [T. x] [T. y]> = 'A' & map [T. x] [T. y] <= 'E '&&! BZ [T. x] [T. y]) // is the key {findyao [map [T. x] [T. y]-'a'] ++; q. push (t); BZ [T. x] [T. y] = 1; // determines whether the key has been found and whether the corresponding door has been accessed. If the key is set, the door has been visited before, if (findyao [map [T. x] [T. y]-'a'] = yaosum [map [T. x] [T. y]-'a']) {point M; M. X = men [map [T. x] [T. y]-'a']. x; M. y = men [map [T. x] [T. y]-'a']. y; If (BZ [M. x] [M. y]) Q. push (m); // field} else if (Map [T. x] [T. y] = '. '| map [T. x] [T. y] = 'G ')&&! BZ [T. x] [T. y]) {q. push (t); BZ [T. x] [T. y] = 1 ;}}} return 0;} int main (INT argc, char * argv []) {int I, J; while (CIN> N> M & (n + M) {memset (yaosum, 0, sizeof (yaosum); memset (findyao, 0, sizeof (findyao); memset (BZ, 0, sizeof (BZ); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {CIN> map [I] [J]; If (Map [I] [J] = 's') {S. X = I; S. y = J;} else if (Map [I] [J] = 'G') {e. X = I; E. y = J;} else if (Map [I] [J]> = 'A' & map [I] [J] <= 'E ') yaosum [map [I] [J]-'a'] ++; else if (Map [I] [J]> = 'A' & map [I] [J] <= 'E ') {MEN [map [I] [J]-'a']. X = I; men [map [I] [J]-'a']. y = J ;}} if (BFS (s) cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}

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.