삼성 2022 상반기 오후 기출2번 

0KB, 21ms

 

https://www.codetree.ai/frequent-problems/tree-kill-all/description

 

  • BFS + 구현
  • 주의점
    • 벽이 있거나 나무가 없어도 스프레이는 뿌려야한다.
#include <iostream>
#include <queue>
#include <iomanip>

#define MAX 20
using namespace std;

int map[MAX][MAX] = {}, spray[MAX][MAX] = {};
int N, M, K, C;

int dy[4] = { -1,1,0,0 }, dx[4] = { 0,0,-1,1 };
int dyy[4] = { -1,-1,1,1 }, dxx[4] = { -1,1,1,-1 };

int answer = 0;

typedef struct tree {
	int y, x;
	int k;
	int dir;
}TREE;

void input() {
	cin >> N >> M >> K >> C;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> map[i][j];
}

void grow() {
	for(int i=0; i<N; i++)
		for (int j = 0; j < N; j++) {
			if (spray[i][j] > 0) spray[i][j]--;
			if (map[i][j] <= 0) continue;
			int cnt = 0;
			for (int p = 0; p < 4; p++) {
				int y = i + dy[p], x = j + dx[p];
				if (y >= N || x >= N || y < 0 || x < 0) continue;

				if (map[y][x] > 0) cnt++;
			}
			map[i][j] += cnt;
		}
}

void spread() {
	int copy[MAX][MAX] = {};
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			copy[i][j] = map[i][j];

	for(int i=0; i<N; i++)
		for (int j = 0; j < N; j++) {
			if (copy[i][j] <= 0) continue;
			
			int cnt = 0, y, x;
			for (int p = 0; p < 4; p++) { // 1600
				y = i + dy[p]; x = j + dx[p];
				if (y >= N || x >= N || y < 0 || x < 0 
					|| spray[y][x] > 0) continue;

				if (copy[y][x] == 0) cnt++;
			}

			for (int p = 0; p < 4; p++) {
				y = i + dy[p]; x = j + dx[p];
				if (y >= N || x >= N || y < 0 || x < 0 || spray[y][x] > 0) continue;

				if (copy[y][x] == 0) map[y][x] += copy[i][j] / cnt;
			}
		}
}

pair<int, int> get_spray() {
	pair<int,int> res;
	int res_num = -1;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (map[i][j] <= 0) continue;

			bool visited[MAX][MAX] = {};
			int num = 0; 
			queue<TREE> q;

			visited[i][j] = true;
			num += map[i][j];

			q.push({ i, j, 0, 0 });
			q.push({ i, j, 0, 1 });
			q.push({ i, j, 0, 2 });
			q.push({ i, j, 0, 3 });

			while (!q.empty()) {
				TREE now = q.front(); q.pop();
				if (now.k == K) continue;

				int new_y = now.y + dyy[now.dir], new_x = now.x + dxx[now.dir];
				if (new_y >= N || new_x >= N 
					|| new_y < 0 || new_x < 0 
					|| visited[new_y][new_x]
					|| map[new_y][new_x] <= 0) continue;

					visited[new_y][new_x] = true;
					num += map[new_y][new_x];
					q.push({new_y, new_x, now.k+1, now.dir});
			}

			if (res_num < num) {
				res_num = num;
				res = { i, j };
			}
		}
	}

	if (res_num == -1) res = { 0,0 };

	return res;
}

