UVA-10047 the Monocycle (BFS)

Source: Internet
Author: User

The main idea: there is a n*m grid, there are some places on the grid there are obstacles
Now there is a man, riding a wheelbarrow, asking from one place to another, when riding a wheelbarrow, you can only go straight, or turn left, turn right, can't go backwards
The wheel of a wheelbarrow is divided into 5 parts, each of which has a corresponding color, the first is green downward, and when passed a lattice, the color will change
Ask from the starting point to the end point, to the end of the wheel car green color downward, how long it takes

Problem-solving ideas: violent BFS

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace STD;#define Nstructnode{intX, Y, dir, time, color;} Startintdir[4][2] = {{-1,0}, {0,1}, {0, -1}, {1,0}};intG[n][n], vis[n][n][5][4];CharStr[n];intend_x, End_y, N, M;voidInit () { for(inti =0; I < n; i++) {scanf('%s ', str); for(intj =0; J < M; J + +) {if(Str[j] = =' # ') {G[i][j] =1; }Else{G[i][j] =0;if(Str[j] = =' S ') {start.x = i;                Start.y = j; }if(Str[j] = =' T ') {end_x = i;                End_y = j; }            }        }    }memset(Vis,0,sizeof(VIS));}voidSolve () { queue<Node>Q Start.color =0; Start.time =0; Start.dir =0;    Q.push (start); vis[start.x][start.y][0][0] =1; while(!q.empty ())        {Node T = Q.front (); Q.pop ();if(T.x = = end_x && T.y = = End_y && T.color = =0) {printf("Minimum time =%d sec\n", t.time);return; } for(inti =0; I <4; i++) {if(T.dir + i = =3)Continue;            Node tt;            Tt.x = T.x;            Tt.y = T.y;            Tt.color = T.color; Tt.time = T.time +1; Tt.dir = T.dir;if(Tt.dir = = i) {Tt.x + = dir[tt.dir][0]; Tt.y + = dir[tt.dir][1]; Tt.color = (Tt.color +1) %5;if(Tt.x <0|| Tt.x >= N | | Tt.y <0|| Tt.y >= m | | G[TT.X][TT.Y] | | Vis[tt.x][tt.y][tt.color][tt.dir])Continue; Vis[tt.x][tt.y][tt.color][tt.dir] =1;            Q.push (TT); }Else{Tt.dir = i;if(!vis[tt.x][tt.y][tt.color][tt.dir]) {Vis[tt.x][tt.y][tt.color][tt.dir] =1;                Q.push (TT); }            }        }    }printf("Destination not reachable\n");}intMain () {intCAS =1;intFlag =0; while(scanf("%d%d", &n, &m)! = EOF && n + m) {if(flag)printf("\ n");ElseFlag =1; Init ();printf("Case #%d\n", cas++);    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA-10047 the Monocycle (BFS)

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.