求助!!!半百卡住了,和答案总相差一

P1126 机器人搬重物

~~那你加油哦!~~
by 素质玩家孙1超 @ 2020-08-09 11:27:46


~~加油 您能行~~
by lnwsh @ 2020-08-09 11:44:04


# 过了!!!```cpp #include <bits/stdc++.h> using namespace std; struct by { int x,y,fx,step; }q[100000]; char w; int n,m,h,t = 1,p,bx,by,ex,ey,a[100][100],g[100][100],zhuan[4][4] = { {0,1,2,1}, {1,0,1,2}, {2,1,0,1}, {1,2,1,0}, }; int main() { cin >> n >> m; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { cin >> p; if (p == 1) a[i][j] = a[i][j + 1] = a[i + 1][j] = a[i + 1][j + 1] = 1; } cin >> bx >> by >> ex >> ey >> w; if (a[bx][by] || a[ex][ey]) { cout << -1; return 0; } if (bx == ex && by == ey) { cout << 0; return 0; } if (w == 'N') q[1].fx = 0; if (w == 'E') q[1].fx = 1; if (w == 'S') q[1].fx = 2; if (w == 'W') q[1].fx = 3; q[1].x = bx,q[1].y = by; while (h < t) { h++; int x = q[h].x,y = q[h].y; for (int k = 0; k <= 3; ++k) { for (int i = 1; i <= 3; ++i) { int f = 0,nx,ny; switch(k) { case 0: nx = x - i,ny = y; for (int j = x - 1; j >= nx && j >= 1 && nx > 0 && nx < n && ny > 0 && ny < m; --j) if (a[j][y]) f = 1; break; case 1: nx = x,ny = y + i; for (int j = y + 1; j <= ny && j <= m && nx > 0 && nx < n && ny > 0 && ny < m; ++j) if (a[x][j]) f = 1; break; case 2: nx = x + i,ny = y; for (int j = x + 1; j <= nx && j <= n && nx > 0 && nx < n && ny > 0 && ny < m; ++j) if (a[j][y]) f = 1; break; case 3: nx = x,ny = y - i; for (int j = y - 1; j >= ny && j >= 0 && nx > 0 && nx < n && ny > 0 && ny < m; --j) if (a[x][j]) f = 1; break; } if (!f && nx > 0 && nx < n && ny > 0 && ny < m && a[nx][ny] == 0 && (g[nx][ny] == 0 || q[h].step + zhuan[q[h].fx][k] + 1 < g[nx][ny])) { q[++t].x = nx; q[t].y = ny; q[t].fx = k; g[nx][ny] = q[t].step = q[h].step + zhuan[q[h].fx][k] + 1; } } } } g[ex][ey]?cout << g[ex][ey]:cout << -1; } ```
by chenjiannan @ 2020-08-11 14:50:02


|