void set_spray(pair<int, int> ch) {
	int y = ch.first, x = ch.second;
	bool visited[MAX][MAX] = {};
	queue<TREE> q;

	if (map[y][x] <= 0) {
		spray[y][x] = C + 1;
		return;
	}

	visited[y][x] = true;
	q.push({ y, x, 0, 0 });
	q.push({ y, x, 0, 1 });
	q.push({ y, x, 0, 2 });
	q.push({ y, x, 0, 3 });
	answer += map[y][x];
	map[y][x] = 0;
	spray[y][x] = C + 1;
	
	while (!q.empty()) {
		TREE now = q.front(); q.pop();
		if (now.k == K) continue;

		int new_y = now.y + dyy[now.dir], new_x = now.x + dxx[now.dir];
		if (new_y >= N || new_x >= N
			|| new_y < 0 || new_x < 0
			|| visited[new_y][new_x]) continue;

		if (map[new_y][new_x] <= 0) {
			spray[new_y][new_x] = C + 1;
			continue;
		}

		visited[new_y][new_x] = true;
		answer += map[new_y][new_x];
		map[new_y][new_x] = 0;
		spray[new_y][new_x] = C + 1;
		q.push({ new_y, new_x, now.k + 1, now.dir });
	}
}

void solve() {
	while (M--) {
		grow();
		spread();
		
		pair<int, int> choice = get_spray();
		set_spray(choice);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	solve();

	cout << answer;

	return 0;
}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

20058 마법사 상어와 파이어스톰  (0) 2022.10.12
21608 상어 초등학교  (0) 2022.10.11
21610 마법사 상어와 비바라기  (0) 2022.10.11
20057 마법사 상어와 토네이도  (0) 2022.10.10
17825 주사위 윷놀이  (0) 2022.10.10

2224KB, 52ms

 

  • BFS, 행렬 회전 문제
    • [q-j-i][pow(2,L)-(p-i)+j]
  • 주의사항
    • 0을 출력하라고 한게 그냥 0이 아니라 maxblock에 0을 출력하라고 한거임.. 에바
#include <iostream>
#include <cmath>
#include <queue>
#include <iomanip>

#define MAX 64
using namespace std;

int N, Q, L, map[MAX][MAX] = {};
int pow2N;

int dy[4] = { -1,1,0,0 }, dx[4] = { 0,0,-1,1 };

void input() {
	cin >> N >> Q;
	pow2N = pow(2, N);
	for (int i = 0; i < pow2N; i++)
		for (int j = 0; j < pow2N; j++)
			cin >> map[i][j];
}

void turn() {
	int copy_map[MAX][MAX] = {};
	for (int i = 0; i < pow2N; i++)
		for (int j = 0; j < pow2N; j++)
			copy_map[i][j] = map[i][j];

	int jump = pow(2, L);
	for (int i = 0; i < pow2N; i += jump) 
		for (int j = 0; j < pow2N; j += jump) 
			for (int p = i; p < i + jump; p++) 
				for (int q = j; q < j + jump; q++) 
					map[q - j + i][jump -1 - (p - i) + j] = copy_map[p][q];
}

void melt() {
	int copy_map[MAX][MAX] = {};
	for (int i = 0; i < pow2N; i++)
		for (int j = 0; j < pow2N; j++)
			copy_map[i][j] = map[i][j];

	for (int i = 0; i < pow2N; i++) {
		for (int j = 0; j < pow2N; j++) {
			if (copy_map[i][j] == 0) continue;
			int cnt = 0;
			for (int k = 0; k < 4; k++) {
				int y = i + dy[k], x = j + dx[k];
				if (y >= pow2N || x >= pow2N || y < 0 || x < 0) continue;
				if (copy_map[y][x] > 0) cnt++;
			}
			if (cnt < 3) map[i][j]--;
		}
	}
}

void solve() {
	while (Q--) {
		cin >> L;
		turn();
		melt();
	}
}

void result() {
	bool visited[MAX][MAX] = {};
	int res = 0;
	int maxblock = 0;

	queue<pair<int, int>> q;

	for (int i = 0; i < pow2N; i++) {
		for (int j = 0; j < pow2N; j++) {
			if (visited[i][j] || map[i][j] == 0) continue;
			visited[i][j] = true;
			q.push({i, j});

			int cnt = 0;
			while (!q.empty()) {
				int y = q.front().first, x = q.front().second;
				cnt++;
				q.pop();
				res += map[y][x];

				for (int i = 0; i < 4; i++) {
					int new_y = y + dy[i], new_x = x + dx[i];
					if (new_y >= pow2N || new_y < 0 || new_x >= pow2N || new_x < 0
						|| map[new_y][new_x] == 0 || visited[new_y][new_x]) continue;

					visited[new_y][new_x] = true;
					q.push({ new_y, new_x });
				}
			}
			maxblock = maxblock < cnt ? cnt : maxblock;
		}
	}
	cout << res << '\n' << maxblock;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	solve();
	result();
}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

나무박멸  (0) 2022.10.13
21608 상어 초등학교  (0) 2022.10.11
21610 마법사 상어와 비바라기  (0) 2022.10.11
20057 마법사 상어와 토네이도  (0) 2022.10.10
17825 주사위 윷놀이  (0) 2022.10.10

2400KB, 0ms

  • 구현
  • 풀이법
    • 좋아하는 학생을 bool 배열로 구현
    • 위, 왼쪽부터 좋아하는 학생 수, 비어있는 칸 확인하고 업데이트
  • 주의점
    • 처음에 best_favo = 0, best_empty = 0으로 했더니 마지막 한 칸에서 자꾸 에러 남
    • 둘 다 -1로 바꿔서 갈 수 있는 칸이 한 칸밖에 없을 경우를 커버해줌
#include <iostream>
#include <cmath>

#define MAX 21
using namespace std;

bool student[MAX * MAX][MAX * MAX] = {};
int map[MAX][MAX] = {};
int order[MAX * MAX] = {};

int N;

void input() {
	int a[5];
	cin >> N;
	for (int i = 1; i <= N * N; i++) {
		cin >> a[0];
		for (int j = 1; j <= 4; j++) {
			cin >> a[j];
			student[a[0]][a[j]] = true;
		}
		order[i] = a[0];
	}
}

void solve() {
	int dy[4] = { -1,1,0,0 }, dx[4] = { 0,0,-1,1 };

	for (int i = 1; i <= N * N; i++) {
		int now = order[i];
		int best_favo = -1, best_empty = -1, best_y, best_x;

		for (int p = 0; p < N; p++) {
			for (int q = 0; q < N; q++) {
				if (map[p][q] != 0) continue;

				int favo = 0, empty = 0;
				for (int k = 0; k < 4; k++) {
					int y = p + dy[k], x = q + dx[k];
					if (y >= N || y < 0 || x >= N || x < 0) continue;
					
					if (map[y][x] == 0) empty++;
					else
						if (student[now][map[y][x]] == true) favo++;
				}

				if (favo > best_favo) {
					best_favo = favo;
					best_y = p; best_x = q;
					best_empty = empty;
				}
				else if (favo == best_favo && empty > best_empty) {
					best_favo = favo;
					best_y = p; best_x = q;
					best_empty = empty;
				}
				
			}
		}

		map[best_y][best_x] = now;
	}

	int res = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			int cnt = 0;
			for (int k = 0; k < 4; k++) {
				int y = i + dy[k], x = j + dx[k];
				if (y >= N || y < 0 || x >= N || x < 0) continue;

				if (student[map[i][j]][map[y][x]]) cnt++;
			}

			if (cnt > 0) res += pow(10, cnt - 1);
		}
	}

	cout << res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	solve();

	return 0;
}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

