Total passenger garlic problem solution --T1769: Roommate

Related topics

Topic Link

Total passenger garlic, https://nanti.jisuanke.com/t/T1769 .

My OJ, http://47.110.135.197/problem.php?id=5253 .

Title Description

Garlic king came to a strange city to work, he and some friends rented a house. The landlord sent them to the size chart, this house is very large, and very strange layout. Specifically, the house can be seen as a rectangle M × N, where the walls have been labeled '#', is labeled elsewhere '.'. Through '.'the area known as the room together, and now garlic king wanted to know, how many rooms there are.

Input Format

The first line contains two integers, N and M.

Next N rows, each row comprising M symbols described case rooms, comprising only '#'and '.'.

Output Format

Output an integer representing the number of the room.

SAMPLE INPUT

3 3
#.#
#.#
.#.

Sample Output

3

Topic analysis

Analysis of the meaning of problems

In a rectangle of N × M pieces identify communication region. Standard BFS topic.

P.S.

This problem is a communication problem is determined, and the search may be used to solve the set.

Sample data analysis

Nothing special to be analyzed, to paraphrase BFS template on it.

Range of data analysis

Despise about the topic and who did not give a clear range of M and N, n + 1 times I made mistakes, and finally tested to obtain N, M ≤ 2000.

Algorithm thinking

The core matrix is ​​traversed from start to finish, if a position (x, y), if the point is., And has not been visited, with regard to the starting point once BFS, number of rooms plus a complete search. Until the entire matrix traversal complete.

Such an algorithm complexity O (n ^ {2}). According to the most unfavorable principle, in the worst case, the matrix half of the room, half walls, and each room has only one point. Sample the following data:

6 6
. # . # . #
# . # . # .
. # . # . #
# . # . # .
. # . # . #
# . # . # .

That is, the maximum amount of data processing is data 2000 * 2000/2 = 2e6 is.

The current violence in the search algorithm garlic passenger count has passed.

Reference Code

Violence Code Search

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2002;
const int MAXM = MAXN;

typedef struct _POS {
    int x;
    int y;
} POS;

typedef struct _ROOM {
    int n;
    int m;
    char data[MAXN][MAXM];
    bool vis[MAXN][MAXM];
} ROOM;

void bfs(ROOM &room, int x, int y) {
    queue<POS> q;
    POS cur = {x,y};
    q.push(cur);
    room.vis[cur.x][cur.y]=true;

    POS next;
    const POS moves[] = {{0,-1}, {1,0}, {0,1}, {-1,0}};

    while (false==q.empty()) {
        cur = q.front();
        q.pop();

        for (int i=0; i<4; i++) {
            next.x = cur.x+moves[i].x;
            next.y = cur.y+moves[i].y;

            if (next.x>=0&&next.x<room.n
                &&next.y>=0&&next.y<room.m
                &&'.'==room.data[next.x][next.y]
                &&false==room.vis[next.x][next.y]) {
                room.vis[next.x][next.y]=true;
                q.push(next);
            }
        }
    }
}

int main() {
    ROOM room = {};
    cin>>room.n>>room.m;

    for (int i=0; i<room.n; i++) {
        for (int j=0; j<room.m; j++) {
            cin>>room.data[i][j];
        }
    }

    int ans=0;
    for (int i=0; i<room.n; i++) {
        for (int j=0; j<room.m; j++) {
            if ('.'==room.data[i][j] && false==room.vis[i][j]) {
                //房间
                bfs(room, i, j);
                ans++;
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}

Pruning optimization algorithm

Violence is on the basis of the search, to optimize the code. We can see that most of the core code is n * m search for this violence. If we saved all the rooms, and then search on the preservation of the queue. Such an approach is not the time to reduce the complexity of the algorithm, but reduced the number of searches, pruning techniques Bale.

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int MAXN = 2e3+4;
const int MAXM = 2e3+4;

typedef struct _POS {
    int x;
    int y;
} POS;

typedef struct _ROOM {
    int n;
    int m;
    char data[MAXN][MAXM];
    bool vis[MAXN][MAXM];
} ROOM;

vector<POS> myvect;

void bfs(ROOM &room, const POS &pos) {
    queue<POS> q;
    q.push(pos);
    room.vis[pos.x][pos.y]=true;

    const POS moves[] = {{0,-1}, {1,0}, {0,1}, {-1,0}};
    POS cur;
    POS next;

    while (false==q.empty()) {
        cur = q.front();
        q.pop();

        for (int i=0; i<4; i++) {
            next.x = cur.x+moves[i].x;
            next.y = cur.y+moves[i].y;

            if (next.x>=0&&next.x<room.n
                &&next.y>=0&&next.y<room.m
                &&'.'==room.data[next.x][next.y]
                &&false==room.vis[next.x][next.y]) {
                room.vis[next.x][next.y]=true;
                q.push(next);
            }
        }
    }
}

int main() {
    ROOM room = {};
    cin>>room.n>>room.m;

    char data;
    for (int i=0; i<room.n; i++) {
        for (int j=0; j<room.m; j++) {
            cin>>room.data[i][j];
            if ('.'==room.data[i][j]) {
                POS pos = {i,j};
                myvect.push_back(pos);
            }
        }
    }

    int ans=0;
    vector<POS>::iterator it;
    for (it=myvect.begin(); it<myvect.end(); it++) {
        POS pos = *it;
        if (false==room.vis[pos.x][pos.y]) {
            bfs(room, pos);
            ans++;
        }
    }

    cout<<ans<<endl;

    return 0;
}

 

Published 235 original articles · won praise 289 · Views 1.07 million +

Guess you like

Origin blog.csdn.net/justidle/article/details/104855000