그럴듯한 개발 블로그
Published 2023. 9. 21. 14:15
백준 12100 2048(Easy) c++ <algorithm>/백준
반응형

https://www.acmicpc.net/problem/12100

 

12100번: 2048 (Easy)

첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2

www.acmicpc.net

전 문제 스티커돌리기에서 배열 돌리는 함수 빼와서 금방 풀었다. 아니였으면 dir마다 함수 구현하고 있었을듯 극혐...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <deque>
#include <unordered_set>
#include <cstring>
#include <utility>
#include <climits> // int32max

using namespace	std;

int len, res = 2;
vector<vector<int> > board;

int getLargeNum() {
	int res = 0;
	for (int i = 0; i < board.size(); i++)
		for (int j = 0; j < board[0].size(); j++)
			res = max(res, board[i][j]);
	return res;
}
vector<vector<int> > getTurnedBoard(vector<vector<int> > b) {
	int col = b[0].size(), row = b.size();

	vector<vector<int> > res(col, vector<int>(row, 0));
	for (int x = 0; x < col; x++) {
		for (int y = 0; y < row; y++) {
			res[x][y] = b[row - y - 1][x];
		}
	}
	return res;
}

void swipe() { // 왼쪽으로 밀겠음 왜냐? 생각하기 편하니까!
	for (int x = 0; x < len; x++) {
		for (int y = 0; y < len - 1; y++) {
			int diff = 1;
			while (y + diff < len && board[x][y + diff] == 0)
				diff++; // 중간에 0 들어있을 수 있걸랑
			if (board[x][y] == board[x][y + diff]) {
				board[x][y] *= 2; // 같은 숫자 다 합쳐줌
				board[x][y + diff] = 0;
			}
			y += diff - 1;
		}
		vector<int> v;
		for (int y = 0; y < len; y++)
			if (board[x][y] != 0)
				v.push_back(board[x][y]);
		for (int y = 0; y < len; y++) {
			if (y < v.size())
				board[x][y] = v[y];
			else
				board[x][y] = 0;
		}
	}
}

void	dfs(int depth) {
	if (depth == 5) {
		res = max(res, getLargeNum());
		return ;
	}
	for (int i = 0; i < 4; i++) { // 4방향
		vector<vector<int> > backup(board);
		swipe();
		dfs(depth + 1);
		board = getTurnedBoard(backup);
	}
}

int	main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	cin >> len;
	vector<vector<int> > v(len, vector<int>(len));
	board = v;
	for (int i = 0; i < len; i++)
		for (int j = 0; j < len; j++)
			cin >> board[i][j];
	dfs(0);
	cout << res;
}
반응형

'<algorithm> > 백준' 카테고리의 다른 글

백준 11047 동전 0 c++  (0) 2023.09.23
백준 15686 치킨 배달 c++  (0) 2023.09.22
백준 18808 스티커 붙이기 c++  (0) 2023.09.21
백준 15683 감시 c++  (0) 2023.09.18
백준 1107 리모컨 c++  (0) 2023.09.17
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!