岛屿问题Number of Islands

**

岛屿问题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;
    else
        return 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;
    else
        return 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a soldiers

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值