18. 地下城與勇士
阿新 • • 發佈:2018-12-26
Description
龍神覺得無聊於是來到了地下城,這裡是一個巨大的迷宮,有一些可以通行的路、一些不可以通行的牆,還有一些怪物。雖然龍神可以輕易地幹掉這些怪物,但他覺得這樣就太沒意思了,他觀察到這些怪物每
#include <queue> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn = 105; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0};//上下右左 struct node { int x, y, z; int step; // 初始化列表 node(int _x, int _y,int _z, int _step) : x(_x), y(_y), z(_z), step(_step){} }; queue<node> que; int vis[maxn][maxn][55]; char mp[maxn][maxn]; int main() { int n, m, T, k; int start_x, start_y; scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &m,&k); for (int i = 0; i < n; ++i) { scanf("%s", &mp[i]); for (int j = 0; j < m; ++j) { if (mp[i][j] == 'S') { start_x = i; start_y = j; } } } // 又是一套初始化 memset(vis, 0, sizeof(vis)); que.push(node(start_x, start_y, 0, 0));//把括號內的三個作為三個引數傳遞進去,把一個node型資料放進佇列 vis[start_x][start_y][0] = 1; int ans; int flag = 0; while (!que.empty()) { node tmp = que.front();//tmp為取出來的待處理的隊首struct node型別的元素 que.pop(); if (mp[tmp.x][tmp.y] == 'E') { ans = tmp.step; flag = 1; break; } for (int i = 0; i < 4; ++i) { int x = tmp.x + dx[i], y = tmp.y + dy[i]; if((tmp.z+1)%k)//此層有怪 { if (x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#' || mp[x][y]=='*'||vis[x][y][(tmp.z+1)%k] == 1) continue; } else//此層無怪 { if (x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#'||vis[x][y][(tmp.z+1)%k] == 1) continue; } que.push(node(x, y, tmp.z+1, tmp.step + 1)); vis[x][y][(tmp.z+1)%k] = 1; } } if(flag) printf("%d\n", ans); else printf("-1\n"); while(!que.empty()) que.pop(); } return 0; } // 7 7 // ...#... // .....#F // ....... // .#.#... // ....... // .S#..#. // .......