1일1알

백준 4577번 소코반 C++ 본문

알고리즘

백준 4577번 소코반 C++

영춘권의달인 2022. 2. 25. 13:40

출처 : https://www.acmicpc.net/problem/4577

문제에 써있는대로 구현하였다.

 

#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