p6p
11/30/2015 - 11:57 AM

hw2.c

#include <stdio.h>
#include <stdlib.h>

struct path {
    int x, y;
    struct path *next, *previous;
};

int main() {
    int m, n;
    printf("Enter the size of the Maze ( n, m ) : ");
    scanf("%d%d", &m, &n);
    puts("Enter the maze structure :");
    
    int *data = (int*)malloc(sizeof(int)*n*m);
    for (int i=0; i<m; ++i)
        for (int j=0; j<n; ++j)
            scanf("%d", data+i*m+j);
    
    struct path s, *root = &s;
    root->x = 1;
    root->y = 1;
    root->next = NULL;
    root->previous = NULL;
    
    data[m+1] = -1;
    
    struct path *current = root;
    int sFlag = 1;
    
    
    while (current->x != m-2 || current->y != n-2) {
        if (data[(current->y-1)*m + current->x] == 0) { // top
            data[(current->y-1)*m + current->x] = -1;
            struct path *tmp = (struct path*)malloc(sizeof(tmp));
            tmp->previous = current;
            tmp->next = NULL;
            tmp->x = current->x;
            tmp->y = current->y-1;
            current->next = tmp;
            current = tmp;
        } else if (data[(current->y+1)*m + current->x] == 0) { // botom
            data[(current->y+1)*m + current->y] = -1;
            struct path *tmp = (struct path*)malloc(sizeof(tmp));
            tmp->previous = current;
            tmp->next = NULL;
            tmp->x = current->x;
            tmp->y = current->y+1;
            current->next = tmp;
            current = tmp;
        } else if (data[current->y*m + current->x-1] == 0) { // left
            data[current->y*m + current->x-1] = -1;
            struct path *tmp = (struct path*)malloc(sizeof(tmp));
            tmp->previous = current;
            tmp->next = NULL;
            tmp->x = current->x-1;
            tmp->y = current->y;
            current->next = tmp;
            current = tmp;
        } else if (data[current->y*m + current->x+1] == 0) { // right
            data[current->y*m + current->x+1] = -1;
            struct path *tmp = (struct path*)malloc(sizeof(tmp));
            tmp->previous = current;
            tmp->next = NULL;
            tmp->x = current->x+1;
            tmp->y = current->y;
            current->next = tmp;
            current = tmp;
        } else {
            if (current->x == 1 && current->y == 1) {
                sFlag = 0;
                break;
            } else {
                current = current->previous;
            }
        }
    }
    
    if (sFlag) {
        printf("yes");
        while (root!=NULL) {
            printf("(%d,%d)>", root->x, root->y);
            root = root->next;
        }
    } else {
        puts("No");
    }
}
Enter the size of the Maze ( n, m ) : 7 10
Enter the maze structure :
1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 1
1 0 1 0 1 0 1 1 0 1
1 0 1 0 1 1 1 0 0 1
1 0 1 0 0 0 0 0 1 1
1 0 0 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1
2.
Enter the size of th e Maze ( n, m ) : 6 6
Enter the maze structure :
1 1 1 1 1 1
1 0 0 1 0 1
1 1 0 0 0 1
1 1 1 1 1 1
1 1 0 0 0 1
1 1 1 1 1 1