• Home
  • About
    • on Weekend photo

      on Weekend

      ๐™Ž๐™ฉ๐™ช๐™™๐™ฎ๐™ž๐™ฃ๐™œ

    • Learn More
    • Instagram
    • Github
  • Archive
    • All Posts
    • All Tags
    • All Categories
  • Categories
    • Problem Solving
    • TIL
    • Study
    • Etc
    • ํ•„์‚ฌ
  • Projects

[๋ฐฑ์ค€] 14052 (C++)

29 Nov 2020

๋ฌธ์ œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธ
์ฝ”๋“œ๋กœ ๋ฐ”๋กœ ๋„˜์–ด๊ฐ€๊ธฐ

๋ฌธ์ œํ’€์ด

๐ŸŽ‡์นดํ…Œ๊ณ ๋ฆฌ : ๊ทธ๋ž˜ํ”„?
๐ŸŽ‡๋ฌธ์ œ๋ช… : ์—ฐ๊ตฌ์†Œ
๐ŸŽ‡ํ’€์ด :
์—ฐ๊ตฌ์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ N*M์ธ ์ง์‚ฌ๊ฐํ˜•, ๋ฒฝ์€ ์นธ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋“ ์ฐจ์ง€ํ•œ๋‹ค.
๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ๋นˆ ์นธ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ„๋‹ค. ์ƒˆ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฉฐ, ๊ผญ 3๊ฐœ๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค.
0์€ ๋นˆ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์ด๋‹ค.
๋ฒฝ์„ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค.

1. ๋ฒฝ ์ƒ์„ฑ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋งŒ๋“ค๊ธฐ
2. BFS๋กœ ๋ฐ”์ด๋Ÿฌ์Šค ํผ๋œจ๋ฆฌ๊ธฐ
3. ์•ˆ์ „์˜์—ญ์˜ ๊ฐฏ์ˆ˜ ์„ธ๊ธฐ
4. ๊ฐ€์žฅ ํฐ ์•ˆ์ „ ์˜์—ญ ๊ตฌํ•˜๊ธฐ

๋‚ด๊ฐ€ ๋ฐฐ์šด ๊ฒƒ

์˜ˆ์œ ์žฌ๊ท€
tmp_MAP[i][j] = 1;
make_wall(cnt + 1);
tmp_MAP[i][j] = 0;

์ด๋ ‡๊ฒŒ ์žฌ๊ท€๋ฅผ ์“ฐ๋Š” ๊ฑด ๋‚ด๊ฐ€ ์ž์ฃผ ์žŠ๋Š” ๊ธฐ๋ฒ• ์ค‘์— ํ•˜๋‚˜์ด๋‹ค. ์ž˜ ๊ธฐ์–ตํ•ด๋‘ฌ์•ผ๊ฒ ๋‹ค.

์˜ˆ์œ ๋ณต์‚ฌ(deep copy)
void copy(int (*target)[MAX], int (*dest)[MAX])
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			target[i][j] = dest[i][j];
}

2์ฐจ์› ๋ฐฐ์—ด์„ deep copyํ•˜๋Š” ๊ฑธ ์ด๋ ‡๊ฒŒ ํ–ˆ์—ˆ๋‹ค๋‹ˆ. ์ด์™• ์˜ค๋žœ๋งŒ์— ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฑฐ, ์•„๋ฆ„๋‹ต๊ฒŒ ํ’€์ž๋Š” ์ƒ๊ฐ์— ๋Š˜ 2์ค‘ for๋ฌธ์œผ๋กœ ๋Œ๋ ธ๋˜ ๋ณต์‚ฌ๋ฅผ copy ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ–ˆ๋‹ค. ๋Š˜ 2์ค‘ for๋ฌธ์œผ๋กœ๋งŒ ๋Œ๋ ธ๋˜ ํƒ“์—, 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธฐ๋Š” ๋ฒ•์„ ์™„์ „ํžˆ ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ๋‹ค. ๋ถ€๋„๋Ÿฌ์šด ์ผ์ด๋‹ค.

