1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string.h>
 
typedef struct node{
    node* Next;
    int a, b;
 
    node() {
        Next = nullptr;
        a = 0;
        b = 0;
    }
}Node;
 
typedef struct stack {
 
    Node* Head;
    int length;
    stack() {
        Head = nullptr;
        length = 0;
    }
    bool isEmpty() {
        if (!length) return true;
        return false;
    }
    void push_back(int a, int b) {
        Node* temp = new Node;
        temp->= a;
        temp->= b;
        if (!Head) {
            Head = temp;
        }
        else {
            Node* temp2 = Head;
            while (temp2->Next) { temp2 = temp2->Next; }
            temp2->Next = temp;
        }
        length++;
    }
    void pop_up() {
        if (isEmpty()) return;
        if (length == 1) {
            Head = nullptr;
        }
        else {
            Node* temp = Head;
            while (temp->Next) { temp = temp->Next;}
            delete[] temp;
        }
        length--;
    }
    int* back() {
        if (isEmpty()) return nullptr;
        int result[2= { 0 };
        if (length == 1) {
            result[0= Head->a;
            result[1= Head->b;
            Head = nullptr;
        }
        else {
            Node* temp = Head;
            while (temp->Next) { temp = temp->Next; }
            result[0= temp->a;
            result[1= temp->b;
            delete[] temp;
        }
        return result;
    }
}Stack;
 
void checkMaze(Stack* stackint** map, int size ,const int* escape) {
    int temp[2];
    memcpy(temp, stack->back(), sizeof(int* 2);
    if (temp[0< 0 || temp[0>= size || temp[1< 0 || temp[1>= size) {
        stack->pop_up();
        return;
    }
    if (temp[0== escape[0&& temp[1== escape[1]) {
        printf("성공");
        return;
    }
    ;
    if (map[temp[0]][temp[1- 1== 1 || map[temp[0]][temp[1]] != 2) {
        stack->push_back(temp[0], temp[1- 1);
        checkMaze(stack, map, size, escape);
    }
    if (map[temp[0]+1][temp[1]] == 1 || map[temp[0]][temp[1]] != 2) {
        stack->push_back(temp[0+ 1, temp[1]);
        checkMaze(stack, map, size, escape);
    }
 
    if (map[temp[0]][temp[1+ 1== 1 || map[temp[0]][temp[1]] != 2) {
        stack->push_back(temp[0], temp[1+ 1);
        checkMaze(stack, map, size, escape);
    }
    if (map[temp[0- 1][temp[1]] == 1 || map[temp[0]][temp[1]] != 2) {
        stack->push_back(temp[0- 1, temp[1]);
        checkMaze(stack, map, size, escape);
    }
    stack->pop_up();
}
 
int main() {
    int size;
    printf("미로의 전체 크기를 지정해주세요");
 
    scanf("크기 : %d"&size);
 
    //이중포인터 할당
    int** map = new int*[size];
    for (int i = 0; i < size; i++) {
        map[i] = new int[size];
    }
    
    printf("갈 수 있는 통로의 갯수들을 입력하세요");
    int root = 0;
    scanf("%d"&root);
    int m, n;
    for (int i = 0; i < root; i++) {
        scanf("%d%d"&m, &n);
        map[m][n] = 1;
    }
    
    /*
        스택은 만들었고 위아래 좌우 확인해서 0이면 못가고 1이면 갈수있다.
        그리고 1이였는데 그 지역으로 갔을시 map데이터를 0으로 이동
        위아래 양옆 막혔을지
        인덱스 :(1,0) 실제:(2,1);
        들어가는곳 (4,4)
    */
    Stack maze;
    maze.push_back(10);
    int escape[2= { 44 };
    checkMaze(&maze, map, size,escape);
    //이중포인터 해제
    for (int i = 0; i < size; i++) {
        delete[] map[i];
    }
    delete[] map;
    return 0;
}
cs


+ Recent posts