나무박멸  (0) 2022.10.13
20058 마법사 상어와 파이어스톰  (0) 2022.10.12
21610 마법사 상어와 비바라기  (0) 2022.10.11
20057 마법사 상어와 토네이도  (0) 2022.10.10
17825 주사위 윷놀이  (0) 2022.10.10

2168KB, 4ms

연결된 map 로직

  • 단순 구현 문제
    • move_cloud
      • 구름 이동 및 비 내리고, 구름 사라지기까지
      • 구름 사라지는 과정에서는 queue에 또다시 추가를 해서 물복사 버그를 할 수 있게 함
      • 연결된 map
    • make_cloud
      • 물복사 버그, 구름 생성까지
      • queue에 있는 위치의 대각선을 보고 물복사 버그
      • bool 배열을 통해 물복사 버그가 일어난 곳 표시
  • 주의점
    • map이 연결되어 있음 
    • 상하좌우대각선 이동
#include <iostream>
#include <queue>

#define MAX 51
using namespace std;

int N, M, map[MAX][MAX] = {};
int di, si;

queue<pair<int, int>> q;

int dy[9] = { 0,0,-1,-1,-1,0,1,1,1 };
int dx[9] = { 0,-1,-1,0,1,1,1,0,-1 };

void input() {
	cin >> N >> M;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			cin >> map[i][j];
}

