import sys
from collections import deque
toks = (tok for tok in sys.stdin.read().split())
N = int(next(toks))
r1 = int(next(toks))-1
c1 = int(next(toks))-1
r2 = int(next(toks))-1
c2 = int(next(toks))-1
grid = [[] for _ in range(N)]
for i in range(N):
row = next(toks)
for j in range(N):
if row[j] == "0":
grid[i].append(False)
else:
grid[i].append(True)
def reachable_cells(r, c):
reachable = []
vis = [[False for _ in range(N)] for _ in range(N)]
bfs_q = deque()
bfs_q.append((r, c))
vis[r][c] = True
while len(bfs_q) > 0:
cur_r, cur_c = bfs_q.popleft()
reachable.append((cur_r, cur_c))
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
new_r = cur_r+dx
new_c = cur_c+dy
if new_r < 0: continue
if new_c < 0: continue
if new_r >= N: continue
if new_c >= N: continue
if vis[new_r][new_c]: continue
if grid[new_r][new_c] == 1: continue
bfs_q.append((new_r, new_c))
vis[new_r][new_c] = True
return reachable
first_reachable = reachable_cells(r1, c1)
second_reachable = reachable_cells(r2, c2)
if (r1, c1) in second_reachable:
print(0)
else:
min_land_tunnel_cost = None
for rs, cs in first_reachable:
for rt, ct in second_reachable:
land_tunnel_cost = (rs-rt)**2 + (cs-ct)**2
if min_land_tunnel_cost == None:
min_land_tunnel_cost = land_tunnel_cost
else:
min_land_tunnel_cost = min(min_land_tunnel_cost, land_tunnel_cost)
print(min_land_tunnel_cost)
//اللَّهُ لَا إِلَٰهَ إِلَّا هُوَ ۚ وَعَلَى اللَّهِ فَلْيَتَوَكَّلِ الْمُؤْمِنُونَ
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Abdulrhem ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
const long long N=2e5+10,mod=1e9+7;
#define all(x) x.begin(),x.end()
#define take(arr,n) for (int i=0;i<n;i++) cin>>arr[i];
#define f first
#define s second
vector<ll>theprimes;
int n,m;
bool vis[55][55];
int dist[1005][1005];
vector<bool>primes(N,1);
void sieve()
{
//o(nlogn)
// memset(primes,1,sizeof primes);
primes[0]=primes[1]=0;
for(int i=2;i*i<N;i++)
{
if(primes[i])
{
//theprimes.push_back(i);
for(int j=i*2;j<N;j+=i)
primes[j]=0;
}
}
}
pair<int,vector<int>> comp[N];
void modified_seive()
{
for(int i=0;i<N;i++)
{
comp[i].first=i;
comp[i].second.push_back(i);
}
for(int i=2;i*i<N;i++)
{
if(comp[i].first==i)
{
for(int j=i*2;j<N;j+=i) {
comp[j].second.push_back(i);
if (comp[j].first == j)
comp[j].first = i;
}
}
}
}
long long gcd(long long a,long long b)
{
if(b==0)return a;
else{
long long rem=a%b;
return gcd(b,rem);
}
}
ll lcm(ll a,ll b){
return a*b / gcd(a,b);
}
ll mul(int x, int y) {
return 1LL * x * y % mod;
}
ll add(int x, int y) {
return (x + y) % mod;
}
ll power(int x, unsigned int y)
{
int temp;
if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
ll binarySearch(ll &maxCoins,ll k,ll m,ll l){
ll left = 1,right = maxCoins;
ll mid;
ll ans = -1;
while(left<=right){
mid = (left+right)>>1;
if ((mid*m)-k >= l){
right = mid -1;
ans = mid;
}
else{
left = mid +1;
}
}
return ans;
}
bool inRange(int i,set<pair<int,int>>ans){
i++;
for (auto it:ans){
if (i>=it.f&&i<=it.s)return 1;
}
return 0;
}
bool allZero (string &b){
for (auto it:b){
if (it == '1')return 0;
}
return 1;
}
bool allOne(string &b){
for (auto it:b){
if (it == '0')return 0;
}
return 1;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char matrix[55][55];
void BFS(int x,int y,set<pair<int,int>>&island){
vis[x][y]=1;
queue<pair<int,int>>q;
q.push({x,y});
while(!q.empty()){
int xx=q.front().f;
int yy = q.front().s;
//cout<<"am in "<<xx<<" "<<yy<<endl;
q.pop();
for (int i=0;i<4;i++){
int xtmp = xx+dx[i];
int ytmp = yy + dy[i];
if (xtmp>=0&&xtmp<n&&ytmp>=0&&ytmp<n){
if (matrix[xtmp][ytmp]=='0'&&!vis[xtmp][ytmp]){
vis[xtmp][ytmp]=1;
q.push({xtmp,ytmp});
}
else if (matrix[xtmp][ytmp]=='1'){
island.insert({xx,yy});
}
}
}
}
}
void solve(){
cin>>n;
int x1,y1,x2,y2;
cin>>x1>>y1;
cin>>x2>>y2;
x1--;
y1--;
x2--;
y2--;
memset(vis,0,sizeof vis);
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cin>>matrix[i][j];
}
}
//cout<<"shit"<<endl;
set<pair<int,int>>island1;
set<pair<int,int>>island2;
BFS(x1,y1,island1);
if (vis[x2][y2]){
cout<<0<<endl;
return;
}
BFS(x2,y2,island2);
int ans = INT_MAX;
for (auto it:island1){
for (auto itt:island2){
int x1 = it.f;
int y1= it.s;
int x2 = itt.f;
int y2 = itt.s;
int tmp = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
ans = min(ans,tmp);
}
}
cout<<ans<<endl;
}
int main() {
Abdulrhem;
//sieve();
//to erase the first character in string use s.erase(0,1);
//long double pi = 3.141592653589793238462643383279502884;
int t = 1;
//cin>>t;
while (t--)
{
solve();
}
return 0;
}
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |
1268. Search Suggestions System | 841. Keys and Rooms |
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |
72. Edit Distance | 563. Binary Tree Tilt |
1306. Jump Game III | 236. Lowest Common Ancestor of a Binary Tree |
790. Domino and Tromino Tiling | 878. Nth Magical Number |