POJ 1856 Sea Battle (DFS)

Source: Internet
Author: User
Tags integer numbers

POJ 1856 Sea Battle (DFS)

In the figure, each rectangle '#' connecting block represents a ship. If one ship is adjacent to the other ship with edges or edges, the two ships are considered to have collided. If a ship has collided with each other, the output is bad. Otherwise, the output is shown in the figure. how many ships are there?

You can enclose the image in a circle '. 'Each vertex in the traversal graph shows that when one of the four structures in the graph exists, the ship will collide with the output and exit the loop. Otherwise, the dfs vertex can output the number of connected blocks if the structure does not exist in the graph. ship count


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> # include # Include Using namespace std; const int N = 1005; char mat [N] [N]; bool vis [N] [N]; int x [4] =, 0, 0}, y [4] = {0,-1, 1}; bool isBad (int I, int j) {if (mat [I] [j] = '. '& mat [I-1] [j] =' # '& mat [I] [J-1] =' # ') return 1; if (mat [I] [j] = '. '& mat [I-1] [j] =' # '& mat [I] [j + 1] =' # ') return 1; if (mat [I] [j] = '. '& mat [I + 1] [j] =' # '& mat [I] [J-1] =' # ') return 1; if (mat [I] [j] = '. '& mat [I + 1] [j] =' # '& mat [I] [j + 1] =' # ') return 1; return 0;} int dfs (int I, int j) {if (vis [I] [j] | mat [I] [j] = '. ') return 0; vis [I] [j] = true; for (int k = 0; k <4; ++ k) if (mat [I + x [k] [j + y [k] = '#') dfs (I + x [k], j + y [k]); return 1 ;}int main () {int I, j, n, m; while (scanf ("% d", & n, & m), n) {for (I = 1; I <= n; ++ I) scanf ("% s", mat [I] + 1 ); for (I = 0; I <= n + 1; ++ I) mat [I] [0] = mat [I] [m + 1] = '. '; for (j = 0; j <= m + 1; ++ j) mat [0] [j] = mat [n + 1] [j] = '. '; int ans = 0; memset (vis, 0, sizeof (vis); for (I = 1; I <= n; ++ I) {for (j = 1; j <= m; ++ j) if (isBad (I, j) break; else ans + = dfs (I, j ); if (j <= m) break;} if (I <= n) {printf ("Bad placement. \ n "); continue;} printf (" There are % d ships. \ n ", ans);} return 0 ;}

Sea Battle

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.