30分求助,样例都过不了

P1126 机器人搬重物

```cpp #include<iostream> #include<queue> using namespace std; bool vis[55][55],x = false; int sx,sy,gx,gy,n,m,ans = -1; char face; int dx[] = {-1,1,0,0,0, 0}, dy[] = { 0,0,1,2,3,-1}, df[] = { 2,2,1,1,1, 3}; struct point { int x,y; int step; }; queue <point> q; void bfs() { while(!q.empty()) { point f = q.front(); q.pop(); int x = f.x,y = f.y; if(x == gy && y == gx) { x = true; ans = f.step; return; } for(int i = 0;i < 6;i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && vis[xx][yy] == true) { point tool; tool.x = xx; tool.y = yy; tool.step = f.step + df[i]; q.push(tool); vis[xx][yy] = false; } } } } int main() { cin >> n >> m; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { cin >> vis[i][j]; vis[i][j] = !vis[i][j]; } } cin >> sx >> sy >> gx >> gy >> face; if(sx == gx && sy == gy) { cout << 0; return 0; } point tool; tool.x = sx,tool.y = sy; q.push(tool); bfs(); cout << (x ? ans : -1); return 0; } ``` 这样子50分拿下 后面我打表的
by ZackofZHOU @ 2024-01-08 21:11:55


此贴结
by GenesisCrystal @ 2024-01-09 18:27:06


|