[코드트리] 토스트 계란틀

rhkr9080·2023년 7월 19일
0

코드트리

목록 보기
10/29

문제링크 : https://www.codetree.ai/training-field/frequent-problems/problems/toast-eggmold/description?page=1&pageSize=20

💻 문제 풀이 : C++

#include <iostream>
#include <queue>
#include <vector>
#include <cstring>

#define MAP_SIZE 55

using namespace std;

struct Coord
{
	int row;
	int col;
};

int n, L, R;
int MAP[MAP_SIZE][MAP_SIZE];
int visited[MAP_SIZE][MAP_SIZE];

int dr[] = { 0 ,0, -1, 1 };
int dc[] = { -1, 1, 0, 0 };

int getAbs(int a)
{
	return a > 0 ? a : (-1) * a;
}

void INPUT()
{
	cin >> n >> L >> R;

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> MAP[i][j];
}

void bfs(Coord start, int idx)
{
	int tmpSum = MAP[start.row][start.col];
	vector<Coord> tmp;
	tmp.push_back(start);
	queue<Coord> nowQ;
	nowQ.push(start);
	while (!nowQ.empty())
	{
		Coord now = nowQ.front();
		nowQ.pop();
		for (int i = 0; i < 4; i++)
		{
			Coord next = { now.row + dr[i], now.col + dc[i] };
			int dist = getAbs(MAP[next.row][next.col] - MAP[now.row][now.col]);
			if (next.row < 0 || next.col < 0 || next.row >= n || next.col >= n) continue;
			if (dist < L || dist > R) continue;
			if (visited[next.row][next.col] != 0) continue;
			visited[next.row][next.col] = idx;
			nowQ.push({ next.row, next.col });
			tmp.push_back(next);
			tmpSum += MAP[next.row][next.col];
		}
	}
	int debug = 0;
	for (int i = 0; i < tmp.size(); i++)
	{
		MAP[tmp[i].row][tmp[i].col] = tmpSum / tmp.size();
	}
}

void SIMUL()
{
	int time = 0;

	while (time < 2000)
	{
		memset(visited, 0, sizeof(visited));
		int idx = 1;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (visited[i][j] == 0)
				{
					visited[i][j] = idx;
					bfs({ i, j }, idx);
					idx++;
				}
			}
		}
		if (visited[n - 1][n - 1] == n * n) break;
		time++;
	}

	cout << time << "\n";
}

int main()
{
	INPUT();
	SIMUL();

	return 0;
}

📌 memo 😊

profile
공부방

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

이런 정보를 찾고 있었습니다, 감사합니다.

답글 달기