ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C Language The number of islands

2021-10-15 20:02:29  阅读:254  来源: 互联网

标签:matrix Language int number coord current Coord islands data


C Language The number of islands

Assignment #7

  1. From input file, “input.txt”, read a map, which consists of ‘0’: sea, ‘1’: land, and
    print the number of the isolated lands. Land is connected only if there is another
    land in left, right, top, bottom. Land in diagonal direction is NOT connected.
    • Input
    • input.txt in the same directory with binary file.
    • The first line of input file: two positive integers (width and height of the map)
    • width <= 1024, height <=1024
    • From the second line: map consists of 0 and 1
    • Output: (on screen) the number of isolated lands
    • Tip
    • Use one of the section learn this lecture
    • Files to submit (only source code)
    • Source code: EEE2017_ID_07_01.c
    • No binary

demo.jpg

input.txt

10 10
1 1 0 1 1 1 0 0 1 0
1 1 1 0 1 1 0 0 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 1 1 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 1 1 0 0 1 1 1 1 1
0 1 1 0 0 0 0 1 1 1
1 1 1 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 0 0

地图中,0表示海洋,1表示岛屿,算地图上的岛屿数量,以下是代码实现:

/*************************
* Assignment #7
* QQ <1561968262>
*************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct _tCoord {
    int x;
    int y;
} Coord;

typedef struct _tStack {
    Coord* data;
    int len;
} Stack;

Stack initStack(int capacity) {
    Stack s;
    memset(&s, 0, sizeof(s));
    s.data = (Coord*)malloc(sizeof(Coord) * capacity);
    return s;
}

Coord* topStack(Stack* s) {
    if (s->len == 0) {
        return NULL;
    }
    return &s->data[s->len - 1];
}

void clearStack(Stack* s) {
    s->len = 0;
}

void pushStack(Stack* s, Coord coord) {
    s->data[s->len++] = coord;
}

void popStack(Stack* s) {
    --s->len;
}

typedef struct _tMatrix {
    int** data;
    int width;
    int height;
} Matrix;

Matrix initMatrix(int width, int height) {
    int i;
    Matrix m;
    memset(&m, 0, sizeof(m));
    m.data = (int**)malloc(sizeof(int*) * height);
    for (i = 0; i < height; ++i) {
        m.data[i] = (int*)malloc(sizeof(int) * width);
    }
    m.width = width;
    m.height = height;
    return m;
}

Matrix readFile() {
    Matrix m = {0};
    FILE* input = fopen("input.txt", "r");
    if (input) {
        int w, h;
        if (fscanf(input, "%d%d", &w, &h) == 2) {
            m = initMatrix(w, h);
            int i, j;
            for (i = 0; i < m.height; ++i) {
                for (j = 0; j < m.width; ++j) {
                    fscanf(input, "%d", &m.data[i][j]);
                }
            }
            return m;
        }
    } else {
        printf("open file faild!\n");
    }
    return m;
}

void printFile(Matrix* m) {
    int i, j;
    printf("--------\n");
    for (i = 0; i < m->height; ++i) {
        for (j = 0; j < m->width; ++j) {
            printf("%d ", m->data[i][j]);
        }
        printf("\n");
    }
}

int findNewIsland(Matrix* m, Coord* coord) {
    int i, j;
    for (i = 0; i < m->height; ++i) {
        for (j = 0; j < m->width; ++j) {
            if (m->data[i][j] == 1) {
                coord->x = j;
                coord->y = i;
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    Matrix matrix = readFile();
    //printFile(&matrix);
    Coord begin;
    Stack stack = initStack(matrix.width * matrix.height);
    int count = 0;
    while (findNewIsland(&matrix, &begin)) {
        ++count;
        clearStack(&stack);
        pushStack(&stack, begin);
        matrix.data[begin.y][begin.x] = 0;
        while (topStack(&stack)) {
            Coord current = *topStack(&stack);
            popStack(&stack);
            //go left
            if (current.x > 0 && matrix.data[current.y][current.x - 1] != 0) {
                matrix.data[current.y][current.x - 1] = 0;
                Coord coord = current;
                --coord.x;
                pushStack(&stack, coord);
            }
            //go right
            if (current.x < matrix.width - 1 && matrix.data[current.y][current.x + 1] != 0) {
                matrix.data[current.y][current.x + 1] = 0;
                Coord coord = current;
                ++coord.x;
                pushStack(&stack, coord);
            }
            //go up
            if (current.y > 0 && matrix.data[current.y - 1][current.x] != 0) {
                matrix.data[current.y - 1][current.x] = 0;
                Coord coord = current;
                --coord.y;
                pushStack(&stack, coord);
            }
            //go down
            if (current.y < matrix.height - 1 && matrix.data[current.y + 1][current.x] != 0) {
                matrix.data[current.y + 1][current.x] = 0;
                Coord coord = current;
                ++coord.y;
                pushStack(&stack, coord);
            }
        }
    }
    //printFile(&matrix);
    printf("%d\n", count);
    return 0;
}

标签:matrix,Language,int,number,coord,current,Coord,islands,data
来源: https://blog.csdn.net/qq_35960743/article/details/120789710

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有