題目地址:點擊打開鏈接
數組int visited[maxsize][maxsize][4][5];//4代表4個方向,5代編5中顏色,0,1,2,3代編北東南東西,Color:0,1,2,3,4
用來標記到達某個點的方向,顏色和時間。
注意輸出格式!
C++代碼:
#include#include #include #include #include using namespace std; const int maxsize = 30; string s[maxsize]; int visited[maxsize][maxsize][4][5];//4代表4個方向,5代編5中顏色,0,1,2,3代編北東南東西,Color:0,1,2,3,4 int m,n;//m行n列 bool flag; int start_x,start_y,end_x,end_y; int add[4][2]={-1,0,0,-1,1,0,0,1}; int t; struct MyStruct { int x; int y; int d; int c; }; void bfs(int x,int y,int d,int c) { visited[x][y][d][c]=0; deque dp; MyStruct p; p.x=x;p.y=y;p.d=d;p.c=c; dp.push_back(p); while(!dp.empty()) { MyStruct q=dp.front(); if(q.x==end_x&&q.y==end_y&&q.c==0) { t=visited[q.x][q.y][q.d][q.c]; flag=true; return ; } dp.pop_front(); int x_temp=q.x+add[q.d][0]; int y_temp=q.y+add[q.d][1]; int d=q.d; int c=(q.c+1)%5; if(x_temp>=0&&x_temp =0&&y_temp >m>>n&&(m||n)) { int i,j; for(i=0;i >s[i]; for(j=0;j 1) cout<