bfs怎么写记忆化,T了
  • 板块P1141 01迷宫
  • 楼主Acwing_zbz
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/3/27 17:33
  • 上次更新2024/3/27 20:25:37
查看原帖
bfs怎么写记忆化,T了
1038943
Acwing_zbz楼主2024/3/27 17:33
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=1010;
char g[N][N];
bool st[N][N];
int n,m,dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int bfs(int x,int y)
{
	int res=1;
	queue<PII> q;
	q.push({x,y});
	st[x][y]=true;
	while(q.size())
	{
		auto t=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int a=dx[i]+t.first,b=dy[i]+t.second;
			if(a>n||b>n||a<1||b<1||g[a][b]==g[t.first][t.second]||st[a][b])continue;
			st[a][b]=true;
			q.push({a,b});
			res++;
		}
	}
	return res;
}
int main()
{
	cin>>n>>m;
	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		cin>>g[i][j];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		memset(st,false,sizeof st);
		cout<<bfs(x,y)<<endl;
	}
	return 0;
 } 
2024/3/27 17:33
加载中...