본문 바로가기
백준 알고리즘/브루트포스

백준 2048 (Easy) C++

by paysmile 2019. 10. 19.

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
const int MAX = 21;
int n;
int board[MAX][MAX];
int answer = 0;
vector<int> v;
int boardcopy[MAX][MAX];
struct Move {
	int x, y;
};
Move mv[4] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

void copyboard() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			board[i][j] = boardcopy[i][j];
		}
	}
}

void calmaxvalue() {
	int value = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			value = max(value, board[i][j]);
		}
	}
	answer = max(answer, value);
	return;
}

void bfs(int count) {
	if (count == 5){
		for (int i = 0; i < 5; i++) {
			queue<int> q;
			if (v[i] == 0) {
				for (int y = 0; y < n; y++) {
					for (int x = 0; x < n; x++) {
						if(board[x][y] != 0)
							q.push(board[x][y]);
						board[x][y] = 0;
					}
					int index = 0;
					while (!q.empty()) {
						int value = q.front();
						q.pop();

						if (board[index][y] == 0) {
							board[index][y] = value;
						}
						else if (board[index][y] == value) {
							board[index][y] = board[index][y] * 2;
							index++;
						}
						else {
							index++;
							board[index][y] = value;
						}
					}
				}
			}
			else if (v[i] == 1) {
				for (int x = 0; x < n; x++) {
					for (int y = n - 1; y >= 0; y--) {
						if (board[x][y] != 0)
							q.push(board[x][y]);
						board[x][y] = 0;
					}
					int index = n - 1;
					while (!q.empty()) {
						int value = q.front();
						q.pop();

						if (board[x][index] == 0) {
							board[x][index] = value;
						}
						else if (board[x][index] == value) {
							board[x][index] = board[x][index] * 2;
							index--;
						}
						else {
							index--;
							board[x][index] = value;
						}
					}
				}
			}
			else if (v[i] == 2) {
				for (int y = 0; y < n; y++) {
					for (int x = n-1; x >= 0; x--) {
						if (board[x][y] != 0)
							q.push(board[x][y]);
						board[x][y] = 0;
					}
					int index = n-1;
					while (!q.empty()) {
						int value = q.front();
						q.pop();

						if (board[index][y] == 0) {
							board[index][y] = value;
						}
						else if (board[index][y] == value) {
							board[index][y] = board[index][y] * 2;
							index--;
						}
						else {
							index--;
							board[index][y] = value;
						}
					}
				}
			}
			else {
				for (int x = 0; x < n; x++) {
					for (int y = 0; y < n; y++) {
						if (board[x][y] != 0)
							q.push(board[x][y]);
						board[x][y] = 0;
					}
					int index = 0;
					while (!q.empty()) {
						int value = q.front();
						q.pop();

						if (board[x][index] == 0) {
							board[x][index] = value;
						}
						else if (board[x][index] == value) {
							board[x][index] = board[x][index] * 2;
							index++;
						}
						else {
							index++;
							board[x][index] = value;
						}
					}
				}
			}
		}
		calmaxvalue();
		copyboard();
		return;
	}

	for (int i = 0; i < 4; i++) {
		v.push_back(i);
		bfs(count + 1);
		v.pop_back();
	}
	return;
}

int main(void) {
	cin >> n;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> board[i][j];
			boardcopy[i][j] = board[i][j];
		}
	}
	bfs(0);
	cout << answer << endl;
}

'백준 알고리즘 > 브루트포스' 카테고리의 다른 글

사다리 조작 C++  (0) 2021.10.22
백준 15686번 C++  (0) 2019.04.13
백준 14502번 C++  (0) 2019.04.09
백준 1018번 C++  (0) 2019.02.04
백준 14889번 C++  (0) 2019.01.28