dfs 7WA+4TLE玄关求调
  • 板块P1141 01迷宫
  • 楼主int_inf
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/2 09:40
  • 上次更新2025/2/2 10:52:29
查看原帖
dfs 7WA+4TLE玄关求调
1201348
int_inf楼主2025/2/2 09:40

希望有dalao指正 蒟蒻太菜了qwq

#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,dx[]={1,0,-1,0},dy[]={0,1,0,-1},ans;
char c[1001][1001];
bool st[1001][1001];
inline int read()
{
	char c=getchar();
	int sign=1,k=0;
	while(c<'0'||c>'9')
	{
		if(c=='-') sign=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		k=k*10+c-'0';
		c=getchar();
	}
	return k*sign;
}
void dfs(int x,int y)
{
	for(int i=0;i<4;i++)
	{
		int tx=x+dx[i],ty=y+dy[i];
		if(st[tx][ty]==false&&tx>=1&&ty>=1&&tx<=n&&ty<=n&&(c[tx][ty]-'0')+(c[x][y]-'0')==1)
		{
			st[tx][ty]=true;
			dfs(tx,ty);
		}
	}
}
int main()
{
	n=read(),m=read();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>c[i][j];
		}
	}
	while(m--)
	{
		memset(st,sizeof(st),false);
		x=read(),y=read();
		st[x][y]=true;
		dfs(x,y);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(st[i][j]) ans++;
			}
		}
		cout<<ans<<endl;
	}
	
	return 0;
}
2025/2/2 09:40
加载中...