POJ 1856 Sea Battle(dfs)

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   for   ar   

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.

   

否則輸出矩形個數


比賽時沒有好的思路,糾結好久,後來看別人代碼,碉堡了,每次看到#,就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 1005char 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.