设为首页 加入收藏

TOP

POJ 1856 Sea Battle(dfs)
2015-07-20 17:55:28 】 浏览:2329
Tags:POJ 1856 Sea Battle dfs

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
  
   
#include
   
     #include
    
      #include
     
       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
      
       =0&&yy
       
        

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇POJ 1856 Sea Battle(BFS). 下一篇C++中 auto自动变量,命名空间,u..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目