[ACWing] 1098. The Castle Problem

Subject address:

https://www.acwing.com/problem/content/1100/

     1   2   3   4   5   6   7  
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #
   #---#########---#####---#---#
 4 #   #   |   |   |   |   #   #
   #############################

   #  = Wall   
   |  = No wall
   -  = No wall

   方向:上北下南左西右东。

The picture above is a topographic map of a castle. Please write a program to calculate how many rooms there are in the castle and how big is the largest room. The castle is divided into m ∗ nm∗nm n grid areas, each grid area can have0 ∼ 4 0\sim 404 walls. Note: The wall thickness is ignored.

Input format: the
first line contains two integers mmm andnnn , respectively represent the length of the castle in the north-south direction and the east-west direction. Nextmmm lines, each line containsnnn integers, each integer represents the characteristics of the wall of the square at the corresponding position in the plan. The characteristics of the walls in each square are represented by the numberPPP to describe, we use1 11 means the west wall,2 22 means the north wall,4 44 represents the east wall,8 88 means south wall,PPP is the sum of the numbers of the square containing the wall. For example, if thePP ofa squareP is3 33 , then3 = 1 + 2 3 = 1 + 23=1+2. The square contains the west wall and the north wall. The inner wall of the castle is calculated twice, the block(1, 1) (1,1)(1,The south wall of 1 ) is also a block(2, 1) (2, 1)(2,1 ) The north wall. The entered data ensures that the castle has at least two rooms.

Output format:
a total of two lines, the first line outputs the total number of rooms, and the second line outputs the area (number of squares) of the largest room.

Data range:
1 ≤ m, n ≤ 50 1≤m,n≤501m,n50
0 ≤ P ≤ 15 0≤P≤15 0P15

You can use BFS. Here you need to correspond the binary digits of each number to the four directions, 1 = 2 0 1=2^01=20 West Wall, is the first0 00 directions;2 = 2 1 2=2^12=21 North Wall, which is the1st 11 direction;4 = 2 2 4=2^24=22 east wall is the2nd 22 directions;8 = 2 3 8=2^38=23 south wall is the3rd 33 directions. A trick can be used to make the arrayd = [0, − 1, 0, 1, 0] d=[0,-1,0,1,0]d=[0,1,0,1,0 ] , sod [0, 1, 2, 3] d[0,1,2,3]d[0,1,2,3 ] respectively represent the directions of the four walls in the northwest, southeast and southeast. code show as below:

#include <iostream>
#include <queue>
using namespace std;

const int N = 55, d[] = {
    
    0, -1, 0, 1, 0};
int m, n;
int a[N][N];
bool st[N][N];
queue<pair<int, int> > q;

int bfs(int x, int y) {
    
    
    int area = 1;
    q.push({
    
    x, y});
    st[x][y] = true;

    while (!q.empty()) {
    
    
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
    
    
            int nx = t.first + d[i], ny = t.second + d[i + 1];
            // 不能出界
            if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
            // 不能之前已经访问过
            if (st[nx][ny]) continue;
            // 不能撞墙
            if (a[t.first][t.second] >> i & 1) continue;

            q.push({
    
    nx, ny});
            st[nx][ny] = true;
            area++;
        }
    }

    return area;
}

int main() {
    
    
    cin >> m >> n;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            cin >> a[i][j];

    int cnt = 0, area = 0;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (!st[i][j]) {
    
    
                area = max(area, bfs(i, j));
                cnt++;
            }

    cout << cnt << endl;
    cout << area << endl;

    return 0;
}

Time and space complexity O (mn) O(mn)O ( m n )

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114559261