Poj 1856 Sea Battle【Dfs+思维】

来源:互联网 发布:山西省网络快报系统 编辑:程序博客网 时间:2024/06/07 07:53

Sea Battle

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 3030

 

Accepted: 1082

Description

During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to guard the banks of the Vltava river. Unfortunately, in the case of any incident, the Czech Admiralty have only a few captains able to control over the large sea battle. Therefore, it was decided to educate new admirals. As an excellent preparation, the game of "Sea Battle" was chosen to help with their study program. 

In this well-known game, a predefined number of ships of predefined shapes are placed on the square board in such a way that they cannot contact one another even with their corners. In this task, we will consider rectangular shaped ships only. The unknown number of rectangular ships of unknown sizes are placed on a rectangular board. All the ships are full rectangles built of hash characters. Write a program that counts the total number of ships present in the field. 

Input

The input consists of more scenarios. The description of each scenario begins with two integer numbers R and C separated with a single space, 1 <= R,C <= 1000. These numbers give the number of rows and columns in the game field. 

After these two numbers, there are R lines, each of them containing C characters. Each character is either hash ("#") or dot ("."). Hashes denote ships, dots water. 

Then, the next scenario description begins. At the end of the input, there will be a line containing two zeros instead of the field size. 

Output

Output a single line for every scenario. If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence "There are S ships." where S is the number of ships. 

Otherwise, print the sentence "Bad placement.". 

Sample Input

6 6

.....#

##...#

##...#

..#..#

.....#

######

6 8

.....#.#

##.....#

##.....#

.......#

#......#

#..#...#

0 0

Sample Output

Bad placement.

There are 5 ships.

Source

CTU Open 2002

 

题目大意:合法船只的定义:由#构成的矩形,被一圈“.”所围成的船就是合法船只。如果有不合法的船只输出Bad placement.,否则输出合法船只的个数。


思路:两层for枚举船的左上角起点,然后开始深搜,只找#,并维护最大x坐标和最大y坐标,并且统计#的个数。如果#的个数==(maxnx-起点x+1)*(maxny-起点y+1)那么这一定是个合法的矩形,否则就一定不是合法的矩形,如果有一个不合法的矩形就跳出。


AC代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char a[1005][1005];int fx[8]={-1,1,0,0,1,-1,1,-1};int fy[8]={0,0,1,-1,1,-1,-1,1};int maxnx;int maxny;int minnx;int minny;int n,m;int Dfs(int x,int y){    maxnx=max(maxnx,x);    maxny=max(maxny,y);    a[x][y]='.';    int sum=1;    for(int i=0;i<8;i++)    {        int yy=y+fy[i];        int xx=x+fx[i];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='#')        {            sum+=Dfs(xx,yy);        }    }    return sum;}int main(){    while(~scanf("%d%d",&n,&m))    {        if(n==0&&m==0)break;        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);        }        int ans=0;        int flag=0;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(a[i][j]=='#')                {                    maxnx=i;maxny=j;                    minnx=i;minny=j;                    int sum=Dfs(i,j);                    if(sum==(maxnx-minnx+1)*(maxny-minny+1))                    ans++;                    else                    {                        flag=1;break;                    }                }            }            if(flag==1)break;        }        if(flag==1)printf("Bad placement.\n");        else printf("There are %d ships.\n",ans);    }}/*6 66 6.....#####.#.###.#.....#.....#######*/












0 0