알고리즘 문제/BOJ
2178번 미로 탐색
parkit
2018. 10. 27. 19:51
728x90
반응형
https://www.acmicpc.net/problem/2178
기본적인 BFS 문제이다.
C++로 엄청 간단하게 풀리는 문제이지만,
C언어로 구현하고싶어서, C언어로 작성하였다.
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int N = 0, M = 0; int Map[103][103] = { 0, }; int visit[103][103] = { 0, }; int dy[4] = { -1,0, 1, 0 }; int dx[4] = { 0, -1, 0, 1 }; typedef struct Queue { int y; int x; struct Queue *next; }Queue; Queue * front = NULL; Queue * back = NULL; void Push(int r, int c) { Queue * temp = (Queue*)malloc(sizeof(Queue)); temp->y = r; temp->x = c; temp->next = NULL; // 맨 처음 push if (front == NULL && back == NULL) { front = temp; back = temp; return; } // 맨 처음이 아니라면 temp->next = back; back = temp; } int Size() { // 아무것도 없을 때 if (front == NULL && back == NULL) return 0; // 1개 있을 때 if (front == back) return 1; // 여러 개 있을 때 int ret = 0; Queue * temp = NULL; temp = back; while (temp) { temp = temp->next; ++ret; } return ret; } void Pop() { // 아무것도 없을 때 if (front == NULL && back == NULL) return; // Queue안에 데이터가 1개 있을 때 if (front == back) { front = back = NULL; return; } // 여러 개 있을 때 Queue * temp = NULL; temp = back; while (temp->next != front) { temp = temp->next; } temp->next = NULL; front = temp; } int Empty() { if (front == NULL && back == NULL) return 1; else return 0; } int Fronty() { if (front == NULL && back == NULL) return -1; int ret = 0; ret = front->y; return ret; } int Frontx() { if (front == NULL && back == NULL) return -1; int ret = 0; ret = front->x; return ret; } int BFS() { visit[1][1] = 1; Push(1, 1); int ret = 1; while (Empty() == 0) // 비어 있지 않은 동안 진행 { int qSize = Size(); while (qSize--) { int r = Fronty(); int c = Frontx(); Pop(); if (r == N && c == M) return ret; for (int i = 0; i < 4; i++) { int row = r + dy[i]; int column = c + dx[i]; if (row <= 0 || row > N || column <= 0 || column > M || Map[row][column] == 0 || visit[row][column] == 1) continue; visit[row][column] = 1; Push(row, column); } } ++ret; } } int main(void) { memset(Map, 0, sizeof(Map)); memset(visit, -1, sizeof(Map)); scanf("%d %d", &N, &M); for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { scanf("%1d", &Map[i][j]); } } printf("%d\n", BFS()); return 0; } | cs |
728x90
반응형