본문 바로가기

코테 문풀/[Algorithm]백준(BOJ) 풀이

[BOJ] 12100 - 2048(EASY)

#include<iostream>
#include<queue>


#define endl "\n"
#define up 0
#define down 1
#define left 2
#define right 3
using namespace std;
int basic_map[21][21];
int n;

int max(int** map)
{
	int m = -1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++)
		{
			if (map[i][j] > m)
				m = map[i][j];
		}
	}
	return m;
}
int** move(int** map, int dir)
{
	int idx;
	queue<int> q;
	if (dir == up)
	{
		//cout << "!!";
		
		
		for (int i = 0; i < n; i++) {
			idx = 0;
			for (int j = 0; j < n; j++)
			{
				if(map[j][i])
					q.push(map[j][i]);
				
			}
			while (!q.empty())
			{
				int num1, num2;
				num1 = q.front();
				//cout <<"!!!"<< num1;
				q.pop();
				if (!q.empty()) {
					num2 = q.front();
					if (num2 == num1)
					{
						q.pop();
						map[idx++][i] = num1 + num2;
					}
					else
						map[idx++][i] = num1;
					
				}
				else {
					map[idx++][i] = num1;
				}
			}
			for (int k = idx; k < n; k++)
				map[k][i] = 0;
		}

		
		
		
	}
	if (dir == down)
	{
		
		for (int i = 0; i < n; i++) {
			idx = n - 1;
			for (int j = n-1; j >=0 ; j--)
			{
				if (map[j][i])
					q.push(map[j][i]);
				map[j][i] = 0;
			}
			while (!q.empty())
			{
				int num1, num2;
				num1 = q.front();
				q.pop();
				if (!q.empty()) {
					num2 = q.front();
					if (num2 == num1)
					{
						q.pop();
						map[idx--][i] = num1 + num2;
					}
					else
						map[idx--][i] = num1;

				}
				else {
					map[idx++][i] = num1;
				}
			}
		}

	}
	if (dir == left)
	{
		
		for (int i = 0; i < n; i++) {
			idx = 0;
			for (int j = 0; j < n; j++)
			{
				if (map[i][j])
					q.push(map[i][j]);
				map[i][j] = 0;
			}
			while (!q.empty())
			{
				int num1, num2;
				num1 = q.front();
				q.pop();
				if (!q.empty()) {
					num2 = q.front();
					if (num2 == num1)
					{
						q.pop();
						map[i][idx++] = num1 + num2;
					}
					else
						map[i][idx++] = num1;
				}
				else {
					map[i][idx++] = num1;
				}
			}
		}
		

	}
	if (dir == right)
	{
		
		for (int i = 0; i < n; i++) {
			idx = n - 1;
			for (int j = n-1; j>=0; j--)
			{
				if (map[i][j])
					q.push(map[i][j]);
				map[i][j] = 0;
			}
			while (!q.empty())
			{
				int num1, num2;
				num1 = q.front();
				q.pop();
				if (!q.empty()) {
					num2 = q.front();
					if (num2 == num1)
					{
						q.pop();
						map[i][idx--] = num1 + num2;
					}
					else
						map[i][idx--] = num1;

				}
				else {
					map[i][idx--] = num1;
				}
			}
		}
		
	}
	/*
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cout << map[i][j] << " ";

			}
			cout << endl;
		}
		*/
	map[n][n] += 1;
	return map;
}

void BFS()
{
	int max_val = -1;
	int count = 0;
	int **tmp = new int *[n+1];
	for (int i = 0; i <= n; i++)
		tmp[i] = new int[n+1];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++)
			tmp[i][j] = basic_map[i][j];
	}
	tmp[n][n] = count;
	queue<int**> q;
	q.push(tmp);
	while (!q.empty())
	{
		int** pop_map = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++)///////
		{
			int** new_map = new int* [n + 1];
			for (int m = 0; m <= n; m++)
				new_map[m] = new int[n + 1];
			for (int m = 0; m < n; m++) {
				for (int k = 0; k < n;k++)
					new_map[m][k] = pop_map[m][k];
			}
			new_map[n][n] = pop_map[n][n];
			
			new_map = move(new_map, i);
			
			//cout << count;
			if (new_map[n][n] == 5) {
				if( max_val < max(new_map))
					max_val = max(new_map);
				continue;
			}
			q.push(new_map);
			
		}
		for (int m = 0; m <= n; m++)
			delete pop_map[m];
		delete pop_map;
	}
	cout << max_val;
}

void Answer()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> basic_map[i][j];

		}
	}
	
	BFS();
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	Answer();
	return 0;
}