void move_cloud() {
	int qsize = q.size();
	
	int y, x;
	while (qsize--) {
		y = q.front().first + dy[di] * si; 
		x = q.front().second + dx[di] * si;
		q.pop();

		while (y <= 0) y += N;
		while (x <= 0) x += N;
		while (y > N) y -= N;
		while (x > N) x -= N;

		map[y][x]++;
		q.push({ y,x });
	}

}

void make_cloud() {
	bool cloud[MAX][MAX] = {};

	int y, x;
	while (!q.empty()) {
		y = q.front().first;
		x = q.front().second;
		q.pop();
		cloud[y][x] = true;

		// 2,4,6,8
		int cnt = 0;
		for (int i = 2; i <= 8; i += 2) {
			if (y + dy[i] > N || y + dy[i] <= 0
				|| x + dx[i] > N || x + dx[i] <= 0) continue;
			if (map[y + dy[i]][x + dx[i]] > 0) cnt++;
		}

		map[y][x] += cnt;
	}

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			if (cloud[i][j] || map[i][j] < 2) continue;
			
			q.push({ i,j });
			map[i][j] -= 2;
		}
	}
}

void solve() {
	q.push({ N,1 });
	q.push({ N, 2 });
	q.push({ N - 1, 1 });
	q.push({ N - 1,2 });

	while (M--) {
		cin >> di >> si;

		move_cloud();
		make_cloud();
	}
}

void result() {
	int res = 0;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			res += map[i][j];

	cout << res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	solve();
	result();
}

e

'coding > baekjoon_codingstudy' 카테고리의 다른 글

20058 마법사 상어와 파이어스톰  (0) 2022.10.12
21608 상어 초등학교  (0) 2022.10.11
20057 마법사 상어와 토네이도  (0) 2022.10.10
17825 주사위 윷놀이  (0) 2022.10.10
20061 모노미노도미노2  (0) 2022.10.06

2992KB, 32ms

  • 구현 문제
  • 주의점
    • 회오리 배열
#include <iostream>
#include <iomanip>

#define MAX 499
using namespace std;

int dy[4][10] = { {-1,1,-2,2,0,-1,1,-1,1,0},
				 {1,1,0,0,-2,0,0,-1,-1,-1},
				 {-1,1,-2,2,0,-1,1,-1,1,0},
				 {-1,-1,0,0,2,0,0,1,1,1} }; // 우상좌하
int dx[4][10] = { {-1,-1,0,0,2,0,0,1,1,1},
				 {-1,1,-2,2,0,-1,1,-1,1,0},
				 {1,1,0,0,-2,0,0,-1,-1,-1},
				 {-1,1,-2,2,0,-1,1,-1,1,0} };
int ratio[9] = { 1,1,2,2,5,7,7,10,10};

int dir_dy[4] = { 0,-1,0,1 }, dir_dx[4] = { 1,0,-1,0 };

int N;
int map[MAX][MAX] = {};
int y, x;

int result = 0;

