TLE了#2#7#9#10 求条
  • 板块P1331 海战
  • 楼主Mukerzmg
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/29 19:55
  • 上次更新2024/11/29 20:00:24
查看原帖
TLE了#2#7#9#10 求条
752466
Mukerzmg楼主2024/11/29 19:55

求助

#include<bits/stdc++.h>
using namespace std;
int r,c,sum=0;
int sea[1024][1024];
void dfs(int x,int y);
int main()
{
//	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>r>>c;
	for(int i=1;i<=r;i++)
	{
		for(int j=1;j<=c;j++)
		{
			char ch;cin>>ch;
			switch(ch)
			{
				case '#':
					sea[i][j]=1;
					break;
				case '.':
					sea[i][j]=0;
					break;
			}
		}
	}
	bool judge=1;
	for(int i=1;i<r&&judge;i++)
	{
		for(int j=1;j<c&&judge;j++)
		{
			if(sea[i][j]+sea[i+1][j+1]+sea[i][j+1]+sea[i+1][j]==3)
				judge=0;
		}
	}
	if(judge)
	{
		dfs(1,1);
		cout<<"There are "<<sum<<" ships.";
	}
	else
	{
		cout<<"Bad placement.";
	}
	return 0;
}
void dfs(int x,int y)
{
	if(y>r)return;
	if(sea[y][x])
	{
		int xmax,ymax;
		for(xmax=x;xmax<c&&sea[y][xmax+1];xmax++);
		for(ymax=y;ymax<r&&sea[ymax+1][x];ymax++);
		for(int i=y;i<=ymax;i++)
			for(int j=x;j<=xmax;j++)
				sea[j][i]=0;
		sum++;
		
		int xx,yy;
		if(x>1)xx=x-1;
		else	xx=1;
		if(y>1)yy=y-1;
		else	yy=1;
		
		dfs(xx,yy);
	}
	else
	{
		if(x==c)
			dfs(1,y+1);
		else
			dfs(x+1,y);
	}
}

样例也过不了(TLE)

2024/11/29 19:55
加载中...