Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 다이나믹 프로그래밍
- 알고리즘
- 구현
- 우선순위 큐
- 백트래킹
- 시뮬레이션
- 그리디 알고리즘
- 브루트포스
- BFS
- 유니티
- 문자열
- 재귀
- ue5
- 트리
- 누적 합
- 다익스트라
- VR
- DFS
- 그래프
- Team Fortress 2
- 스택
- 자료구조
- 백준
- 수학
- XR Interaction Toolkit
- 유니온 파인드
- c++
- 정렬
- 투 포인터
- Unreal Engine 5
Archives
- Today
- Total
1일1알
백준 21772번 가희의 고구마 먹방 C++ 본문
https://www.acmicpc.net/problem/21772
21772번: 가희의 고구마 먹방
첫 번째 줄에 맵의 세로 크기 R, 가로 크기 C, 가희가 이동하는 시간 T가 주어집니다. 두 번째 줄부터 R+1번째 줄까지 길이가 C인 문자열이 주어집니다. 주어지는 문자열에 있는 문자는 가희를
www.acmicpc.net
백트래킹 + bfs 를 이용해서 해결하였다. 이미 방문했던 곳이라도 다시 방문하는게 핵심인 것 같다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int r, c, t;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int ans = 0;
vector<vector<int>> board;
void BT(int row, int col, int moveCnt, int eatSp) {
if (moveCnt >= t) {
ans = max(ans, eatSp);
return;
}
for (int i = 0; i < 4; i++) {
int nextRow = row + dRow[i];
int nextCol = col + dCol[i];
if (nextRow < 0 || nextRow >= r) continue;
if (nextCol < 0 || nextCol >= c) continue;
if (board[nextRow][nextCol] == '#') continue;
if (board[nextRow][nextCol] == 'S') {
board[nextRow][nextCol] = '.';
BT(nextRow, nextCol, moveCnt + 1, eatSp + 1);
board[nextRow][nextCol] = 'S';
}
else {
BT(nextRow, nextCol, moveCnt + 1, eatSp);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c >> t;
int startRow = -1;
int startCol = -1;
board = vector<vector<int>>(r, vector<int>(c));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
char ch;
cin >> ch;
board[i][j] = ch;
if (board[i][j] == 'G') {
startRow = i;
startCol = j;
}
}
}
BT(startRow, startCol, 0, 0);
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 12018번 Yonsei TOTO C++ (1) | 2022.12.05 |
---|---|
백준 2992번 크면서 작은 수 C++ (0) | 2022.12.04 |
백준 16139번 인간-컴퓨터 상호작용 C++ (0) | 2022.12.02 |
백준 17390번 이건 꼭 풀어야 해! C++ (0) | 2022.11.30 |
백준 13022번 늑대와 올바른 단어 C++ (0) | 2022.11.29 |