반응형
https://www.acmicpc.net/problem/1103
1103번: 게임
줄에 보드의 세로 크기 N과 가로 크기 M이 주어진다. 이 값은 모두 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 보드의 상태가 주어진다. 쓰여 있는 숫자는 1부터 9까지의 자연수 또는
www.acmicpc.net
dfs + dp 문제
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
|
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, arr[51][51], ans;
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };
int visited[51][51];
void input();
void dfs(int x, int y, int cnt) {
// 무한번 이동가능
if (cnt > n*m) {
cout << -1;
exit(0);
}
// dp 체크
if (cnt <= visited[x][y]) return;
// 방문 처리
ans = max(ans, cnt);
visited[x][y] = cnt;
rep(i, 4) {
int nx = x + dx[i]*arr[x][y];
int ny = y + dy[i]*arr[x][y];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || arr[nx][ny] == -1) continue;
dfs(nx, ny, cnt+1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
input();
dfs(0, 0, 1);
cout << ans;
}
void input() {
cin >> n >> m;
rep(i, n) {
rep(j, m) {
char c;
cin >> c;
if (c == 'H') arr[i][j] = -1;
else arr[i][j] = c - '0';
}
}
}
|
cs |
반응형
'백준 > DP' 카테고리의 다른 글
백준 1890 (0) | 2021.07.23 |
---|---|
백준 2293 (0) | 2021.07.20 |
백준 11054 (0) | 2021.02.19 |
백준 13398 [복습 필수] (0) | 2021.02.18 |
백준 2133 [복습 필수] (점화식) (0) | 2021.02.17 |