UVA 10047 UVA Live 2035 bfs//for the shortest, must first try BFS//Determine the state, the first state must have coordinates x,y//also have to have direction, there must be a color value/////This is a state there are three kinds of attributes//each state as long as the experience, and then Experience doesn't make any sense.//Use an array of que thoughts to record on the line. In accordance with the direction of the search, I first use f[i][j] record is to//this point of the minimum distance, but how can not get through the sample//suddenly understand, if only this record the shortest distance, is not possible//because each element removed from the queue may change the status of the end of the state//So, Should be in the state to record a property, that is the time//Finally, notice the two samples between the blank line Ah ... WA got a pitch//well, then it's over. Well, keep practicing. #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat > #include <climits> #include <cmath> #include <complex> #include <cstdio> #include < cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL (A, B) (((a) + (b)-1)/(c)) #define Endl ' \ n ' #define GCD __gcd#define highbit (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_popcountlltypedef long Long ll;using namespace Std;const int MOD = 1000000007;const long Double PI = ACOs ( -1.L); Template<class t& Gt Inline T LCM (const t& A, const t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 30; Char Mp[maxn][maxn];int dx[4] = { -1,0,1,0};int Dy[4] = {0,1,0,-1};int N,m;char a[5][10] = {"GREEN", "White", "BLUE", "RED", " BLACK "};struct Node {int x,y;int on;int color;int time;}; int F[maxn][maxn];bool que[26][26][5][6];queue<node> q;void init () {for (int i=0;i<n;i++) scanf ("%s", Mp[i]); /memset (F,-1,sizeof (f)), memset (que,0,sizeof (que)), while (!q.empty ()) Q.pop (); void print () {for (int. i=0;i<n;i++) {for (int j=0;j<m;j++) {printf ("%d", F[i][j]);} Puts ("");} Puts ("");} void solve (int kase) {if (kase!=1) puts ("");p RinTF ("Case #%d\n", kase), int ex,ey;for (int. i=0;i<n;i++) for (int j=0;j<m;j++) {if (mp[i][j]== ' S ') {Q.push ((node) {I,j , 0,0,0}); que[i][j][0][0] = true;//f[i][j] = 0;} if (mp[i][j]== ' T ') {ex = I;ey = j;}} int time = -1;int cc = 0;while (!q.empty ()) {node x = Q.front ();//printf ("%d%d%d%s\n", X.x,x.y,x.on,a[x.color]); Q.pop (); if (X.x==ex && x.y==ey && x.color==0) {//time =//f[x.x][x.y];time = X.time;break;} for (int i=-1;i<=1;i++) {Int J = X.on + i;if (j==-1)//left-hand J = 3;if (j==4)//Right Turn J = 0;if (i==0) {//go straight int tx = x.x + dx[j];int ty = x.y + dy[j];if (tx<0| | tx>=n| | ty<0| | ty>=m) continue;if (mp[tx][ty]== ' # ') continue;if (!que[tx][ty][j][(x.color+1)%5]) {//f[tx][ty] = f[x.x][x.y]+1; Q.push (node) {tx,ty,j, (x.color+1)%5,x.time+1}); que[tx][ty][j][(x.color+1)%5] = true;}} else {//Turn case if (!que[x.x][x.y][j][x.color]) {F[x.x][x.y]++;q.push ((node) {x.x,x.y,j,x.color,x.time+1}); que[x.x][ X.y][j][x.color] = true;}}} Que[x.x][x.y][x.on][x.color] = false;} print (); if (time = =-1) {printf ("desTination not reachable ");} else {printf ("Minimum time =%d sec", time);} Puts ("");//puts ("");} int main () {int kase = 0;//freopen ("G:\\code\\1.txt", "R", stdin), while (scanf ("%d%d", &n,&m)) {if (!n&&! m) break;init (); solve (++kase);} return 0;}
UVA 10047 UVA Live 2035 the monocycle BFS