提交时间:2023-12-12 16:49:40

运行 ID: 115662

#include<bits/stdc++.h> #define int long long using namespace std; namespace Fast{ //注:快读快写与CCF函数不能一起用 inline int fr(){ register int x=0,f=1; static char c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=x<<1+x<<3+c^48; c=getchar(); } return x*f; } inline void fw(int x){ if(x==0){ putchar('0'); return; } if(x<0){ putchar('-'); x=-x; } stack<char> s; while(x){ s.push(x%10+48); x/=10; } while(!s.empty()){ putchar(s.top()); s.pop(); } } inline void DEBUG(){ puts("awa"); } inline void CCF(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); } } using namespace Fast; int n,m,a[55][55],b[55][55],stx,sty,enx,eny,d[55][55][4],o[13][3]={{-1,0},{-2,0},{-3,0},{0,-1},{0,-2},{0,-3},{1,0},{2,0},{3,0},{0,1},{0,2},{0,3}}; char dir[5]={'N','W','S','D'},di; map<char,int> mp; struct P{ int x,y,dire; }; void bfs(int i,int j,int di){ queue<P> q; q.push((P){i,j,di}); d[i][j][di]=0; while(!q.empty()){ P p=q.front(); q.pop(); int cx=p.x,cy=p.y,cdir=p.dire; for(int i=0;i<3;i++){ int sx=cx+o[cdir*3+i][0],sy=cy+o[cdir*3+i][1]; if(sx<=0||sx>n||sy<=0||sy>m||d[sx][sy][cdir]<0x3f3f3f3f||b[sx][sy]==1) continue; bool fl=false; for(int j=0;j<=i;j++){ if(b[cx+o[cdir*3+j][0]][cy+o[cdir*3+j][1]]==1){ fl=true; break; } } if(fl) continue; d[sx][sy][cdir]=d[cx][cy][cdir]+1; q.push((P){sx,sy,cdir}); } if(d[cx][cy][(cdir+3)%4]>=0x3f3f3f3f){ d[cx][cy][(cdir+3)%4]=d[cx][cy][cdir]+1; q.push((P){cx,cy,(cdir+3)%4}); } if(d[cx][cy][(cdir+1)%4]>=0x3f3f3f3f){ d[cx][cy][(cdir+1)%4]=d[cx][cy][cdir]+1; q.push((P){cx,cy,(cdir+1)%4}); } if(d[cx][cy][(cdir+2)%4]>=0x3f3f3f3f){ d[cx][cy][(cdir+2)%4]=d[cx][cy][cdir]+2; q.push((P){cx,cy,(cdir+2)%4}); } } } signed main(){ memset(d,0x3f3f3f3f,sizeof(d)); mp['N']=0; mp['W']=1; mp['S']=2; mp['E']=3; n=fr(),m=fr(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ a[i][j]=fr(); } } n--,m--; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) b[i][j]=a[i][j]==1||a[i+1][j]==1||a[i][j+1]==1||a[i+1][j+1]==1; } stx=fr(),sty=fr(),enx=fr(),eny=fr(); cin>>di; if(stx<=0||stx>n||sty<=0||sty>m||b[stx][sty]==1||b[enx][eny]==1){ cout<<-1; return 0; } if(stx==enx&&sty==eny){ cout<<0; return 0; } bfs(stx,sty,mp[di]); int ans=0x3f3f3f3f; for(int i=0;i<4;i++) ans=min(ans,d[enx][eny][i]); if(ans>=0x3f3f3f3f) ans=-1; fw(ans); return 0; }