本人虽然AC,但是过了一个月后,TLE。
TLE on 10
MY code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int fx[5]={0,0,1,0,-1};
int fy[5]={0,1,0,-1,0};
int r,c,ans;
char a[1000][1000];
int vis[1000][1000];
bool pd() {
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
int cnt=0;
if(a[i][j]=='#') cnt++;
if(a[i-1][j]=='#') cnt++;
if(a[i-1][j+1]=='#') cnt++;
if(a[i][j+1]=='#') cnt++;
if(cnt==3) return false;
}
}
return true;
}
void dfs(int sx,int sy) {
for(int i=1; i<=4; i++) {
int tx=fx[i]+sx;
int ty=sy+fy[i];
if(tx>=1&&tx<=r&&ty>=1&&ty<=c&&vis[tx][ty]==0&&a[tx][ty]=='#') {
vis[tx][ty]=1;
dfs(tx,ty);
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin>>r>>c;
for(int i=1; i<=r; i++)
for(int j=1; j<=c; j++)
cin>>a[i][j];
if(!pd()) {
cout<<"Bad placement."<<endl;
return 0;
}
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
if(vis[i][j]==0&&a[i][j]=='#') {
ans++;
vis[i][j]=1;
a[i][j]='.';
dfs(i,j);
}
}
}
cout<<"There are "<<ans<<" ships.";
return 0;
}