Codeforces Round #420 (Div. 2), problem: (D) Okabe and City Solution In C/C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=1e4+5;
const int inf=0x7f7f7f7f;
int x[maxn],y[maxn],dis[maxn];
bool vis[maxn];
int k,n,m;
int bfs()
{
queue<int>q;
q.push(1);
//memset
//for(int i=1;i<=n;i++)dis[i]=inf;
memset(dis,0x7f,sizeof dis);
dis[1]=0;
vis[1]=true;
while(!q.empty())
{
int u;
u=q.front();q.pop();
for(int i=1;i<=k;i++)
{
if(u==i)continue;
int w=inf;
int l1=abs(x[i]-x[u]);
int l2=abs(y[i]-y[u]);
if(l1+l2<=1)w=0;
else if(l1<=2||l2<=2)w=1;
if(dis[i]>dis[u]+w)
{
dis[i]=dis[u]+w;
if(!vis[i])
{
vis[i]=true;q.push(i);
}
q.push(i);
}
}
vis[u]=0;

}
return dis[k]==inf?-1:dis[k];

}
int main()
{
scanf(“%d %d %d”,&n,&m,&k);
bool flag=false;
for(int i=1;i<=k;i++)
{
scanf(“%d %d”,&x[i],&y[i]);
flag|=(x[i]==n&&y[i]==m);
}
if(!flag)
{
x[++k]=n+1;y[k]=m+1;
}
printf(“%d\n”,bfs());
}

Everything You Need to...

📢 As an Amazon Associate, I earn from qualifying...

Russ Allbery: Review: Radiant...

Science fiction enthusiasts and literary critics alike are turning...

Why Pet Travel Gear...

📢 As an Amazon Associate, I earn from qualifying...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to...

7 Reasons This Coffee...

📢 As an Amazon Associate, I earn from qualifying...

Why Kitchen Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Everything You Need to Know About Rechargeable Home Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Russ Allbery: Review: Radiant Star

Science fiction enthusiasts and literary critics alike are turning their eyes toward the upcoming release of Radiant Star, the latest novel by award-winning author...

Why Pet Travel Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Pet Travel Gear through a proper 30-day real-world test, I can tell...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to tackle with the arrival of Mall Together-TENOKE. This brand-new release delivers an immersive shopping mall...

7 Reasons This Coffee Bar Accessories Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Why Kitchen Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Is Dorm Room Hacks Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Camping Essentials Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Everything You Need to Know About Outdoor Patio Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...