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
- 브루트포스
- VR
- 자료구조
- 백트래킹
- 다이나믹 프로그래밍
- 구현
- 정렬
- 트리
- BFS
- 그리디 알고리즘
- ue5
- 알고리즘
- 재귀
- 그래프
- 투 포인터
- Team Fortress 2
- 수학
- 유니온 파인드
- 유니티
- 스택
- 시뮬레이션
- DFS
- 백준
- 문자열
- XR Interaction Toolkit
- 누적 합
- 다익스트라
- Unreal Engine 5
- 우선순위 큐
- c++
Archives
- Today
- Total
1일1알
백준 14600번 샤워실 바닥 깔기 (Small) C++ 본문
k의 범위가 1~2이기때문에 바닥의 크기는 2 혹은 4이다. 바닥의 크기가 크지 않기 때문에 백트래킹을 이용해서 모든 경우를 탐색하여 문제를 해결하였다.
#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>
#include <iomanip>
using namespace std;
using ll = long long;
int dRow[8][2] = { {0,1}, {0,-1} ,{1,1} ,{1,1} ,{0,-1} ,{0,1} ,{-1,-1} ,{-1,-1} };
int dCol[8][2] = { {1,1}, {1,1}, {0,1}, {0,-1}, {-1,-1}, {-1,-1}, {0,1}, {0,-1}, };
bool isPrinted = false;
void PrintBoard(const vector<vector<int>>& board) {
for (auto a : board) {
for (auto b : a) {
cout << b << " ";
}
cout << "\n";
}
}
void BT(int num, int len, int cnt, vector<vector<int>>& board) {
if (isPrinted) return;
if (cnt == len * len - 1) {
if (!isPrinted) {
PrintBoard(board);
isPrinted = true;
}
return;
}
for (int k = 0; k < len * len; k++) {
int row = k / len;
int col = k % len;
if (board[row][col] == 0) {
for (int i = 0; i < 8; i++) {
bool isPossible = true;
for (int j = 0; j < 2; j++) {
int nextRow = row + dRow[i][j];
int nextCol = col + dCol[i][j];
if (nextRow < 0 || nextRow >= len) {
isPossible = false;
continue;
}
if (nextCol < 0 || nextCol >= len) {
isPossible = false;
continue;
}
if (board[nextRow][nextCol] != 0) {
isPossible = false;
continue;
}
}
if (isPossible) {
board[row][col] = num;
board[row + dRow[i][0]][col + dCol[i][0]] = num;
board[row + dRow[i][1]][col + dCol[i][1]] = num;
BT(num + 1, len, cnt + 3, board);
board[row][col] = 0;
board[row + dRow[i][0]][col + dCol[i][0]] = 0;
board[row + dRow[i][1]][col + dCol[i][1]] = 0;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k;
cin >> k;
int len = pow(2, k);
int x, y;
cin >> x >> y;
swap(x, y);
x = len - x;
y -= 1;
vector<vector<int>> board(len, vector<int>(len, 0));
board[x][y] = -1;
BT(1, len, 0, board);
if (!isPrinted) cout << -1;
};
'알고리즘' 카테고리의 다른 글
백준 1730번 판화 C++ (0) | 2022.02.10 |
---|---|
백준 2374번 같은 수로 만들기 C++ (0) | 2022.02.09 |
백준 1275번 커피숍2 C++ (0) | 2022.02.07 |
백준 1802번 종이 접기 C++ (0) | 2022.02.06 |
백준 6576번 쿼드 트리 C++ (0) | 2022.02.06 |