WA第3和8个点的,注意边界不能走

P1126 机器人搬重物

``` #include<bits/stdc++.h> using namespace std; #define N 100 #define inf 0x3f3f3f3f //int d[4][2]={(0,1),(1,0),(0,-1),(-1,0)}; int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; struct Node{ int x,y,d,w; bool operator<(const Node& b)const{ return this->w>b.w; } }; int n,m,mp[N][N],x,xa,ya,dr,xb,yb,dist[N][N]; char dir; priority_queue<Node> q; void bfs(){ memset(dist,inf,sizeof(dist)); dist[xa][ya]=0; q.emplace(Node({xa,ya,dr,0})); Node cur,nxt; while(q.size()){ cur=q.top(); q.pop(); if(dist[cur.x][cur.y]<cur.w)continue; for(int i=0;i<4;i++){ int dis; if(i==cur.d) dis=0; else dis=(i-cur.d)%2?1:2; for(int j=1;j<=3;j++){ nxt.x=cur.x+dx[i]*j,nxt.y=cur.y+dy[i]*j,nxt.w=cur.w+dis+1,nxt.d=i; if(mp[nxt.x][nxt.y])break; if(nxt.x<=1||nxt.y<=1||nxt.x>=n||nxt.y>=m||dist[nxt.x][nxt.y]<=nxt.w)continue; dist[nxt.x][nxt.y]=nxt.w; q.emplace(nxt); } } } } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>x; if(x)mp[i][j]=mp[i+1][j]=mp[i][j+1]=mp[i+1][j+1]=1; } } cin>>xa>>ya>>xb>>yb>>dir; if(dir=='E')dr=0; else if(dir=='S')dr=1; else if(dir=='W')dr=2; else dr=3; n++,m++,xa++,ya++,xb++,yb++; bfs(); // cout<<'\n'; // for(int i=1;i<=n;i++){ // for(int j=1;j<=m;j++){ // cout<<mp[i][j]<<' '; // } // cout<<endl; // } // // cout<<'\n'; // for(int i=1;i<=n;i++){ // for(int j=1;j<=m;j++){ // if(dist[i][j]>=inf)cout<<'*'<<'\t'; // else cout<<dist[i][j]<<'\t'; // } // cout<<endl; // } if(dist[xb][yb]>=inf)cout<<-1; else cout<<dist[xb][yb]; //system("pause"); return 0; } ```
by xie_yuhao @ 2023-05-30 21:19:08


注意不能等1和n、m
by xie_yuhao @ 2023-05-30 21:19:37


#8 ``` 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 4 15 17 E ```
by xie_yuhao @ 2023-05-30 21:20:10


您拯救了一位敲了3.36KB代码却没能AK的苦逼
by liu_he_yong @ 2023-07-06 22:40:14


感谢
by bluedoor418 @ 2023-07-20 16:17:10


|