其他分享
首页 > 其他分享> > C Language The number of islands

C Language The number of islands

作者:互联网

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