为啥我会TLE
  • 板块P1141 01迷宫
  • 楼主__hjwucj__
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/2/26 19:23
  • 上次更新2024/2/26 21:31:59
查看原帖
为啥我会TLE
1021663
__hjwucj__楼主2024/2/26 19:23

题目链接

#include <bits/stdc++.h>
using namespace std;
int n,m,ans;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char a[1001][1001];
bool b[1001][1001];
struct node{
	int x,y;
} s;
queue <node> st;
int main ()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin>>a[i][j];
	for (int i=1;i<=m;i++)
	{
		cin>>s.x>>s.y;
		ans=0;
		st.push (s);
		memset (b,false,sizeof (b));
		while (st.size ()!=0)
		{
			ans++;
			if (a[st.front ().x][st.front ().y]=='0') 
			{
				for (int i=0;i<4;i++)
				{
					int tx=st.front ().x+dx[i];
					int ty=st.front ().y+dy[i];
					if (tx<1||tx>n||ty<1||ty>n) continue;
					if (b[tx][ty]) continue;
					if (a[tx][ty]=='0') continue;
					b[st.front ().x][st.front ().y]=true;
					b[tx][ty]=true;
					node l={tx,ty};
					st.push (l);
				}
			} else
				{
					for (int i=0;i<4;i++)
					{
						int tx=st.front ().x+dx[i];
						int ty=st.front ().y+dy[i];
						if (tx<1||tx>n||ty<1||ty>n) continue;
						if (b[tx][ty]) continue;
						if (a[tx][ty]=='1') continue;
						b[st.front ().x][st.front ().y]=true;
						b[tx][ty]=true;
						node l={tx,ty};
						st.push (l);
					}
				}
			st.pop ();
		}
		cout<<ans<<endl;
	}	
	return 0;
}

2024/2/26 19:23
加载中...