void gol() {
	int twice = 0, cnt = 1, count = 0;
	y = N / 2; x = N / 2 - 1;
	int dir = 2;

	while (!(y == 0 && x == -1)) {
		int minus = 0, new_y, new_x;
		for (int i = 0; i < 9; i++) {
			new_y = y + dy[dir][i]; new_x = x + dx[dir][i];
			minus += map[y][x] * ratio[i] / 100;

			if (new_y >= N || new_x >= N || new_y < 0 || new_x < 0)
				result += map[y][x] * ratio[i] / 100;	
			else map[new_y][new_x] += map[y][x] * ratio[i] / 100;
		}

		new_y = y + dy[dir][9]; new_x = x + dx[dir][9];
		if (new_y >= N || new_x >= N || new_y < 0 || new_x < 0)
			result += map[y][x] - minus;
		else map[new_y][new_x] += map[y][x] - minus;

		map[y][x] = 0;

		count++;
		
		if (cnt == count) {
			twice++;
			dir = (dir + 5) % 4;
			count = 0; 
			if (twice == 2) {
				cnt++;
				twice = 0;
			}
		}
		y += dir_dy[dir]; x += dir_dx[dir];
	}
}

void input() {
	cin >> N;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> map[i][j];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	gol();

	cout << result;

}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

21608 상어 초등학교  (0) 2022.10.11
21610 마법사 상어와 비바라기  (0) 2022.10.11
17825 주사위 윷놀이  (0) 2022.10.10
20061 모노미노도미노2  (0) 2022.10.06
12100 2048(Easy)  (0) 2022.10.03

경로를 총 5개로 나눠서 풀었음

백트래킹하면 더 빨리 풀듯

#include <iostream>
#include <queue>

using namespace std;

int score_map[5][21] = { {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38},
					     {10,13,16,19},
					     {20,22,24},
					     {30,28,27,26},
					     {25,30,35,40} };

int dis_map[5] = {20, 4, 3, 4, 4};
int dice[10] = {};
int result = 0;

typedef struct m {
	bool finished[4] = {};
	int finish_cnt = 0;
	int usingmap[4] = {};
	int point[4] = {};
	int turn = 0;
	int cnt = 0;
}MARKERS;

void input() {
	for (int i = 0; i < 10; i++)
		cin >> dice[i];
}

void show(MARKERS marker) {
	for (int i = 0; i < 4; i++) {
		cout << "marker" << i << ":" << marker.usingmap[i] << ' ' << marker.point[i] << endl;
	}
	cout << "cnt : " << marker.cnt << endl;
}

void solve() {
	queue<MARKERS> q;

	MARKERS markers;
	q.push(markers);

	while (!q.empty()) {
		MARKERS now = q.front();
		//show(now);
		if (now.turn == 10 || now.finish_cnt == 4) {
			//if (result < now.cnt) show(now);
			result = (result < now.cnt) ? now.cnt : result;		
			q.pop();
			continue;
		}

		for (int i = 0; i < 4; i++) {
			MARKERS move = q.front();
			if (move.finished[i]) continue;

			// move
			move.point[i] += dice[move.turn];
			move.turn++;
			
			bool end_flag = false;
			if (move.point[i] >= dis_map[move.usingmap[i]]) {
				if (move.usingmap[i] == 0) {
					if (move.point[i] == dis_map[0]) {
						move.usingmap[i] = 4;
						move.point[i] = 3;
					}
					else {
						move.finished[i] = true;
						end_flag = true;
					}
				}
				else if (move.usingmap[i] < 4) {
					move.point[i] -= dis_map[move.usingmap[i]];
					move.usingmap[i] = 4;
				}

				if (move.usingmap[i] == 4 && 
					move.point[i] >= dis_map[move.usingmap[i]]) {
					move.finished[i] = true;
					end_flag = true;
				}
			}
			else {
				if (move.usingmap[i] == 0 &&
					score_map[0][move.point[i]] % 10 == 0) {
					move.usingmap[i] = score_map[0][move.point[i]] / 10;
					move.point[i] = 0;
				}
			}

			// check
			if (!end_flag) {
				bool check_flag = false;
				for (int j = 0; j < 4; j++) {
					if (move.finished[i] || i == j) continue;

					if (move.point[i] == move.point[j]
						&& move.usingmap[i] == move.usingmap[j]) {
						check_flag = true;
						break;
					}
				}

				if (check_flag) continue;

				move.cnt += score_map[move.usingmap[i]][move.point[i]];
			}

			q.push(move);
		}

		q.pop();
		
	}

}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	input();
	solve();
	cout << result;
}

 