์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ž˜ ์ด์šฉํ•˜์ž

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ž๊พธ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ณค ํ–ˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋А๋ผ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ž๊พธ ๋ณต์žกํ•˜๊ฒŒ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ๋ฐ›๊ณ  ์ˆ˜์ •ํ•˜๊ณ  ํ•˜๋ฉด์„œ ๋ณ€์ˆ˜๊ฐ€ ์˜๋„ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋˜์—ˆ์—ˆ๋‹ค.

์ฝ”๋“œ ์ž‘์„ฑ์€ ์‹ ์ค‘ํžˆ, ํ™•์‹คํ•˜๊ฒŒ

copy(virus_MAP,tmp_MAP)์„ ํ•ด์คฌ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, copy(virus_MAP,MAP)์œผ๋กœ ์ž…๋ ฅํ•ด์„œ ํ•œ์ฐธ์„ ๋‹ต์ด ์•ˆ๋‚˜์™”์—ˆ๋‹ค. Print ๋ฌธ์„ ์ž‘์„ฑํ•ด์„œ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•ด ์ง์ ‘ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ๊ท€์ฐฎ์•„์„œ, ๋ˆˆ๋ฒ„๊น…์„ ํ–ˆ๋”๋‹ˆ ์–ธ์ œ๋‚˜ ๊ทธ๋ ‡๋“ฏ ๋” ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค. ์„ค๊ณ„๋Š” ๋จธ๋ฆฌ๊ฐ€ ํ•˜๊ณ  ๊ตฌํ˜„์€ ์†๋…ธ๊ฐ€๋‹ค๋ฅผ ๋›ฐ์ž.

๐ŸŽˆ

์ฝ”๋“œ
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int MAX = 8;
int res;
int N, M;
int MAP[MAX][MAX];
int tmp_MAP[MAX][MAX];
void Print(int(*)[MAX]);

void copy(int (*target)[MAX], int (*dest)[MAX])
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			target[i][j] = dest[i][j];
}

bool isIn(int y, int x)
{
	return (y >= 0 && y < N) && (x >= 0 && x < M);
}

int dir[4][2] = { {-1,0},{1,0},{0,1},{0,-1} };
void BFS()
{
	queue<pair<int, int>> q;

	int virus_MAP[MAX][MAX];
	copy(virus_MAP, tmp_MAP);

	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			if (virus_MAP[i][j] == 2) q.push(make_pair(i, j));

	while (!q.empty())
	{
		int y = q.front().first;
		int x = q.front().second;
		q.pop();

		for (int i = 0; i < 4; i++)
		{
			int dy = y + dir[i][0];
			int dx = x + dir[i][1];

			if (isIn(dy, dx) && virus_MAP[dy][dx] == 0) {
				virus_MAP[dy][dx] = 2;
				q.push(make_pair(dy, dx));
			}
		}
	}

	int ans = 0;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			if (virus_MAP[i][j] == 0) ans++;

	res = max(res, ans);
	return;
}

void make_wall(int cnt)
{
	if (cnt == 3)
	{
		BFS();
		return;
	}
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++) {
			if (tmp_MAP[i][j] == 0)
			{
				tmp_MAP[i][j] = 1;
				make_wall(cnt + 1);
				tmp_MAP[i][j] = 0;
			}
		}
}

void Print(int(*arr)[MAX])
{
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cout << arr[i][j];
		}
		cout << endl;
	}
	return;
}
int main()
{
	res = -1;
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {

			cin >> MAP[i][j];
		}
	}

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

			if (MAP[i][j] == 0) {
				copy(tmp_MAP, MAP);

				tmp_MAP[i][j] = 1; //make wall
				make_wall(1);
				tmp_MAP[i][j] = 0;
			}
		}
	}

	cout << res;
	return 0;
}

๋„ˆ๋ฌด ๊ธธ๋‹ค.. ๋งˆํฌ๋‹ค์šด์—๋Š” ํ† ๊ธ€์ด ์—†๋‚˜?



problem_solvingc++ Share Tweet +1