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 |
Tags
- 누적 합
- 다이나믹 프로그래밍
- 브루트포스
- 우선순위 큐
- 그리디 알고리즘
- 유니티
- 유니온 파인드
- 다익스트라
- c++
- XR Interaction Toolkit
- ue5
- 문자열
- 스택
- 그래프
- 정렬
- 구현
- 트리
- 백준
- VR
- Team Fortress 2
- 자료구조
- 수학
- 백트래킹
- BFS
- DFS
- 재귀
- 시뮬레이션
- Unreal Engine 5
- 투 포인터
- 알고리즘
Archives
- Today
- Total
1일1알
백준 2210번 숫자판 점프 C++ 본문
1. 백트래킹으로 5번 이동하는 경우를 모두 구한다.
2. 모든 칸에 대하여 1에서 구한 이동하는 경우를 적용한다.
3. 2의 과정에서 6자리수를 만들 수 있으면 그 수를 set에 넣는다.
4. set의 size를 출력한다.
#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 <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
vector<vector<int>> board(5, vector<int>(5));
vector<int> dirV;
set<int> s;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
void Solve() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int row = i;
int col = j;
int sum = board[i][j];
bool isPossible = true;
for (int k = 0; k < dirV.size(); k++) {
int nextRow = row + dRow[dirV[k]];
int nextCol = col + dCol[dirV[k]];
if (nextRow < 0 || nextRow >= 5) {
isPossible = false;
break;
}
if (nextCol < 0 || nextCol >= 5) {
isPossible = false;
break;
}
row = nextRow;
col = nextCol;
sum = sum * 10 + board[nextRow][nextCol];
}
if (isPossible) {
s.insert(sum);
}
}
}
}
void BT(int cnt) {
if (cnt >= 5) {
Solve();
return;
}
for (int i = 0; i < 4; i++) {
dirV.push_back(i);
BT(cnt + 1);
dirV.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> board[i][j];
}
}
BT(0);
cout << s.size();
};
'알고리즘' 카테고리의 다른 글
백준 16943번 숫자 재배치 C++ (0) | 2022.01.17 |
---|---|
백준 16922번 로마 숫자 만들기 C++ (0) | 2022.01.16 |
백준 1068번 트리 C++ (0) | 2022.01.15 |
백준 5014번 스타트링크 C++ (0) | 2022.01.14 |
백준 13549번 숨바꼭질 3 C++ (0) | 2022.01.13 |