'coding > baekjoon_codingstudy' 카테고리의 다른 글

21610 마법사 상어와 비바라기  (0) 2022.10.11
20057 마법사 상어와 토네이도  (0) 2022.10.10
20061 모노미노도미노2  (0) 2022.10.06
12100 2048(Easy)  (0) 2022.10.03
21609 상어중학교  (0) 2022.10.02

20061 모노미노도미노2 > 2024KB, 4ms

 

 

  • 속도 무난, 메모리 무난
  • 단순 구현 문제
  • 간단한 풀이
    • 입력 받기
    • blue 칸으로, green 칸으로 이동
    • 4칸이 다 찬 게 있나 확인
    • 특별한 칸에 블록이 있나 확인
  • 단순한 구현 문제였다. 주의사항만 잘 숙지하면 무난하게 풀 수 있는 문제
    • 주의 사항 : 중력이 작용하는게 아니라 "사라진 행/열 만큼만" 행/열이 움직이는 것! 

 

#include <iostream>

using namespace std;

int map[10][10] = {};
int N, t, y, x, score = 0;

void move_blue() {
	int new_x;
	switch (t) {
	case 1:
		new_x = x + 1;
		while (new_x <= 9 && map[y][new_x] == 0) new_x++;
		map[y][new_x - 1] = 1;
		break;
	case 3:
		new_x = x + 1;
		while (new_x <= 9 && map[y][new_x] == 0 && map[y + 1][new_x] == 0) new_x++;
		map[y][new_x - 1] = 1;
		map[y + 1][new_x - 1] = 1;
		break;
	case 2:
		new_x = x + 2;
		while (new_x <= 9 && map[y][new_x] == 0) new_x++;
		map[y][new_x - 1] = 1;
		map[y][new_x - 2] = 1;
		break;
	}
}

void move_green() {
	int new_y;
	switch (t) {
	case 1:
		new_y = y + 1;
		while (new_y <= 9 && map[new_y][x] == 0) new_y++;
		map[new_y - 1][x] = 1;
		break;
	case 3:
		new_y = y + 2;
		while (new_y <= 9 && map[new_y][x] == 0) new_y++;
		map[new_y - 1][x] = 1;
		map[new_y - 2][x] = 1;
		break;
	case 2:
		new_y = y + 1;
		while (new_y <= 9 && map[new_y][x] == 0 && map[new_y][x + 1] == 0) new_y++;
		map[new_y - 1][x] = 1;
		map[new_y - 1][x + 1] = 1;
		break;
	}
}

void check_max() {
	int move_col = 0;
	for (int j = 9; j > 3; j--) {
		int move_j = j;
		if (move_col > 0) {
			move_j = j + move_col;
			for (int i = 0; i < 4; i++) {
				map[i][move_j] = map[i][j];
				map[i][j] = 0;
			}
		}

		int cnt = 0;
		for (int i = 0; i < 4; i++)
			if (map[i][move_j] == 1) cnt++;
		if (cnt == 4) {
			for (int i = 0; i < 4; i++)
				map[i][move_j] = 0;
			move_col++;
			score++;
		}
	}

	int move_row = 0;
	for (int i = 9; i > 3; i--) {
		int move_i = i;
		if (move_row > 0) {
			move_i = i + move_row;
			for (int j = 0; j < 4; j++) {
				map[move_i][j] = map[i][j];
				map[i][j] = 0;
			}
		}

		int cnt = 0;
		for (int j = 0; j < 4; j++)
			if (map[move_i][j] == 1) cnt++;
		if (cnt == 4) {
			for (int j = 0; j < 4; j++)
				map[move_i][j] = 0;
			move_row++;
			score++;
		}
	}
}

