Poj 1856 sea battle

Source: Internet
Author: User

Find the number of rectangles that do not want to be handed in the r * C diagram. If there is an intersection rectangle, the output isBad placement. Otherwise, the number of ships can be output.

Non-intersection means that the circle around a rectangle ('#') is '. 'surrounded, so we only need to find the largest and smallest x and y values starting from a certain vertex, and the number of '#' in the search is rec, if rec = (Maxx-Minx + 1) * (Maxy-miny + 1) is true, the rectangle is valid.


# Include <iostream> # include <stdio. h ># include <string >#include <cstring> # include <algorithm> # include <cmath> # define n 1010int Maxx, Maxy, Minx, miny; using namespace STD; char map [N] [N]; int R, C; bool check (int x, int y) {If (x> = 0 & x <R & Y> = 0 & Y <C & map [x] [Y] = '#') return true; return false;} int DFS (int x, int y) {Minx = min (Minx, x); Maxx = max (Maxx, X ); miny = min (miny, Y); Maxy = max (Maxy, Y); map [x] [Y] = '. '; int rec = 1; (Int I =-1; I <= 1; I ++) for (Int J =-1; j <= 1; j ++) {If (check (I + X, J + y) {rec + = DFS (I + X, J + Y) ;}} return rec ;}int main () {While (~ Scanf ("% d", & R, & C) {If (r = 0 & C = 0) break; For (INT I = 0; I <r; I ++) scanf ("% s", map [I]); int ans = 0; For (INT I = 0; I <R; I ++) for (Int J = 0; j <C; j ++) {If (Map [I] [J] = '#') {Maxx = Minx = I; Maxy = miny = J; int rec = DFS (I, j); If (Rec = (Maxx-Minx + 1) * (Maxy-miny + 1) ans ++; else {ans =-1; // if there is a bad ship, it is bad I = r; break ;}}} if (ANS =-1) printf ("Bad placement. \ n "); else printf (" there are % d ships. \ n ", ANS);} return 0 ;}


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.