๋ฌธ์
ํ์ด
#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์์๋ง ์ต๋๊ฐ์ ๋น๊ตํ๋ ๊ฒ์ด ํจ์จ์ ์ด๋ค.
โ
์ฌ์ค ๊ณผ์ ๋ฅผ ํ ๋๋ ํผ์ํ๋๊ฒ ํธํ ๋๊ฐ ๋ง์๋ฐ, ์ค๋์ ํ์ผ๋ก ํด์ ์ค๋ฅ๋ ๋นจ๋ฆฌ ์ฐพ๊ณ ํจ์จ์ ์ด์๋ค. ์๋ก ๊ตฌํํ ํจ์๊ฐ ๋ง์ ๋๋๊ฒ ํ์ผ๋ก ์ฐ์ตํ๋ ์๋์ ๋ง๋ ๊ฒ ๊ฐ์ ๋ฟ๋ฏํ๋ค.
'๐ > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] ๋ฑ (๋ฐฑ์ค, 3190) (0) | 2020.11.20 |
---|---|
[์๊ณ ๋ฆฌ์ฆ] โพ (๋ฐฑ์ค, 17281) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] ์ฐ๊ตฌ์ (๋ฐฑ์ค, 14502) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] ์ ์ ์ผ๊ฐํ (๋ฐฑ์ค, 1932) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] N-Queen (๋ฐฑ์ค, 9663) (0) | 2020.11.20 |