void check_pale() {
	int move_col = 0;
	for (int j = 4; j < 6; j++) {
		for (int i = 0; i < 4; i++) {
			if (map[i][j] == 1) {
				move_col++;
				break;
			}
		}
	}

	if (move_col > 0) {
		for (int j = 9; j > 3 + move_col; j--) {
			for (int i = 0; i < 4; i++) {
				map[i][j] = map[i][j - move_col];
				map[i][j - move_col] = 0;
			}
		}
	}

	int move_row = 0;
	for (int i = 4; i < 6; i++) {
		for (int j = 0; j < 4; j++) {
			if (map[i][j] == 1) {
				move_row++;
				break;
			}
		}
	}


	if (move_row > 0) {
		for (int i = 9; i > 3 + move_row; i--) {
			for (int j = 0; j < 4; j++) {
				map[i][j] = map[i - move_row][j];
				map[i - move_row][j] = 0;
			}
		}
	}
	
}

void solve() {
	for (int n = 0; n < N; n++) {
		cin >> t >> y >> x;

		move_blue();
		move_green();

		check_max();

		check_pale();
	}
}

void result() {
	cout << score << endl;

	int blue = 0, green = 0;

	for (int j = 6; j < 10; j++) {
		for (int i = 0; i < 4; i++) {
			if (map[i][j] == 1) blue++;
		}
	}

	for (int i = 6; i < 10; i++) {
		for (int j = 0; j < 4; j++) {
			if (map[i][j] == 1) green++;
		}
	}

	cout << blue + green;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N;
	solve();
	result();

}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

20057 마법사 상어와 토네이도  (0) 2022.10.10
17825 주사위 윷놀이  (0) 2022.10.10
12100 2048(Easy)  (0) 2022.10.03
21609 상어중학교  (0) 2022.10.02
19238 스타트택시  (0) 2022.10.02

12100 2048(Easy) > 2028KB, 4ms

  • 간단한 로직
    • DFS -> 움직임(move) -> DFS
    • 종료조건(cnt==6)에 최종값 갱신
      • 왜 종료조건이 6?
    • 움직임
      • 0이면 다음 숫자 끌어올리고 합치는 동작
      • 0이 아니면 합치는 동작
  • 시간 느림 

시간이 느린 이유는 map을 copy하는데 드는 N^2 시간 때문

가로, 세로로 움직이는 건 각각 하나의 함수로 합칠 수 있음

참고 사이트 : https://girlfriend-yerin.tistory.com/99

 

Baekjoon 12100 2048 (Easy)

Link https://www.acmicpc.net/problem/12100 소스결과 1988 KB / 0ms 출처 Baekjoon 언어 C++ 17 분류 브루트 포스 설명 과거 유행했던 2048 게임을 모방한 Easy버전의 2048게임을 구현하자. 2048 게임을 해봤다..

girlfriend-yerin.tistory.com

 

 

#include <iostream>

#define MAX 20
using namespace std;

int N, map[MAX][MAX], result = 0;
int dy[4] = { -1,1,0,0 }, dx[4] = { 0,0,-1,1 };

void input() {
	cin >> N;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> map[i][j];
}

int find_max(int map_ori[MAX][MAX]) {
	int rtn = 0;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			if (rtn < map_ori[i][j]) rtn = map_ori[i][j];

	return rtn;
}

