【2023】Huawei OD machine test real test Java CC++ Python JS Go-question 0246-How many monitors need to be turned on

Topic 0246-How many monitors need to be turned on

topic description

In a rectangular parking lot, there is a corresponding monitor above each parking space. If and only when parking in the current parking space or any parking space in the four directions, front, rear, left, and right, the monitor needs to be turned on, and the parking lot at a certain moment is given. Distribution, please count the minimum number of monitors that need to be opened

enter description

Enter m in the first line, n indicates the length and width, satisfying 1<m, n<=20; enter m lines in the next line,
each line has n integers of 0 or 1, and the integers are separated by a space,
indicating that the line is parked case, where 0 means empty and 1 means stopped

output description

The minimum number of monitors that need to be turned on;

example one

enter

3 3
0 0 0
0 1 0
0 0 0

output

1

Idea analysis and complexity analysis

Idea analysis

The main idea of ​​this question is to perform a scan traversal to check the status of each parking space. If there is a car parked in the parking space (value 1), mark itself and the parking spaces in the four directions of up, down, left, and right as needing monitoring (set the value to 1). Note that due to the limited input format of the topic, we need to add a circle of boundaries outside the original parking lot map to facilitate the handling of boundary parking spaces. In order to facilitate the calculation of the four directions of up, down, left and right, we predefine a direction array, which contains the current position

Guess you like

Origin blog.csdn.net/amos_cloud/article/details/131226312