Ultraviolet A 10047-The Monocycle BFS

Source: Internet
Author: User

 

Question:

The wheels of a unicorhole are divided into five slices, respectively coated with a different color, and now there is a person traveling on the M * N playing plane. The size of each grid is just a slice. There are obstacles in some grids. cyclists will arrive at T from S. On the way, he will either ride to the next grid or turn left or turn right 90 degrees, initially, the grid is facing north, and the green grid is mounted to the ground. The green grid is also mounted to the ground at the end.

 

Ideas:

Questions that I thought were difficult six months ago seem simple now. Haha ~

BFS ..

The status needs to record the direction and the color at this time.

The code is slightly ugly. I directly enumerated the BFS process.

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; const int MAXN = 30; char map [MAXN] [MAXN]; bool vis [MAXN] [MAXN] [5] [4]; // x, y, color, dirint m, n; struct state {int x, y; int step; int dir; // direction: north: 0 south: 1 east: 2 west: 3int color; // start with 0 and end with 0 bool operator <(const state & a) const {return step>. step;} state () {;} state (int x, int y, int step, int dir, int color): x (x), y (y ), step (step), dir (dir), color (color) {}} start, fin; int bfs () {memset (vis, 0, sizeof (vis); priority_queue
    
     
Q; q. push (start); vis [start. x] [start. y] [start. color] [start. dir] = true; while (! Q. empty () {state cur = q. top (); q. pop (); if (cur. x = fin. x & cur. y = fin. y & cur. color = 0) // find the answer! Return cur. step; int x = cur. x, y = cur. y, step = cur. step, color = cur. color, dir = cur. dir; switch (cur. dir) {case 0: // north if (! Vis [x-1] [y] [(color + 1) % 5] [dir] & map [x-1] [y]! = '#') {Vis [x-1] [y] [(color + 1) % 5] [dir] = 1; q. push (state (x-1, y, step + 1, dir, (color + 1) % 5);} if (! Vis [x] [y] [color] [2]) {vis [x] [y] [color] [2] = 1; q. push (state (x, y, step + 1, 2, color);} if (! Vis [x] [y] [color] [3]) {vis [x] [y] [color] [3] = 1; q. push (state (x, y, step + 1, 3, color);} break; case 1: // south if (! Vis [x + 1] [y] [(color + 1) % 5] [dir] & map [x + 1] [y]! = '#') {Vis [x + 1] [y] [(color + 1) % 5] [dir] = 1; q. push (state (x + 1, y, step + 1, dir, (color + 1) % 5);} if (! Vis [x] [y] [color] [2]) {vis [x] [y] [color] [2] = 1; q. push (state (x, y, step + 1, 2, color);} if (! Vis [x] [y] [color] [3]) {vis [x] [y] [color] [3] = 1; q. push (state (x, y, step + 1, 3, color);} break; case 2: // east if (! Vis [x] [y + 1] [(color + 1) % 5] [dir] & map [x] [y + 1]! = '#') {Vis [x] [y + 1] [(color + 1) % 5] [dir] = 1; q. push (state (x, y + 1, step + 1, dir, (color + 1) % 5);} if (! Vis [x] [y] [color] [0]) {vis [x] [y] [color] [0] = 1; q. push (state (x, y, step + 1, 0, color);} if (! Vis [x] [y] [color] [1]) {vis [x] [y] [color] [1] = 1; q. push (state (x, y, step +, color);} break; case 3: // West if (! Vis [x] [Y-1] [(color + 1) % 5] [dir] & map [x] [Y-1]! = '#') {Vis [x] [Y-1] [(color + 1) % 5] [dir] = 1; q. push (state (x, Y-1, step + 1, dir, (color + 1) % 5);} if (! Vis [x] [y] [color] [0]) {vis [x] [y] [color] [0] = 1; q. push (state (x, y, step + 1, 0, color);} if (! Vis [x] [y] [color] [1]) {vis [x] [y] [color] [1] = 1; q. push (state (x, y, step +, color);} break;} return-1;} int main () {int kase = 1; while (~ Scanf (% d, & m, & n), m | n) {for (int I = 1; I <= m; I ++) scanf (% s, map [I] + 1); for (int I = 0; I <= n + 1; I ++) // Add a wall to the outermost side, and so on. Map [0] [I] = map [m + 1] [I] = '#'; for (int I = 0; I <= m + 1; I ++) map [I] [0] = map [I] [n + 1] = '#';/* for (int I = 0; I <= m + 1; I ++) {for (int j = 0; j <= n + 1; j ++) printf (% c, map [I] [j]); puts ();} */for (int I = 1; I <= m; I ++) {for (int j = 1; j <= n; j ++) {if (map [I] [j] ='s ') {state temp (I, j, 0, 0); start = temp; map [I] [j] = '. ';} else if (map [I] [j] = 'T') {state temp (I, j, 0, 0); fin = temp; map [I] [j] = '. ';}} int ans = bfs (); if (kase! = 1) printf (); printf (Case # % d, kase ++); if (ans =-1) printf (destination not reachable ); elseprintf (minimum time = % d sec, ans);} return 0 ;}
    
   
  
 


 


 

 

Related Article

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.