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 ;}