求助,全TLE
查看原帖
求助,全TLE
233815
zhjzhmh楼主2020/6/10 17:46
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int ito,jto;
}f[1010][1010];
int n,m,c[1000010],d[1000010],ans,b[1010][1010];
char ch;
void dfs(int x,int y,int k)
{
	c[k]=x;
	d[k]=y;
	if(x<1||x>n||y<1||y>m||b[x][y]==1) return;
	b[x][y]=1;
	if(f[x][y].ito==0&&f[x][y].jto==0)
	{
		for(int i=1;i<=k;i++)
		{
			f[c[i]][d[i]].ito=0;f[c[i]][d[i]].jto=0;
		}
		ans++;
		return;
	}
	dfs(f[x][y].ito,f[x][y].jto,k+1);
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>ch;
			if(ch=='u') {f[i][j].ito=i-1;f[i][j].jto=j;}
			if(ch=='d') {f[i][j].ito=i+1;f[i][j].jto=j;}
			if(ch=='l') {f[i][j].ito=i;f[i][j].jto=j-1;}
			if(ch=='r') {f[i][j].ito=i;f[i][j].jto=j+1;}
			if(ch=='o') {f[i][j].ito=0;f[i][j].jto=0;}
		}
	} 
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(f[i][j].ito==0&&f[i][j].jto==0) ans++;
			else {memset(c,0,sizeof(c));memset(d,0,sizeof(d));memset(b,0,sizeof(b));dfs(i,j,1);}
		}
	} 
	cout<<ans;
	return 0;
}
2020/6/10 17:46
加载中...