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
- 문자열
- BFS
- 정렬
- 알고리즘
- 다익스트라
- Team Fortress 2
- c++
- 수학
- Unreal Engine 5
- 백트래킹
- 구현
- 그래프
- 유니티
- DFS
- 우선순위 큐
- 다이나믹 프로그래밍
- 스택
- 유니온 파인드
- 누적 합
- 그리디 알고리즘
- XR Interaction Toolkit
- 자료구조
- 투 포인터
- 트리
- 브루트포스
- VR
- 백준
- 시뮬레이션
- 재귀
- ue5
Archives
- Today
- Total
1일1알
백준 3967번 매직 스타 C++ 본문
https://www.acmicpc.net/problem/3967
3967번: 매직 스타
매직 스타의 모양이 주어진다. 수가 채워져 있지 않은 곳은 x로, 채워져 있는 곳은 'A'부터 'L'까지 알파벳으로 채워져 있다. i번째 알파벳은 숫자 i를 의미한다. '.'는 매직 스타의 형태를 만들기
www.acmicpc.net
백트래킹
#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 cnt = 0;
vector<bool> used(12, false);
vector<int> _fill;
vector<pair<int, int>> pos(12);
vector<vector<char>> board(5, vector<char>(9));
void BT(int idx, int currCnt) {
if (currCnt == cnt) {
int sum1, sum2, sum3, sum4, sum5, sum6;
sum1 = board[0][4] + board[1][3] + board[2][2] + board[3][1];
sum2 = board[0][4] + board[1][5] + board[2][6] + board[3][7];
sum3 = board[3][1] + board[3][3] + board[3][5] + board[3][7];
sum4 = board[1][1] + board[1][3] + board[1][5] + board[1][7];
sum5 = board[1][1] + board[2][2] + board[3][3] + board[4][4];
sum6 = board[1][7] + board[2][6] + board[3][5] + board[4][4];
bool ans = true;
if (sum1 != sum2) ans = false;
if (sum2 != sum3) ans = false;
if (sum3 != sum4) ans = false;
if (sum4 != sum5) ans = false;
if (sum5 != sum6) ans = false;
if (ans) {
for (auto a : board) {
for (auto b : a) {
cout << b;
}
cout << "\n";
}
exit(0);
}
return;
}
for (int i = 0; i < 12; i++) {
if (used[i]) continue;
used[i] = true;
board[pos[_fill[idx]].first][pos[_fill[idx]].second] = i + 'A';
BT(idx + 1, currCnt + 1);
used[i] = false;
board[pos[_fill[idx]].first][pos[_fill[idx]].second] = 'x';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int idx = 0;
for (int i = 0; i < 5; i++) {
string str;
cin >> str;
for (int j = 0; j < str.length(); j++) {
board[i][j] = str[j];
if (board[i][j] == '.') continue;
if (board[i][j] == 'x') {
_fill.push_back(idx);
cnt++;
}
else {
used[str[j] - 'A'] = true;
}
pos[idx++] = { i,j };
}
}
BT(0,0);
}
'알고리즘' 카테고리의 다른 글
백준 16564번 히오스 프로게이머 C++ (0) | 2023.04.16 |
---|---|
백준 11441번 합 구하기 C++ (0) | 2023.04.15 |
백준 10655번 마라톤 1 C++ (0) | 2023.04.08 |
백준 9081번 단어 맞추기 C++ (0) | 2023.04.07 |
백준 11504번 돌려 돌려 돌림판! C++ (0) | 2023.04.06 |