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
- c++
- 백준
- 정렬
- BFS
- 백트래킹
- 그래프
- ue5
- 알고리즘
- 시뮬레이션
- 수학
- 브루트포스
- 누적 합
- 우선순위 큐
- 자료구조
- 투 포인터
- 구현
- 재귀
- 다익스트라
- 유니티
- 스택
- XR Interaction Toolkit
- 다이나믹 프로그래밍
- 문자열
- 유니온 파인드
- DFS
- Unreal Engine 5
- Team Fortress 2
- 트리
- 그리디 알고리즘
- VR
Archives
- Today
- Total
1일1알
백준 4577번 소코반 C++ 본문
문제에 써있는대로 구현하였다.
#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[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int target_cnt = 0;
int goal_cnt = 0;
vector<vector<char>> board(15, vector<char>(15));
pair<int, int> Move(int row, int col, int dir) {
int nextRow = row + dRow[dir];
int nextCol = col + dCol[dir];
if (board[nextRow][nextCol] == '#') return { row, col };
if (board[nextRow][nextCol] == 'b' || board[nextRow][nextCol] == 'B') {
int nnRow = nextRow + dRow[dir];
int nnCol = nextCol + dCol[dir];
if (board[nnRow][nnCol] == 'b' || board[nnRow][nnCol] == 'B' || board[nnRow][nnCol] == '#') {
return { row,col };
}
if (board[row][col] == 'w') {
if (board[nextRow][nextCol] == 'b') {
board[row][col] = '.';
board[nextRow][nextCol] = 'w';
if (board[nnRow][nnCol] == '.') {
board[nnRow][nnCol] = 'b';
}
else {
board[nnRow][nnCol] = 'B';
goal_cnt++;
}
}
else if (board[nextRow][nextCol] == 'B') {
board[row][col] = '.';
board[nextRow][nextCol] = 'W';
if (board[nnRow][nnCol] == '.') {
board[nnRow][nnCol] = 'b';
goal_cnt--;
}
else {
board[nnRow][nnCol] = 'B';
}
}
}
else if (board[row][col] == 'W') {
if (board[nextRow][nextCol] == 'b') {
board[row][col] = '+';
board[nextRow][nextCol] = 'w';
if (board[nnRow][nnCol] == '.') {
board[nnRow][nnCol] = 'b';
}
else {
board[nnRow][nnCol] = 'B';
goal_cnt++;
}
}
else if (board[nextRow][nextCol] == 'B') {
board[row][col] = '+';
board[nextRow][nextCol] = 'W';
if (board[nnRow][nnCol] == '.') {
board[nnRow][nnCol] = 'b';
goal_cnt--;
}
else {
board[nnRow][nnCol] = 'B';
}
}
}
}
else if (board[nextRow][nextCol] == '.') {
if (board[row][col] == 'w') {
board[row][col] = '.';
board[nextRow][nextCol] = 'w';
}
else {
board[row][col] = '+';
board[nextRow][nextCol] = 'w';
}
}
else {
if (board[row][col] == 'w') {
board[row][col] = '.';
board[nextRow][nextCol] = 'W';
}
else {
board[row][col] = '+';
board[nextRow][nextCol] = 'W';
}
}
return { nextRow,nextCol };
}
void Simulation(int row, int col, string moveStr) {
int currRow = row;
int currCol = col;
for (int i = 0; i < moveStr.length(); i++) {
pair<int, int> nextPos;
if (moveStr[i] == 'U') nextPos = Move(currRow, currCol, 0);
else if (moveStr[i] == 'R') nextPos = Move(currRow, currCol, 1);
else if (moveStr[i] == 'D') nextPos = Move(currRow, currCol, 2);
else if (moveStr[i] == 'L') nextPos = Move(currRow, currCol, 3);
currRow = nextPos.first;
currCol = nextPos.second;
if (target_cnt == goal_cnt) break;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int cnt = 0;
while (true) {
int r, c;
cin >> r >> c;
if (r == 0 && c == 0) break;
cnt++;
int startRow = -1;
int startCol = -1;
vector<pair<int, int>> targets;
for (int i = 0; i < r; i++) {
string str;
cin >> str;
for (int j = 0; j < c; j++) {
board[i][j] = str[j];
if (str[j] == 'w' || str[j] == 'W') {
startRow = i;
startCol = j;
}
if (str[j] == '+' || str[j] == 'B' || str[j] == 'W') {
if (str[j] == 'B') goal_cnt++;
targets.push_back({ i,j });
target_cnt++;
}
}
}
string moveStr;
cin >> moveStr;
Simulation(startRow, startCol, moveStr);
cout << "Game " << cnt << ": ";
if (target_cnt == goal_cnt) cout << "complete" << "\n";
else cout << "incomplete" << "\n";
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << board[i][j];
}
cout << "\n";
}
target_cnt = 0;
goal_cnt = 0;
}
};
'알고리즘' 카테고리의 다른 글
백준 1987번 알파벳 C++ (0) | 2022.02.27 |
---|---|
백준 2748번 피보나치 수 2 C++ (0) | 2022.02.26 |
백준 11066번 파일 합치기 C++ (0) | 2022.02.23 |
백준 23029번 시식 코너는 나의 것 C++ (0) | 2022.02.22 |
백준 9252번 LCS 2 C++ (0) | 2022.02.21 |