1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<bits/stdc++.h> using namespace std; inline int read(){ int ret=0,f=1;char ch=getchar(); while (ch<'0'ch>'9') {if (ch=='-') f=-f;ch=getchar();} while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar(); return ret*f; } void write(int x){ if(x<0){ putchar('-'); write(-x); return ; } if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } int n,m,a[1010][1010],sum[1010][1010]; int vis[1010][1010],ans; void dfs(int x,int y){ if(vis[x][y]==1) return ; vis[x][y]=1; if(a[x][y+1]==1) dfs(x,y+1); if(a[x-1][y]==1) dfs(x-1,y); if(a[x+1][y]==1) dfs(x+1,y); if(a[x][y-1]==1) dfs(x,y-1); } int main(){ n=read();m=read(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char ch;cin>>ch; if(ch=='#') a[i][j]=1; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]==1){ int k=1; while(a[i+k][j+k]==1) k++; --k; int hh=sum[i+k][j+k]-sum[i-1][j]-sum[i][j-1]+sum[i-1][j-1]; ++k; if(hh<k*k){ puts("Bad placement."); return 0; } k=1; while(a[i+k][j-k]==1) ++k; --k; hh=sum[i+k][j]-sum[i-1][j]-sum[i+k][j-k-1]+sum[i-1][j-k-1]; ++k; if(hh<k*k){ puts("Bad placement."); return 0; } } } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]==1&&vis[i][j]==0){ ++ans; dfs(i,j); } } } cout<<"There are "; write(ans); cout<<" ships.";putchar('\n'); return 0; }
|