bfs WA/MLE 0pts 求助!

P1126 机器人搬重物

现在50pts了 ```cpp #include <bits/stdc++.h> using namespace std; int n,m; int sx,sy,ex,ey; const int dx[5][5] = {{0,0,0,0},{0,0,0,0},{0,1,2,3},{0,0,0,0},{0,-1,-2,-3}}; const int dy[5][5] = {{0,0,0,0},{0,1,2,3},{0,0,0,0},{0,-1,-2,-3},{0,0,0,0}}; const int dd[] = {0,1,-1}; char d; bool a[55][55],vis[55][55][5]; struct node { int x,y,step,dir; }; queue <node> q; int change(char x) { if (x == 'E') return 1; if (x == 'S') return 2; if (x == 'W') return 3; if (x == 'N') return 4; } int main() { cin >> n >> m; for (int i = 1;i <= n;i ++){ for (int j = 1;j <= m;j ++){ cin >> a[i][j]; if (a[i][j]){ a[i - 1][j] = 1; a[i][j - 1] = 1; a[i - 1][j - 1] = 1; } } } cin >> sx >> sy >> ex >> ey >> d; if (a[sx][sy]){ cout << -1 << endl; return 0; } if (sx == ex and sy == ey){ cout << 0 << endl; return 0; } vis[sx][sy][change(d)] = 1; q.push({sx,sy,0,change(d)}); while (!q.empty()){ node tmp = q.front(); q.pop(); if (tmp.x == ex and tmp.y == ey){ cout << tmp.step - 1 << endl; return 0; } for (int i = 1;i <= 2;i ++){ int nd = tmp.dir + dd[i]; if (nd < 1) nd += 4; if (nd > 4) nd -= 4; if (vis[tmp.x][tmp.y][nd]) continue; q.push({tmp.x,tmp.y,tmp.step + 1,nd}); } for (int i = 1;i <= 3;i ++){ int nx = tmp.x + dx[tmp.dir][i],ny = tmp.y + dy[tmp.dir][i]; if (nx < 2 or ny < 2 or nx > n - 1 or ny > m - 1) break; if (a[nx][ny] or vis[nx][ny][tmp.dir]) break; vis[nx][ny][tmp.dir] = 1; q.push({nx,ny,tmp.step + 1,tmp.dir}); } } cout << -1 << endl; return 0; } ```
by syman_liu @ 2024-04-22 13:40:38


|