当前位置: 代码迷 >> 综合 >> 岛屿问题Number of Islands
  详细解决方案

岛屿问题Number of Islands

热度:44   发布时间:2023-12-22 05:30:57.0

**

岛屿问题Number of Islands

**

Problem description
Given a 2d mm * nn grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.(1≤m≤1000, 1≤n≤1000)
Input
Line 1: m and n.
Line 2: ‘1’ or ‘0’ in grid and split by spaces
Output
Line 1: number of islands.
Sample Input 1
4 5
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0
Sample Output 1
1
Sample Input 2
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
Sample Output 2
3

方法一:

#include <stdio.h>
#include <stdlib.h>
#define MAXLENGTH 100000
void num_of_islands(int row,int col,int m, int n,char *islands);
int numIslands(char *islands,int row,int col);
void push(int num);
int pop();
int isnull();
char visit(int m,int n,int row,int col,char *islands);struct pstack
{
    int top;int buf[MAXLENGTH];};struct pstack pstack;
void push(int num)
{
    if (pstack.top >= MAXLENGTH -1){
    printf("stack over");return ;}pstack.buf[pstack.top++] = num;
}
int pop()
{
    if(pstack.top <= 0){
    printf("stack is null");return 0;}--pstack.top;return pstack.buf[pstack.top];
}
int isnull()
{
    if (pstack.top == 0)return 1;elsereturn 0;
}int main()
{
    int row,col,i,j;pstack.top = 0;scanf("%d",&row);scanf("%d",&col);char  *islands = (char *)malloc(sizeof(char) * row * col);if(islands == NULL)return 0;for(i = 0;i < row ;++i)for(j = 0;j < col; ++j){
    getchar();scanf("%c",&islands[i * col + j]);}numIslands(islands,row,col);free(islands);
}
int numIslands(char *islands,int row,int col)
{
    int i,j;int num = 0;for(i = 0;i < row ;++i)for(j = 0;j < col; ++j){
    if( visit(i,j,row,col,islands)== '1'){
    num_of_islands(row,col,i,j,islands);++num;}}printf("%d",num) ;return num;
}void num_of_islands(int row,int col,int m, int n,char *islands)
{
    int location;islands[m * col + n] = '2';if(visit(m-1,n,row,col,islands)=='1'){
    islands[(m - 1)* col + n] == '2';push ((m - 1)* col + n);}if(visit(m,n-1,row,col,islands)=='1'){
    islands[m * col + n - 1] == '2';push(m * col + n - 1);}if(visit(m+1,n,row,col,islands)=='1'){
    islands[(m + 1)* col + n] == '2';push((m + 1)* col + n);}if(visit(m,n+1,row,col,islands)=='1'){
    islands[m * col + n + 1] == '2';push(m * col + n + 1);}while(!isnull()){
    location = pop();m = location/col;n = location % col;islands[location] = '0';if(visit(m-1,n,row,col,islands)=='1'){
    islands[(m - 1)* col + n] == '2';push ((m - 1)* col + n);}if(visit(m,n-1,row,col,islands)=='1'){
    islands[m * col + n - 1] == '2';push(m * col + n - 1);}if(visit(m+1,n,row,col,islands)=='1'){
    islands[(m + 1)* col + n] == '2';push((m + 1)* col + n);}if(visit(m,n+1,row,col,islands)=='1'){
    islands[m * col + n + 1] == '2';push(m * col + n + 1);}}
}char visit(int m,int n,int row,int col,char *islands)
{
    if(m < 0 || n < 0 || m >= row || n >= col)return 0;elsereturn islands[m * col + n];
}

方法二:需要的内存有点多

#include <stdio.h>char map[1000][1000];
int n, m;
int total;void GetMap() {
    scanf("%d %d\n", &n, &m);int i,j;for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) {
    scanf("%c", &map[i][j]);   getchar();}}
}void Search(int x, int y) {
    if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '0') {
    return;}map[x][y] = '0';Search(x - 1, y);Search(x, y - 1);Search(x, y + 1);Search(x + 1, y);
}void GetResult() {
    int i,j;for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) {
    if (map[i][j] == '1') {
    ++total;Search(i, j);}}}
}int main()
{
    GetMap();GetResult();printf("%d\n", total);return 0;
}
  相关解决方案