백준 알고리즘(C++)
백준 14497번 주난의 난 ( C++ )
coding232624
2024. 8. 15. 18:09
문제
https://www.acmicpc.net/problem/14497
해설
외각을 돌며 껍질 벗기는 느낌으로 푸는 문제
visited를 초기화 해주는 방법으로 해결했음
코드
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
|
#include <bits/stdc++.h>
using namespace std;
char mp[304][304];
int ret, x1, x2, y11, y2, n, m, flag;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int visited[304][304];
void dfs(int y, int x, int visited[304][304])
{
if (y == y2 && x == x2)
{
flag = 1;
return;
}
for (int i = 0; i < 4; i++)
{
int ny = y + dy[i];
int nx = x + dx[i];
if (ny == y2 && nx == x2)
{
flag = 1;
return;
}
if (ny >= 0 && ny < n && nx >= 0 && nx < m && visited[ny][nx] == 0)
{
if (mp[ny][nx] == '0')
{
visited[ny][nx] = 1;
dfs(ny, nx, visited);
}
else if (mp[ny][nx] == '1')
{
visited[ny][nx] = 1;
mp[ny][nx] = '0';
}
else if (mp[ny][nx] == '#')
{
flag = 1;
return;
}
}
}
}
int main()
{
scanf("%d %d", &n, &m);
scanf("%d %d %d %d", &y11, &x1, &y2, &x2);
x1--;
x2--;
y11--;
y2--;
for (int i = 0; i < n; i++)
{
scanf("%s", mp[i]);
}
while (!flag)
{
int visited[304][304];
fill(&visited[0][0], &visited[303][304], 0);
visited[y11][x1] = 1;
ret++;
dfs(y11, x1, visited);
}
cout << ret;
}
|
cs |