티스토리 뷰

알고리즘/BOJ

[BOJ 12100] 2048(Easy)

히더 2018. 8. 2. 17:35

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



#include <iostream>

#include <algorithm>

#include <memory.h>

using namespace std;


int N, result;

int map[20][20];

bool check[20][20];


void move(int d, int (*a)[20])

{

memset(check, false, sizeof(check));

if (d == 0)

{

for(int j=0; j<N; j++)

{

for (int i = 1; i < N; i++)

{

if (a[i][j] != 0)

{

for (int count = 1; count <= i; count++)

{

if (a[i - count][j] != 0)

{

if ((a[i - count][j] == a[i][j]) && !check[i-count][j])

{

a[i - count][j] *= 2; a[i][j] = 0; check[i - count][j] = true;

result = max(result, a[i - count][j]);

}

else swap(a[i][j], a[i - count + 1][j]);

break;

}

if (a[i - count][j] == 0 && count == i) swap(a[i][j], a[i - count][j]);

}

}

}

}

}

else if (d == 1)

{

for (int j = 0; j<N; j++)

{

for (int i = N-2; i >= 0; i--)

{

if (a[i][j] != 0)

{

for (int count = 1; count <= N-1-i; count++)

{

if (a[i + count][j] != 0)

{

if ((a[i + count][j] == a[i][j]) && !check[i + count][j])

{

a[i + count][j] *= 2; a[i][j] = 0; check[i + count][j] = true;

result = max(result, a[i + count][j]);

}

else swap(a[i][j], a[i + count - 1][j]);


break;

}

if(a[i + count][j] == 0 && count == N-1-i) swap(a[i][j], a[i + count][j]);

}

}

}

}

}

else if (d == 2)

{

for (int i = 0; i<N; i++)

{

for (int j = N - 2; j >= 0; j--)

{

if (a[i][j] != 0)

{

for (int count = 1; count <= N-1-j; count++)

{

if (a[i][j + count] != 0)

{

if ((a[i][j + count] == a[i][j]) && !check[i][j + count])

{

a[i][j + count] *= 2; a[i][j] = 0; check[i][j + count] = true;

result = max(result, a[i][j + count]);

}

else swap(a[i][j], a[i][j + count - 1]);


break;

}

if (a[i][j + count] == 0 && count == N - 1 - j) swap(a[i][j], a[i][j + count]);

}

}

}

}

}

else if (d == 3)

{

for (int i = 0; i<N; i++)

{

for (int j = 1; j < N; j++)

{

if (a[i][j] != 0)

{

for (int count = 1; count <= j; count++)

{

if (a[i][j - count] != 0)

{

if ((a[i][j - count] == a[i][j]) && !check[i][j - count])

{

a[i][j - count] *= 2; a[i][j] = 0; check[i][j - count] = true;

result = max(result, a[i][j - count]);

}

else

{

swap(a[i][j], a[i][j - count + 1]);

}


break;

}


if (a[i][j - count]==0 && count == j)

swap(a[i][j], a[i][j - count]); 

}

}

}

}

}

}

}


void dfs(int cnt, int(*arr)[20])

{

if (cnt == 5) return;


for (int direc = 0; direc < 4; direc++)

{

if (cnt == 0 && direc == 3)

{

int mmmmmm = 1;

int mmmm = 1;

}

int tmp[20][20];

for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) tmp[i][j] = arr[i][j];

move(direc, tmp); dfs(cnt+1, tmp);

}

}



int main()

{

cin >> N; result = 0;


for (int i = 0; i < N; i++) for (int j = 0; j < N; j++)

{

cin >> map[i][j]; result = max(result, map[i][j]);

}

dfs(0, map);

cout << result;

return 0;

}


'알고리즘 > BOJ' 카테고리의 다른 글

[BOJ 14500] 테트로미노  (0) 2018.08.02
[BOJ 13458] 시험감독  (0) 2018.08.02
[BOJ 14501] 퇴사  (0) 2018.08.02
[BOJ 1463] 1로 만들기  (0) 2018.07.23
[BOJ 11726] 2xn 타일링  (0) 2018.07.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함