提交时间:2023-12-12 16:52:00
运行 ID: 115665
#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(){ //原来想叫CINCOUTFASTER的结果发现缩写竟然是CCF(doge) 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; int cy = p.y; int cdir = p.dire; //cout<<cx<<' '<<cy<<' '<<cdir<<'\n'; for (int i = 0;i < 3;i++){ int sx = cx + o[cdir * 3 + i][0]; int 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(); /* for (int i = 1;i <= n;i++){ for (int j = 1;j <= m;j++){ if (stx == i && sty == j){ cout<<"X "; } else if (enx == i && eny == j){ cout<<"Y "; } else{ cout<<b[i][j]<<' '; } } cout<<'\n'; } */ 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; } //cout<<mp[di]<<'\n'; bfs(stx,sty,mp[di]); int ans = 0x3f3f3f3f; for (int i = 0;i < 4;i++){ ans = min(ans,d[enx][eny][i]); //cout<<d[enx][eny][i]<<'\n'; } //cout<<b[stx][sty]<<' '<<b[stx][sty - 1]<<' '<<b[stx - 3][sty - 1]<<' '<<b[stx - 6][sty - 1]<<' '<<b[stx - 6][sty + 2]<<' '<<b[stx - 6][sty + 1]<<' '<<b[stx - 5][sty + 1]<<' '<<b[enx][eny]<<'\n'; //cout<<d[stx][sty][1]<<' '<<d[stx][sty - 1][1]<<' '<<d[stx][sty - 1][0]<<' '<<d[stx - 3][sty - 1][0]<<' '<<d[stx - 6][sty - 1][0]<<' '<<d[stx - 6][sty - 1][3]<<' '<<d[stx - 6][sty + 2][3]<<' '<<d[stx - 6][sty + 3][3]<<' '<<d[stx - 6][sty + 3][2]<<' '<<d[stx - 5][sty + 3][2]<<' '<<d[stx - 5][sty + 3][3]<<' '<<d[enx][eny][3]<<'\n'; if (ans >= 0x3f3f3f3f){ ans = -1; } fw(ans); return 0; }