标签:des style http color os io for ar
Description
Input
Output
Sample Input
6 6 .....# ##...# ##...# ..#..# .....# ###### 6 8 .....#.# ##.....# ##.....# .......# #......# #..#...# 0 0
Sample Output
Bad placement. There are 5 ships.
Source
#
题意:就是#必须是矩阵,即为长方形或正方形,还有每个#矩形的角不能共着,如第一个图中的## 因为他们有一个点共着了,而题目问的是只要有供着的,就输出
Bad placement.
否则输出矩形个数
比赛时没有好的思路,纠结好久,后来看别人代码,碉堡了,每次看到#,就dfs,记录搜到的最大x,y,最小x,y,和#的个数 temp ,if(temp==(maxx-minx+1)*(maxy-miny+1)),那么就是满足条件的,否者不满足输出
Bad placement.
不说了,上代码了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1005
char a[N][N];
int n,m;
int minx,maxx,miny,maxy;
int temp;
void dfs(int x,int y)
{
a[x][y]='.';
temp++;
int i,j;
minx=min(minx,x);
maxx=max(maxx,x);
miny=min(miny,y);
maxy=max(maxy,y);
for(i=-1;i<=1;i++)
for(j=-1;j<=1;j++)
{
int xx=x+i;
int yy=y+j;
if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='#')
dfs(xx,yy);
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),n+m)
{
for(i=0;i<n;i++)
scanf("%s",a[i]);
int ans=0,flag=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(a[i][j]=='#')
{
minx=maxx=i;
miny=maxy=j;
temp=0;
dfs(i,j);
if(temp==(maxx-minx+1)*(maxy-miny+1))
ans++;
else
{
flag=1;
i=n;
j=m;
}
}
if(flag)
printf("Bad placement.\n");
else
printf("There are %d ships.\n",ans);
}
return 0;
}
POJ 1856 Sea Battle(dfs),布布扣,bubuko.com
标签:des style http color os io for ar
原文地址:http://blog.csdn.net/u014737310/article/details/38532101