我炸了。
查看原帖
我炸了。
955954
Miracle_InDream楼主2024/1/13 23:17
#include<bits/stdc++.h>
using namespace std;
long long n,m;
char mp[1005][1005];
int vis[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
long long a,b;
struct node
{
	long long x;
	long long y;
	long long step;
};
queue<node> q;
node s;
long long step0;
void bfs()
{
	s.x=a-1;
	s.y=b-1;
	s.step=1;
	q.push(s);
	vis[s.x][s.y]=1;
	while(!q.empty())
	{
		node tmp=q.front();
		step0=tmp.step;
		q.pop();
		if(vis[tmp.x][tmp.y]==1)
		{
			continue;
		}
		vis[tmp.x][tmp.y]=1;
		for(int i=0;i<4;i++)
		{
			node a=tmp;
			a.x+=dx[i];
			a.y+=dy[i];
			a.step++;
			if(mp[tmp.x][tmp.y]=='1'&&vis[a.x][a.y]==0)
			{
				if(mp[a.x][a.y]=='0')
				{
					q.push(a);
				}
			}
			else
			{
				if(mp[a.x][a.y]=='1')
				{
					q.push(a);
				}
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(long long i=0;i<n;i++)
	{
		for(long long j=0;j<n;j++)
		{
			cin>>mp[i][j];
		}
	}
	for(long long i=0;i<m;i++)
	{
		cin>>a>>b;
		bfs();
		cout<<step0<<endl;
	}
	return 0;
}
2024/1/13 23:17
加载中...