luogu#P1331. 海战

    ID: 327 远端评测题 800ms 128MiB 尝试: 2 已通过: 0 难度: 3 上传者: 标签>搜索福建省历届夏令营广度优先搜索 BFS深度优先搜索 DFS

海战

Background

During the summit, the armed forces are on high alert. Police will monitor every street, the army will guard buildings, and the airspace will be filled with F-2003 aircraft.

In addition, cruisers and fleets will be sent to protect the coastline. Unfortunately, for various reasons, only a few officers in the Navy can command large naval battles. Therefore, they have trained some new naval commanders. The officers chose the "Sea Battle" game to help them learn.

Problem Description

On a square board, a fixed number of ships with fixed shapes are placed, and no ship may touch any other ship. In this problem, each ship is a square: every ship must be a solid square composed of connected # cells with sides parallel to the grid axes.

Compute the total number of ships placed on the board.

Input Format

The first line contains two integers RR and CC, separated by a space, denoting the number of rows and columns of the game board.

Each of the next RR lines contains CC characters, each being # or .. # denotes a part of a ship, and . denotes water.

Output Format

Output one line. If the placement is correct (that is, the board consists only of pairwise non-touching square ships; if two # cells are vertically or horizontally adjacent but belong to two different ships, then those two ships are considered touching), output There are S ships., where SS is the number of ships. Otherwise, output Bad placement..

6 8
.....#.#
##.....#
##.....#
.......#
#......#
#..#...#
There are 5 ships.

Hint

For 100%100\% of the testdata, 1R,C10001 \le R, C \le 1000.

Translated by ChatGPT 5