๐Ÿ˜‰/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] 2048(Easy) (๋ฐฑ์ค€, 12100)

ruhz 2020. 11. 20. 15:38

๋ฌธ์ œ

 

12100๋ฒˆ: 2048 (Easy)

์ฒซ์งธ ์ค„์— ๋ณด๋“œ์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 20)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฒŒ์ž„ํŒ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ด์™ธ์˜ ๊ฐ’์€ ๋ชจ๋‘ ๋ธ”๋ก์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ธ”๋ก์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋Š” 2

www.acmicpc.net

 

 

ํ’€์ด

 

ruhz3/CodingTest

To prepare for coding test. Contribute to ruhz3/CodingTest development by creating an account on GitHub.

github.com

#include<iostream>   
#include<algorithm>
#include<queue>
using namespace std;

int board[20][20] = { 0 };

int N;
int maxNum = 0;

// ์ตœ๋Œ€๊ฐ’ ์ฐพ๋Š” ํ•จ์ˆ˜
int CalMax() {
	int res = 0;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			res = max(res, board[i][j]);
	return res;
}

// ์›€์ง์ด๋Š” ํ•จ์ˆ˜
void Move(int dir) {
	queue<int> Qcal;
	int prev;
	int idx;
	// 1, 2, 3, 4 = ์ƒ, ํ•˜, ์ขŒ, ์šฐ
	switch (dir) {
	case 1:
		for (int C = 0; C < N; C++) {
			idx = 0;
			// ํ์— ์—ด์˜ ๋ฌธ์ž๋“ค์„ push
			for (int R = 0; R < N; R++) {
				if (board[R][C] != 0) {
					Qcal.push(board[R][C]);
					board[R][C] = 0;
				}
			}
			// ํ์— ์•„์ง ์ˆซ์ž๊ฐ€ ๋‚จ์•˜๋‹ค๋ฉด
			while (!Qcal.empty()) {
				// 1. ๋งจ ์•ž ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ๊บผ๋‚ธ๋‹ค
				prev = Qcal.front();
				Qcal.pop();
				// 2. ํ๊ฐ€ ๋น„๊ฑฐ๋‚˜ ๊ทธ ๋’ท ์ˆซ์ž์™€ ๋‹ค๋ฅด๋ฉด ๊บผ๋‚ธ ์ˆซ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค.
				if (Qcal.empty() || prev != Qcal.front()) {
					board[idx++][C] = prev;
				}
				// 3. ๋’ท ์ˆซ์ž๊ฐ€ ๋˜‘๊ฐ™๋‹ค๋ฉด ๊ทธ๊ฒƒ๋„ ๊บผ๋‚ด๊ณ  ํ•ฉํ•œ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
				if (!Qcal.empty() && prev == Qcal.front()) {
					prev *= 2;
					Qcal.pop();
					board[idx++][C] = prev;
				}
			}
		}
		break;

	case 2:
		for (int C = 0; C < N; C++) {
			idx = N - 1;
			for (int R = N - 1; R >= 0; R--) {
				if (board[R][C] != 0) {
					Qcal.push(board[R][C]);
					board[R][C] = 0;
				}
			}
			while (!Qcal.empty()) {
				prev = Qcal.front();
				Qcal.pop();
				if (Qcal.empty() || prev != Qcal.front()) {
					board[idx--][C] = prev;
				}
				if (!Qcal.empty() && prev == Qcal.front()) {
					prev *= 2;
					Qcal.pop();
					board[idx--][C] = prev;
				}
			}
		}
		break;

	case 3:
		for (int R = 0; R < N; R++) {
			idx = 0;
			for (int C = 0; C < N; C++) {
				if (board[R][C] != 0) {
					Qcal.push(board[R][C]);
					board[R][C] = 0;
				}
			}
			while (!Qcal.empty()) {
				prev = Qcal.front();
				Qcal.pop();
				if (Qcal.empty() || prev != Qcal.front()) {
					board[R][idx++] = prev;
				}
				if (!Qcal.empty() && prev == Qcal.front()) {
					prev *= 2;
					Qcal.pop();
					board[R][idx++] = prev;
				}
			}
		}
		break;

	case 4:
		for (int R = 0; R < N; R++) {
			idx = N - 1;
			for (int C = N - 1; C >= 0; C--) {
				if (board[R][C] != 0) {
					Qcal.push(board[R][C]);
					board[R][C] = 0;
				}
			}
			while (!Qcal.empty()) {
				prev = Qcal.front();
				Qcal.pop();
				if (Qcal.empty() || prev != Qcal.front()) {
					board[R][idx--] = prev;
				}
				if (!Qcal.empty() && prev == Qcal.front()) {
					prev *= 2;
					Qcal.pop();
					board[R][idx--] = prev;
				}
			}
		}
		break;
	}
}

