跳到正文

个人记录

P1746 离开中山路

Joeger约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月18日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int n,ax,ay,bx,by,vis[1100][1100],
d[5][3]={{1,0},{-1,0},{0,-1},{0,1}};
char a[1100][1100];
struct node{
	int x,y;
};
void bfs()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			vis[i][j]=-1;
		}
	}
	queue<node>q;
	node z={ax,ay};
	q.push(z);
	vis[ax][ay]=0;
	while(q.size())
	{
		node t=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int o=t.x+d[i][0],p=t.y+d[i][1];
			if(o<1 || o>n || p<1 || p>n)
			{
				continue;
			}
			if(vis[o][p]!=-1)
			{
				continue;
			}
			if(a[o][p]=='1')
			{
				continue;
			}
			node z={o,p};
			q.push(z);
			vis[o][p]=vis[t.x][t.y]+1;
		}
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>a[i][j];
		}
	}
	cin>>ax>>ay>>bx>>by;
	bfs();
	cout<<vis[bx][by];
	return 0;
}
原文