1 条题解
-
0
C++ :
#include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #include <queue> using namespace std; struct Pos { int x, y, time; }; const int N = 1000 + 10; int n, m, c, sx, sy, vis[N][N]; char maze[N][N]; int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int judge(int x, int y) { if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && maze[x][y] != '#') return 1; return 0; } int dfs(int x, int y) { Pos a, t; queue <Pos> q; a.x = x, a.y = y, a.time = 0; vis[x][y] = 1; q.push(a); while (!q.empty()) { a = q.front(); q.pop(); if(maze[a.x][a.y] == 'E') return a.time; for(int i = 0; i < 4; ++i) { t.x = a.x + dir[i][0]; t.y = a.y + dir[i][1]; t.time = a.time + 1; if(judge(t.x, t.y)) { vis[t.x][t.y] = 1; q.push(t); } } } return -1; } int main() { int t; cin >> t; while (t--) { memset(vis, 0, sizeof(vis)); cin >> n >> m >>c; for(int i = 0; i < m; ++i) { cin >> maze[i]; for(int j = 0; j < n; ++j) { if(maze[i][j] == 'S') { sx = i; sy = j; } } } cout << dfs(sx, sy) << endl; } return 0; }
- 1
信息
- ID
- 1027
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者