// ์™„์ „ ํƒ์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜
void Find(int lev){
	// ํƒˆ์ถœ ์กฐ๊ฑด
	if (lev == 5) {
		maxNum = max(maxNum, CalMax());
		return;
	}

	// ๋ฏธ๋ฆฌ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์ €์žฅ
	int tmp_board[20][20];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++)
			tmp_board[i][j] = board[i][j];
	}
	
	for (int i = 1; i <= 4; i++) {
		// ์˜ฎ๊ธฐ๊ณ  ๋‹ค์Œ ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ
		Move(i);
		Find(lev + 1);
		// ์ €์žฅํ•ด๋‘” ๋ฐฐ์—ด ๋‹ค์‹œ ๋ณต์‚ฌ
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++)
				board[i][j] = tmp_board[i][j];
		}
	}
}

int main(){
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++)
			cin >> board[i][j];
	}
	Find(0);
	cout << maxNum << endl;
	return 0;
}

โ€‹

๋ฌธ์ œ์—๋„ ์ฒจ๋ถ€๋˜์–ด ์žˆ์ง€๋งŒ, ํ•œ ๋•Œ ์œ ํ–‰ํ–ˆ๋˜ 2048๊ฒŒ์ž„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ์ด๋‹ค. https://play2048.co/

Move ํ•จ์ˆ˜ :
์–ด์ฐจํ”ผ ๋๊นŒ์ง€ ๋‹น๊ฒจ์ง€๊ธฐ ๋•Œ๋ฌธ์—, 0์ด ์•„๋‹Œ ์ˆซ์ž๋“ค์„ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋„ฃ๊ณ  ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ๋œ ํ—ท๊ฐˆ๋ฆด ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ๋ฐ€ ๋•Œ ์ˆœ์„œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ Queue๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ , Queue์—์„œ ๊บผ๋‚ด๋ฉฐ ๋‹ค์Œ์ˆซ์ž์™€ ๊ฐ™์œผ๋ฉด ๋‹ค์Œ์ˆซ์ž๋„ ๊ฐ™์ด ๊บผ๋‚ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

Find ํ•จ์ˆ˜ :
N์ด 20, ํƒ์ƒ‰ ๊นŠ์ด๊ฐ€ 5๋กœ ์ˆซ์ž๋“ค์ด ํฌ์ง€ ์•Š์•„ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ๋˜ํ•œ Moveํ•ด์„œ ์ˆซ์ž๊ฐ€ ์ค„์–ด๋“ค ๊ฒฝ์šฐ๋Š” ์—†์œผ๋ฏ€๋กœ ์ตœ๋Œ€ ๊นŠ์ด์ธ 5์—์„œ๋งŒ ์ตœ๋Œ“๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค.

โ€‹

์‚ฌ์‹ค ๊ณผ์ œ๋ฅผ ํ•  ๋•Œ๋„ ํ˜ผ์žํ•˜๋Š”๊ฒŒ ํŽธํ•  ๋•Œ๊ฐ€ ๋งŽ์€๋ฐ, ์˜ค๋Š˜์€ ํŒ€์œผ๋กœ ํ•ด์„œ ์˜ค๋ฅ˜๋„ ๋นจ๋ฆฌ ์ฐพ๊ณ  ํšจ์œจ์ ์ด์—ˆ๋‹ค. ์„œ๋กœ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋งž์•„ ๋“œ๋Š”๊ฒŒ ํŒ€์œผ๋กœ ์—ฐ์Šตํ•˜๋Š” ์˜๋„์™€ ๋งž๋Š” ๊ฒƒ ๊ฐ™์•„ ๋ฟŒ๋“ฏํ–ˆ๋‹ค.