求助
#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)