Total passenger garlic problem solution --T1769: the largest island

Related topics

Topic Link

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

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

Title Description

Mysterious ocean, thrilling adventure of the road, salvage undersea treasure, fierce naval battles, pirates rob the rich, and so on. Pirates of the Caribbean, you know? Captain Jack drove his warships of the Black Pearl pirate 1 will conquer each island, and finally become a pirate king.

This is a by the sea, islands and pirate composed dangerous world. Face the perils of the ocean and treacherous adversary, how relying on their wisdom and luck, to build a powerful pirate empire.

Captain Jack has a chart on hand throughout the waters, dense distribution of the location and size of each island above the sea. He wanted to know how much the entire island and the largest island waters of the total area as soon as possible.

Input Format

Line 11: M, N, T, represents the area size of length, width and a unit represented by the sea

Then there are M rows, each row and wherein the sequence of N interspersed space 01 thereof. 0 for water, 1 represents a land where useless spaces can be ignored.

Output Format

Output line, there are two integers, a blank interval, represents the whole number of the islands of the sea, and the maximum area of ​​the island.

SAMPLE INPUT

8 16 99
00000000 00000000
0000110011000000
0001111000111000
0000000  00 0000000
00111  111000001  10
001110000  0000000
0100001111 111100
0000000000000000

Sample Output

5 990

data range

1 ≤ N, M, T ≤500。

Explanation

1: If a land one of eight directions (up, down, left, right, upper left, upper right, lower left, lower right) is the location of the land is considered the same island.

2: Suppose the first row, the last row, first column, last column are all zero.

Topic analysis

Analysis of the meaning of problems

1 configured to identify a number of islands in the region, and calculates the maximum area of ​​the island.

Therefore, according to the meaning of problems, we can know that this is a standard problem BFS topic. Of course, this question can be used DFS, but the best way is to BFS, DFS because recursion depth is too deep will cause the program to burst stack.

Special Note

1, when the present search topic path is eight directions, i.e., upper, lower, left, right, upper left, upper right, lower left, lower right.

2, there may be a space between the input data, to ignore this space.

Sample data analysis

Templates and other similar questions, there is no particular need of attention. Here we use the following figure to do a simple explanation:

From the figure, we can clearly be seen, a total of five islands, the largest island of the composition 1 10, according to the meaning of the subject, each represents an area 99, the maximum area of ​​the island 990.

Algorithm thinking

1, data is read, the processing space.

2, all of the vector was added to the 1 position.

3, in order to coordinate all the vector as a starting point, a BFS, record every search area, search finished recording the number of islands.

4, the output.

AC reference code

BFS Code

#include <iostream>
#include <queue>
#include <vector>
 
using namespace std;
 
const int MAXN = 500+4;
const int MAXM = 500+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;
 
int 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,-1}, {1,0}, {1,1}, {0,1}, {-1,1}, {-1,0}, {-1,-1}};
    POS cur;
    POS next;
    int steps = 0;
 
    while (false==q.empty()) {
        steps++;
        cur = q.front();
        q.pop();
 
        for (int i=0; i<8; 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
                &&'1'==room.data[next.x][next.y]
                &&false==room.vis[next.x][next.y]) {
                room.vis[next.x][next.y]=true;
                q.push(next);
            }
        }
    }
    return steps;
}
 
int main() {
    int t;
    ROOM room = {};
    cin>>room.n>>room.m>>t;
 
    char data;
    for (int i=0; i<room.n; i++) {
        for (int j=0; j<room.m; ) {
            cin>>room.data[i][j];
            if (' '!=room.data[i][j])  {
                if ('1'==room.data[i][j]) {
                    POS pos = {i,j};
                    myvect.push_back(pos);
                }
                j++;
            }
        }
    }
 
    int nums=0;
    int area=0;
    vector<POS>::iterator it;
    for (it=myvect.begin(); it<myvect.end(); it++) {
        POS pos = *it;
        if (false==room.vis[pos.x][pos.y]) {
            area = max(area, bfs(room, pos));
            nums++;
        }
    }
 
    cout<<nums<<" " << area*t <<endl;
 
    return 0;
}

DFS Code

#include <iostream>
#include <queue>
#include <vector>
 
using namespace std;
 
const int MAXN = 500+4;
const int MAXM = 500+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; 

int steps;

int dfs(ROOM &room, const POS &pos) {
    const POS moves[] = {{0,-1}, {1,-1}, {1,0}, {1,1}, {0,1}, {-1,1}, {-1,0}, {-1,-1}};
    POS next;

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

        if (next.x>=0&&next.x<room.n
            &&next.y>=0&&next.y<room.m
            &&'1'==room.data[next.x][next.y]
            &&false==room.vis[next.x][next.y]) {
            room.vis[next.x][next.y]=true;
            steps++;
            dfs(room, next);
        }
    }
}
 
int main() {
    int t;
    ROOM room = {};
    cin>>room.n>>room.m>>t;
 
    char data;
    for (int i=0; i<room.n; i++) {
        for (int j=0; j<room.m; ) {
            cin>>room.data[i][j];
            if (' '!=room.data[i][j])  {
                if ('1'==room.data[i][j]) {
                    POS pos = {i,j};
                    myvect.push_back(pos);
                }
                j++;
            }
        }
    }
 
    int nums=0;
    int area=0;
    vector<POS>::iterator it;
    for (it=myvect.begin(); it<myvect.end(); it++) {
        POS pos = *it;
        if (false==room.vis[pos.x][pos.y]) {
            steps = 0;
            dfs(room, pos);
            area = max(area, steps);
            nums++;
        }
    }
 
    cout<<nums<<" " << area*t <<endl;
 
    return 0;
}

 

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

Guess you like

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