1 条题解
-
0
C++ :
#include <cstdio> #include <cstring> #include <queue> using namespace std; int m, n; struct node{ int x, y; node(int a, int b){ x = a; y = b; } }*xm, *d; char maze[301][301]; int dist[301][301], steps; bool vis[301][301]; queue<node*> q; void PUSH(int a, int b){ if(vis[a][b] || a < 1 || b < 1 || a > m || b > n || maze[a][b] == 'S') return; q.push(new node(a, b)); dist[a][b] = steps+1; vis[a][b] = 1; } int bfs(){ memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); node *cur = new node(xm->x, xm->y); while(!q.empty()) q.pop(); q.push(cur); vis[xm->x][xm->y] = 1; while(!q.empty()){ cur = q.front(); q.pop(); steps = dist[cur->x][cur->y]; if(cur->x == d->x && cur->y == d->y) return dist[d->x][d->y]; if(maze[cur->x][cur->y] == 'L'){ maze[cur->x][cur->y] = 'P'; vis[cur->x][cur->y] = 0; PUSH(cur->x, cur->y); }else{ PUSH(cur->x - 1, cur->y); PUSH(cur->x, cur->y - 1); PUSH(cur->x, cur->y + 1); PUSH(cur->x + 1, cur->y); } } return -1; } int main(){ int i, j; while(scanf("%d%d", &m, &n) && m||n){ for(i = 1; i <= m; i++) for(j = 1; j <= n; j++){ scanf(" %c ", &maze[i][j]); if(maze[i][j] == 'X') xm = new node(i, j); else if(maze[i][j] == 'D') d = new node(i, j); } printf("%d\n", bfs()); } return 0; }
- 1
信息
- ID
- 951
- 时间
- 2000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者