標籤:des os io for ar 時間 amp size
/*本題的特殊之處,到達一個格子時,因為朝向不同,以及接觸地面的顏色不同,會處於不同的狀態;;;;;;;;;把(x, y, d, c)作為一個結點,表示所在位置(x, y),方向為d,顏色為c;;;;;------------------------------------------------------------------------在方向上我們把前,左,右編號為0,1,2;;;;顏色,從藍色開始編號為0,1,2,3;;;;;;;;;;--------------------------------------------------------------------------vis[x1][y1][0][0] = 1;起點S進入隊列node a, b;a.x = x1;a.y = y1;a.d = 0;a.c = 0;a.t = 0;q.push(a);-----------------------------------------------------------------------b.t = a.t + 1;前進方向;;;時間加1b.c = (a.c + 1)%5;擴充後的顏色b.x = a.x + d[a.d][0];擴充後的位置b.y = a.y + d[a.d][1];b.d = a.d;方向不變if(judge(b)){ if(b.x == x2 && b.y == y2 && b.c == 0)是否到達終點 return b.t; vis[b.x][b.y][b.d][b.c] = 1; q.push(b);}----------------------------------------------------------------------------------b = a;向右轉b.t ++;b.d = (b.d + 1)%4;擴充後的方向if(judge(b)){ vis[b.x][b.y][b.d][b.c] = 1; q.push(b);}b = a;向左轉b.t ++;b.d = (b.d - 1 + 4)%4;if(judge(b)){ vis[b.x][b.y][b.d][b.c] = 1; q.push(b);}-------------------------------------------------------------------for(int i=0; i<m; i++)尋找始末點S和T點{ for(int j=0; j<n; j++) { if(g[i][j] == 'S') { x1 = i; y1 = j; break; } }}for(int i=0; i<m; i++){ for(int j=0; j<n; j++) { if(g[i][j] == 'T') { x2 = i; y2 = j; break; } }}------------------------------------------------------------------------------*/#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int MAXN = 30;struct node{ int x, y, d, c, t;};int m, n;char g[MAXN][MAXN];int vis[MAXN][MAXN][6][6];int x1, x2, y1, y2;int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int judge(node b){ if(b.x < 0 || b.x >= m || b.y < 0 || b.y >= n) return false; if(g[b.x][b.y] == '#') return false; if(vis[b.x][b.y][b.d][b.c]) return false; return true;}int bfs(){ queue<node> q; vis[x1][y1][0][0] = 1; node a, b; a.x = x1; a.y = y1; a.d = 0; a.c = 0; a.t = 0; q.push(a); while(!q.empty()) { a = q.front(); q.pop(); b.t = a.t + 1; b.c = (a.c + 1)%5; b.x = a.x + d[a.d][0]; b.y = a.y + d[a.d][1]; b.d = a.d; if(judge(b)) { if(b.x == x2 && b.y == y2 && b.c == 0) return b.t; vis[b.x][b.y][b.d][b.c] = 1; q.push(b); } b = a; b.t ++; b.d = (b.d + 1)%4; if(judge(b)) { vis[b.x][b.y][b.d][b.c] = 1; q.push(b); } b = a; b.t ++; b.d = (b.d - 1 + 4)%4; if(judge(b)) { vis[b.x][b.y][b.d][b.c] = 1; q.push(b); } } return -1;}int main(){ //freopen("input.txt","r",stdin); int kase = 1; while(scanf("%d%d",&m,&n) != EOF && (n+m)) { if(kase != 1) printf("\n"); printf("Case #%d\n",kase++); getchar(); for(int i=0; i<m; i++) gets(g[i]); for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(g[i][j] == 'S') { x1 = i; y1 = j; break; } } } for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(g[i][j] == 'T') { x2 = i; y2 = j; break; } } } memset(vis, 0, sizeof(vis)); int time = bfs(); if(time == -1) printf("destination not reachable\n"); else printf("minimum time = %d sec\n",time); } return 0;}
----------------------------
wuwuwuwuwuwuwuwu...................................