UVA 10047-the monocycle (BFS)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: From the starting point to the end point, each second can choose to go forward, left, right, each forward a wheel to the next color, a total of 5 colors, the beginning of green contact with the ground, north, requires the final green contact with the ground, to reach the target point and the shortest time.

Idea: Compared with the ordinary BFS, more than two additional conditions, so to the status of comprehensive, but also corresponding to add two dimensions. Water problem.

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long Long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int mod = 5;con St int INF = 1000000000;const int maxn = 50;int T,n,m,d[maxn][maxn][6][4],kase=0;int dir[] = {0,1,2,3};    Cardinal dx[] = {0, 1, 0, -1};int dy[] = {1, 0,-1, 0};struct node {int x, y, col, dir; Node (int x=0, int y=0, int col=0, int dir=0): x (x), Y (y), Col (Col), dir (dir) {}}s,t;char s[maxn][maxn];int BFS () {queue&lt    ;node> Q;    memset (d,-1, sizeof (d));    D[s.x][s.y][s.col][s.dir] = 0;    Q.push (S);        while (!q.empty ()) {node u = q.front (); Q.pop (); if (u.x= = T.x && u.y = = T.y && u.col = 0) return D[u.x][u.y][u.col][u.dir];        Node v = u;        v.x = v.x + Dx[v.dir];        V.Y = V.y + Dy[v.dir];        V.col = (v.col + 1)% MoD; if (v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && D[v.x][v.y][v.col][v.dir] =            =-1 && s[v.x][v.y]! = ' # ') {D[v.x][v.y][v.col][v.dir] = D[u.x][u.y][u.col][u.dir] + 1;        Q.push (v);        } v = u;        V.dir = (v.dir-1 + 4)% 4; if (v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && D[v.x][v.y][v.col][v.dir] =            =-1 && s[v.x][v.y]! = ' # ') {D[v.x][v.y][v.col][v.dir] = D[u.x][u.y][u.col][u.dir] + 1;        Q.push (v);        } v = u;        V.dir = (v.dir + 1)% 4; if (v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && D[v.x][v.y][v.col][v.dir] =   =-1 && s[v.x][v.y]! = ' # ') {         D[v.x][v.y][v.col][v.dir] = D[u.x][u.y][u.col][u.dir] + 1;        Q.push (v); }} return-1;}        int main () {while (~scanf ("%d%d", &n,&m)) {if (n = = 0 && m = = 0) return 0;        if (Kase) printf ("\ n");        for (int i=1;i<=n;i++) {scanf ("%s", s[i]+1);  } for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {if (s[i][j] = = ' s ') s = node (i, J,                0, 3);            else if (s[i][j] = = ' t ') t = node (i, j, 0, 0);        }} int ans = BFS ();        printf ("Case #%d\n", ++kase);        if (ans = =-1) printf ("Destination not reachable\n");    else printf ("Minimum time =%d sec\n", ans); } return 0;}


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.