-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiro.cpp
82 lines (65 loc) · 1.55 KB
/
miro.cpp
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
// 미로 탐색
// issue1: 한줄에 숫자를 한꺼번에 받으면서 100자리 숫자를 받게 될 경우 int로 값을 처리할 수 없다
// 해결방법: char 배열로 한줄 입력을 받고 인덱스로 접근해 한자리씩 int 배열에 변환하여 넣어준다.
//
// 더 좋은 해결 방법: scanf(“%1d”,&num)을 이용하여 한 자리씩 받음
//
// issue2: 최솟값을 출력해야 하는데 여러 경로를 고려하면서 최대값이 출력된다.
// 해결방법 : BTS 함수 안에 flag 불 변수를 만들어 한 번 도착지에 도착하면 함수를 끝낸다.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct location {
int x, y;
};
int N, M;
int board[110][110];
int dx[4] = { -1, 1, 0, 0};
int dy[4] = { 0, 0, 1, -1};
int Max = 0;
queue<location> q;
void BTS();
int main() {
cin >> N >> M;
char temp[110][110];
for (int i = 0; i < N; i++) {
cin >> temp[i];
}
for (int i = 0; i < N; i++) {
for (int j = M - 1; j >= 0; j--) {
board[i][j] = temp[i][j] - '0'; // 문자인 숫자를 정수형으로 변환
}
}
q.push({ 0,0 });
BTS();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] > Max) {
Max = board[i][j];
}
}
}
cout << Max;
cin >> Max;
}
void BTS() {
bool flag = 1; // 최솟값을 위한 불 변수 추가
while (!q.empty() && flag) {
location temp = q.front();
for (int i = 0; i < 4; i++) {
int nx = temp.x + dx[i];
int ny = temp.y + dy[i];
if (nx == N - 1 && ny == M - 1) {
flag = false;
}
if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
if (board[nx][ny] == 1) {
board[nx][ny] = board[temp.x][temp.y] + 1;
q.push({ nx,ny });
}
}
}
q.pop();
}
}