0分求调,在线等!!

P2199 最后的迷宫

用 vector
by xigou1834 @ 2023-10-02 09:48:15


```cpp #include<bits/stdc++.h> using namespace std; vector<vector<char>>a; vector<vector<bool>>vis; vector<vector<bool>>vis1; int q[16385][5], jx, jy, hx, hy, n, m, h, t; const int dx[] = {0, 0, -1, 1, 1, 1, -1, -1}; const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1}; void reset_a() { a.resize(n + 1, vector<char>(m + 1, ' ')); for(int i = 0;i <= n;i ++) { for(int j = 0;j <= m;j ++) { a[i][j] = ' '; } } } void reset_vis() { vis.resize(n + 1, vector<bool>(m + 1, false)); for(int i = 0;i <= n;i ++) { for(int j = 0;j <= m;j ++) { vis[i][j] = false; } } } void reset_vis1() { vis1.resize(n + 1, vector<bool>(m + 1, false)); for(int i = 0;i <= n;i ++) { for(int j = 0;j <= m;j ++) { vis1[i][j] = false; } } } void bfs() { if(vis[hx][hy]) { cout << 0 << endl; return ; } memset(q, 0, sizeof(q)); h = 1, t = 1; q[1][1] = hx, q[1][2] = hy, q[1][3] = 0, q[1][4] = 0; reset_vis1(); while(h <= t) { for(int i = 0;i < 4;i ++) { int nx = q[h][1] + dx[i], ny = q[h][2] + dy[i]; if(nx > 0 && nx <= n && ny > 0 && ny <= m && a[nx][ny] != 'X' && !vis1[nx][ny]) { vis1[nx][ny] = true; t ++, q[t][1] = nx, q[t][2] = ny, q[t][3] = h, q[t][4] = q[h][4] + 1; if(vis[nx][ny]) { cout << q[t][4] << endl; return ; } } } h ++; } cout << "Poor Harry\n"; return ; } int main() { cin >> n >> m;reset_a(); for(int i = 1;i <= n;i ++) { for(int j = 1;j <= m;j ++) { cin >> a[i][j]; } } while(true) { cin >> jx >> jy >> hx >> hy; if(jx == 0 && jy == 0 && hx == 0 && hy == 0) return 0; reset_vis(); for(int i = 0;i < 8;i ++) { int nx = jx, ny = jy; while(nx > 0 && nx <= n && ny > 0 && ny <= m && a[nx][ny] != 'X') { vis[nx][ny] = true, nx += dx[i], ny += dy[i]; } } bfs(); } } ```
by xigou1834 @ 2023-10-02 10:15:21


@[ZhuJiaqizuibang](/user/783810)
by xigou1834 @ 2023-10-02 10:15:56


|