-
[21772] 가희의 고구마 먹방BOJ 2021. 12. 5. 13:43
https://www.acmicpc.net/problem/21772
21772번: 가희의 고구마 먹방
첫 번째 줄에 맵의 세로 크기 R, 가로 크기 C, 가희가 이동하는 시간 T가 주어집니다. 두 번째 줄부터 R+1번째 줄까지 길이가 C인 문자열이 주어집니다. 주어지는 문자열에 있는 문자는 가희를
www.acmicpc.net
<문제>
중복 방문 허용/같은 위치의 고구마를 중복으로 세는 경우를 주의해야한다.
입력이 작으므로 5방향으로 구현해도 무리가 없다.
<소스코드>
123456789101112131415161718192021222324252627282930313233343536373839404142#include <bits/stdc++.h>using namespace std;int n, m, t, sy, sx, ans = -1, dy[5] = {0, 1, 0, -1, 0},dx[5] = {1, 0, -1, 0, 0};char a[103][103];bool visited[101][101];void f(int y, int x, int turn, int cnt) {if (turn == t) {ans = max(cnt, ans);return;}int i;for (i = 0; i < 5; i++) {int ny = y + dy[i];int nx = x + dx[i];if (ny < 0 || nx < 0 || ny >= n || nx >= m) continue;if (a[ny][nx] == '#') continue;if (a[ny][nx] == 'S') {a[ny][nx] = '.';f(ny, nx, turn + 1, cnt + 1);a[ny][nx] = 'S';} elsef(ny, nx, turn + 1, cnt);}}int main(void) {ios_base::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> t;int i, j;for (i = 0; i < n; i++)for (j = 0; j < m; j++) {cin >> a[i][j];if (a[i][j] == 'G') {sy = i;sx = j;}}f(sy, sx, 0, 0);cout << ans;return 0;}cs 'BOJ' 카테고리의 다른 글
[3163] 떨어지는 개미 (0) 2021.12.07 [20944] 팰린드롬 척화비 (0) 2021.12.07 [1459] 걷기 (0) 2021.12.04 [3018] 캠프파이어 (0) 2021.12.03 [1100] 하얀 칸 (0) 2021.12.03