void move(int map_ori[MAX][MAX], int dir) {
	switch (dir) {
	case 0:
		for (int j = 0; j < N; j++) {
			for (int i = 0; i < N; i++) {
				int new_i = i + 1;
				while (new_i < N && map_ori[new_i][j] == 0) new_i++;
				if (new_i == N) break;

				if (map_ori[i][j] == 0) {
					map_ori[i][j] = map_ori[new_i][j];
					map_ori[new_i][j] = 0;
					new_i++;
				}

				while (new_i < N && map_ori[new_i][j] == 0) new_i++;
				if (new_i == N) break;

				if (map_ori[i][j] != 0 && map_ori[new_i][j] == map_ori[i][j]) {
					map_ori[new_i][j] = 0;
					map_ori[i][j] *= 2;
				}
			}
		}
		break;
	case 1:
		for (int j = 0; j < N; j++) {
			for (int i = N - 1; i >= 0; i--) {
				int new_i = i - 1;
				while (new_i >= 0 && map_ori[new_i][j] == 0) new_i--;
				if (new_i == -1) break;

				if (map_ori[i][j] == 0) {
					map_ori[i][j] = map_ori[new_i][j];
					map_ori[new_i][j] = 0;
					new_i--;
				}

				while (new_i >= 0 && map_ori[new_i][j] == 0) new_i--;
				if (new_i == -1) break;

				if (map_ori[i][j] != 0 && map_ori[new_i][j] == map_ori[i][j]) {
					map_ori[new_i][j] = 0;
					map_ori[i][j] *= 2;
				}
			}
		}
		break;
	case 2:
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				int new_j = j + 1;
				while (new_j < N && map_ori[i][new_j] == 0) new_j++;
				if (new_j == N) break;

				if (map_ori[i][j] == 0) {
					map_ori[i][j] = map_ori[i][new_j];
					map_ori[i][new_j] = 0;
					new_j++;
				}

				while (new_j < N && map_ori[i][new_j] == 0) new_j++;
				if (new_j == N) break;

				if (map_ori[i][j] != 0 && map_ori[i][new_j] == map_ori[i][j]) {
					map_ori[i][new_j] = 0;
					map_ori[i][j] *= 2;
				}
			}
		}
		break;
	case 3:
		for (int i = 0; i < N; i++) {
			for (int j = N - 1; j >= 0; j--) {
				int new_j = j - 1;
				while (new_j >= 0 && map_ori[i][new_j] == 0) new_j--;
				if (new_j == -1) break;

				if (map_ori[i][j] == 0) {
					map_ori[i][j] = map_ori[i][new_j];
					map_ori[i][new_j] = 0;
					new_j--;
				}

				while (new_j >= 0 && map_ori[i][new_j] == 0) new_j--;
				if (new_j == -1) break;

				if (map_ori[i][j] != 0 && map_ori[i][new_j] == map_ori[i][j]) {
					map_ori[i][new_j] = 0;
					map_ori[i][j] *= 2;
				}
			}
		}
		break;
	}
}

void DFS(int map_ori[MAX][MAX], int dir, int cnt) {
	// stop case
	if (cnt == 6) {
		int findmax = find_max(map_ori);
		if (findmax > result) result = findmax;
		return;
	}

	// map copy
	int map_copy[MAX][MAX];
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			map_copy[i][j] = map_ori[i][j];

	// move
	move(map_copy, dir);

	// DFS
	for (int i = 0; i < 4; i++)
		DFS(map_copy, i, cnt + 1);

}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	input();

	for (int i = 0; i < 4; i++)
		DFS(map, i, 1);

	cout << result;
}

'coding > baekjoon_codingstudy' 카테고리의 다른 글

17825 주사위 윷놀이  (0) 2022.10.10
20061 모노미노도미노2  (0) 2022.10.06
21609 상어중학교  (0) 2022.10.02
19238 스타트택시  (0) 2022.10.02
13460 구슬 탈출2  (0) 2022.10.02

+ Recent posts