1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=(a);i<(n);i++) #define per(i,a,n) for(int i=(n-1);i>=(a);i--) #define fi first #define se second typedef pair <int,int> pII; typedef long long ll; const int INF = 0x3f3f3f3f;
int n; int stx,sty,edx,edy; int dir[4][2] = {1,0, -1,0, 0,1, 0,-1}; const int maxn = 100; int vis[maxn][maxn]; char mx[maxn][maxn]; vector<pII > st; vector<pII > ed; void bfs(int x,int y,bool sign){ vis[x][y] = 1; queue<pair<int,int> >q; q.push(make_pair(x,y)); while(!q.empty()){ int xx = q.front().first; int yy = q.front().second; q.pop(); if(sign) st.push_back(make_pair(xx,yy)); else ed.push_back(make_pair(xx,yy)); rep(i,0,4){ int xxx = xx + dir[i][0]; int yyy = yy + dir[i][1]; if(xxx<0 || xxx >=n || yyy<0 || yyy>=n) continue; if(mx[xxx][yyy] == '1') continue; if(vis[xxx][yyy]) continue; vis[xxx][yyy] = 1; q.push(make_pair(xxx,yyy)); } } } int main(){ #ifdef LOCAL freopen("3.in","r",stdin); #endif scanf("%d",&n); scanf("%d%d%d%d",&stx,&sty,&edx,&edy); stx--;sty--;edx--;edy--; rep(i,0,n){ scanf("%s",mx+i); } int ans = INF; bfs(stx,sty,1); bfs(edx,edy,0); int len1 = st.size(); int len2 = ed.size(); rep(i,0,len1){ rep(j,0,len2){ int x1 = st[i].first; int y1 = st[i].second; int x2 = ed[j].first; int y2 = ed[j].second; ans = min(ans,(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); } } printf("%d\n",ans); return 0; }
|