Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11-suhyun113 #39

Merged
merged 2 commits into from
Aug 19, 2024
Merged

11-suhyun113 #39

merged 2 commits into from
Aug 19, 2024

Conversation

suhyun113
Copy link
Collaborator

@suhyun113 suhyun113 commented Jul 20, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๐Ÿงช์—ฐ๊ตฌ์†Œ๐Ÿงช

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

์•ฝ 2์‹œ๊ฐ„...? ๋” ๊ฑธ๋ฆฐ ๊ฒƒ ๊ฐ™์€๋ฐ,,,

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๐Ÿ’™1. ๋ฌธ์ œ ์„ ์ • ์ด์œ ๐Ÿ’™
๊ตฌํ˜„ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ•˜์‹œ๊ธธ๋ž˜ ์ €๋„ ๊ตฌํ˜„ ๋ฌธ์ œ๋ฅผ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด๊ณ ๋ ค์‹ถ์–ด์„œ ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ์ฐพ์•„๋ดค๋Š”๋ฐ,,,ํ•˜๋‹ค ๋ณด๋‹ˆ BFS๋กœ ํ’€๊ฒŒ ๋˜์—ˆ๋„ค์š”...
๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด๊ณ ๋‚˜์„œ ๊ทธ๋ƒฅ ๋นˆ ์นธ์œผ๋กœ ๋˜์–ด ์žˆ์œผ๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง€๋Š”๊ตฌ๋‚˜ ์ƒ๊ฐํ–ˆ๊ณ , ๋ฒฝ์ด ์žˆ์œผ๋ฉด ๊ทธ ๋’ค๋กœ๋Š” ํผ์ง€์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค. ์–ด๋Š ์ •๋„ ๋งž๊ฒŒ ์ดํ•ดํ•œ ๊ฒƒ ๊ฐ™์ง€๋งŒ...์ด๊ฒƒ ๋งŒ์œผ๋กœ๋Š” ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€ ๋กœ์ง์„ ์งœ๋ณด๋Š” ๊ณผ์ •์—์„œ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•˜๋ ค๋Š”๋ฐ, ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๊ฐ€ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ดํ•ดํ•ด๋ณด๊ณ ์ž ์‹œ๊ฐ„์„ ๋งŽ์ด ์“ด ๊ฒƒ ๊ฐ™์•„์š”.
๊ทธ ํ›„ ์ƒ๊ฐํ•œ ๊ฒƒ์ด, ๋ฒฝ์„ 3๊ฐœ์”ฉ ๋ฌด์กฐ๊ฑด ์„ธ์›Œ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋ฒฝ์„ ๋‘๊ณ  ๋‚˜์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋ฒฝ ์•ˆ์ชฝ์œผ๋กœ ํผ์ง€๋„๋ก ํ•œ ํ›„, ๋นˆ ์นธ์„ ๋ชจ๋‘ ์„ธ์„œ ์•ˆ์ „ ์˜์—ญ์„ ๊ตฌํ•ด ๊ทธ ํฌ๊ธฐ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋  ๊ฒƒ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
๋จผ๊ฐ€ ๊ฐ์ฒด์ง€ํ–ฅ ์ˆ˜์—…๋•Œ ๋ฐฐ์› ๋˜ ๋ฌธ์ œ๋“ค ์ค‘ ์˜ค๋ชฉ, ์˜ค์…€๋กœ ๋“ฑ๊ณผ ์œ ์‚ฌํ•œ ๋Š๋‚Œ์„ ๋ฐ›์•˜๋Š”๋ฐ...๊ธฐ์–ต์„ ๋– ์˜ฌ๋ฆฌ๋ฉด์„œ ํ•œ๋ฒˆ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค!

2. ๐Ÿ’™๋ฌธ์ œ ์„ค๋ช…๐Ÿ’™
1) ๐Ÿฆฆ์„ค๋ช…๐Ÿฆฆ
๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๊ณ ์ž ํ•จ
NxM ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์ธ ์—ฐ๊ตฌ์†Œ๋Š” ๋นˆ ์นธ, ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Œ

์ผ๋ถ€ ์นธ ๋ฐ”์ด๋Ÿฌ์Šค ์กด์žฌ(์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ๋นˆ ์นธ์œผ๋กœ ํผ์ง)
์ƒˆ๋กœ์šด ๋ฒฝ์˜ ๊ฐœ์ˆ˜ : 3๊ฐœ(๊ผญ ๋‹ค ์„ธ์›Œ์•ผ ํ•จ)

=> ๋ฒฝ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ ํ•  ๋•Œ, ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š”??

2) ๐Ÿฆฆ์กฐ๊ฑด๐Ÿฆฆ

์—ฐ๊ตฌ์†Œ์˜ ํฌ๊ธฐ : NxM ์ธ ์ง์‚ฌ๊ฐํ˜•
์ง์‚ฌ๊ฐํ˜• : 1x1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋‰จ
์—ฐ๊ตฌ์†Œ : ๋นˆ ์นธ, ๋ฒฝ(ํ•œ ์นธ ๊ฐ€๋“ ์ฑ„์›Œ์ง), ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ์ด๋ค„์ง

๋นˆ ์นธ : 0
๋ฒฝ : 1
๋ฐ”์ด๋Ÿฌ์Šค : 2
=> ๋ฒฝ ์—†์œผ๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค ๋ชจ๋“  ๋นˆ ์นธ์œผ๋กœ ํผ์ง€๊ฒŒ ๋จ

3. ๐Ÿ’™๋ฌธ์ œ ํ’€์ด ๋ฐฉ๋ฒ•๐Ÿ’™

  1. N, M ๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ
  2. NxM ํฌ๊ธฐ์˜ ์ง€๋„ ์ƒ์„ฑํ•˜๊ธฐ
  3. ๋นˆ ์นธ์— ๋ฒฝ ์„ธ์šฐ๊ธฐ(0์„ 1๋กœ ๋ฐ”๊พธ๊ธฐ) : ์ด 3๊ฐœ์˜ ๋ฒฝ => 3์ค‘ for๋ฌธ
  4. ๊ฐ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋ฒฝ์ด ์—†๋Š” ๋นˆ ์นธ์ด๋ฉด์„œ, ๋ฐ”์ด๋Ÿฌ์Šค์™€ ์ธ์ ‘ํ•œ ๋นˆ ์นธ ์กด์žฌํ•˜๋ฉด, ๊ทธ ๋นˆ ์นธ์„ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฐ”๊พธ๊ธฐ(0์„ 2๋กœ ๋ฐ”๊พธ๊ธฐ) => BFS
  5. ๋ฐ”์ด๋Ÿฌ์Šค ํผ์ง„ ํ›„, ๋‚จ์€ ๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜(์•ˆ์ „ ์˜์—ญ) ์„ธ๊ธฐ(0์˜ ๊ฐœ์ˆ˜)

4. ๐Ÿ’™ํ’€์ด ํ™•์ธ ์ฝ”๋“œ ์ถ”๊ฐ€๐Ÿ’™
1) ๐Ÿฆฆ๋งต ์ถœ๋ ฅ ํ•จ์ˆ˜ ๊ตฌํ˜„๐Ÿฆฆ

// ๋งต ์ถœ๋ ฅ ํ•จ์ˆ˜
void print_map(const vector<vector<int>>& map) {
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cout << map[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

2) ๐Ÿฆฆ๋ฉ”์ธ ํ•จ์ˆ˜ ์ฝ”๋“œ ์ถ”๊ฐ€๐Ÿฆฆ

int main() {
	cin >> N >> M;
	initial_map.resize(N, vector<int>(M));

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> initial_map[i][j];
			if (initial_map[i][j] == BLANK) {
				blank_map.push_back({ i, j }); // ๋นˆ ์นธ ์ง€๋„ ์ƒ์„ฑ
			}
		}
	}

	vector<vector<pair<int, int>>> wall_combinations = building_walls(); // ๋นˆ ์นธ์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
	int max_safe_area = 0; // ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ
	vector<vector<int>> best_virus_map; // ๋ฐ”์ด๋Ÿฌ์Šค ํผ์ง„ ํ›„ ์ง€๋„ ์ €์žฅ

	for (const auto& walls : wall_combinations) { // ๋ฒฝ 3๊ฐœ๋ฅผ ์„ธ์šด ์—ฌ๋Ÿฌ ์กฐํ•ฉ ๋ฐ˜๋ณต
		// ์ดˆ๊ธฐ ๋งต ๋ณต์‚ฌํ•ด์„œ ๊ทธ ๋งต์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๋Š” ์—ฌ๋Ÿฌ ์กฐํ•ฉ ์ค‘ ํ•œ ๊ฒฝ์šฐ ์ ์šฉ
		vector<vector<int>> map_copy = initial_map;
		for (const auto& wall : walls) { // ํ•œ ๊ฒฝ์šฐ์—์„œ ์„ธ์šด ๋ฒฝ 3๊ฐœ์˜ ๊ฐ๊ฐ ์œ„์น˜์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
			map_copy[wall.first][wall.second] = WALL; // ๋ฒฝ ์„ธ์šฐ๊ธฐ
		}

		bfs(map_copy); // ๋ฐ”์ด๋Ÿฌ์Šค ์ฃผ๋ณ€ ์นธ ๋นˆ ์นธ์ด๋ผ๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆ์ง

		int safe_area = calculate_safe_area(map_copy); // ์•ˆ์ „์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ
		if (safe_area > max_safe_area) {
			max_safe_area = safe_area;
			best_virus_map = map_copy;
		}
	}

	cout << "๋ฒฝ์„ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ์ง€๋„" << endl;
	print_map(best_virus_map);
	cout << max_safe_area << endl;
	return 0;
}

๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ํ›„์˜ ์—ฌ๋Ÿฌ ์ง€๋„ ๋ชจ์Šต ์ค‘ ์ตœ์ ์˜ ์ง€๋„๋ฅผ ์ €์žฅํ•˜๋Š” ์ง€๋„๋ฅผ ์ •์˜ํ•œ ํ›„, ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€์ผ ๋•Œ์˜ ์ง€๋„๋ฅผ ๊ตฌํ•˜์—ฌ ๊ทธ ์ง€๋„๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด, ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ์ง์ ‘ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒฝ์„ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ์ง€๋„๋ฅผ ํ™•์ธํ•˜์—ฌ ์•ˆ์ „ ์˜์—ญ์ด ์ž˜ ๊ตฌํ•ด์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ์—์„œ ์—ฐ๊ตฌ์†Œ์˜ ๋ชจ์Šต์ด

2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

์ด๋Ÿฌํ•  ๋•Œ, ๋ฒฝ์„ ์„ธ์šด ๋’ค ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ์—ฐ๊ตฌ์†Œ ๋ชจ์Šต์„

2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

๋ผ๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด,
image
์œ„์˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ž˜ ๊ตฌํ•ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

5. ๐Ÿ’™์ตœ์ข… ์ฝ”๋“œ๐Ÿ’™

๐Ÿฆฆ์ตœ์ข… ์ฝ”๋“œ ํ™•์ธ๐Ÿฆฆ
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

// ๋ฉ”ํฌ๋กœ ์„ค์ •
#define BLANK 0
#define WALL 1
#define VIRUS 2

int N, M; // ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ, ๊ฐ€๋กœ ํฌ๊ธฐ
vector<vector<int>> initial_map; // ์ดˆ๊ธฐ ์ง€๋„
vector<pair<int, int>> blank_map; // ๋นˆ ์นธ
int directions[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ๋ฐฉํ–ฅ

// ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฒฝ ์„ธ์šฐ๊ธฐ ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ
vector<vector<pair<int, int>>> building_walls() {
	vector<vector<pair<int, int>>> combinations;
	int size = blank_map.size(); // ๋นˆ ์นธ ํฌ๊ธฐ
	for (int i = 0; i < size; i++) {
		for (int j = i + 1; j < size; j++) {
			for (int k = j + 1; k < size; k++) {
				// ๋นˆ ์นธ์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๊ธฐ
				combinations.push_back({blank_map[i], blank_map[j], blank_map[k]});
			}
		}
	}
	return combinations;
}

// ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ ํ์— ์ €์žฅ ํ›„, 
// ๋ฐ”์ด๋Ÿฌ์Šค ์นธ ์ฃผ๋ณ€ ์นธ์ด ๋ฐ”์ด๋Ÿฌ์Šค ์•„๋‹Œ ๋นˆ ์นธ์ด๋ผ๋ฉด,
// ์ฃผ๋ณ€ ์นธ์„ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฐ”๊พธ๊ธฐ)
void bfs(vector<vector<int>>& map_copy) {
	queue<pair<int, int>> q;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (map_copy[i][j] == VIRUS) {
				q.push({i, j});
			}
		}
	}
	// ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ์ด ์•„๋‹ˆ๊ณ  ๋นˆ ์นธ์ด๋ผ๋ฉด,
	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for (int d = 0; d < 4; d++) { // ๋ฐฉํ–ฅ ํƒ์ƒ‰
			int nx = x + directions[d][0];
			int ny = y + directions[d][1];
			if (nx >= 0 && nx < N && ny >= 0 && ny < M && map_copy[nx][ny] == 0) {
				map_copy[nx][ny] = VIRUS; // ๋นˆ ์นธ์„ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฐ”๊พธ๊ธฐ
				q.push({nx, ny});
			}
		}
	}
}

// ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ ํ•จ์ˆ˜
int calculate_safe_area(vector<vector<int>>& map_copy) {
	int safe_area = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (map_copy[i][j] == BLANK) { // ๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜
				safe_area++;
			}
		}
	}
	return safe_area;
}

int main() {
	cin >> N >> M;
	initial_map.resize(N, vector<int>(M));

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> initial_map[i][j];
			if (initial_map[i][j] == BLANK) {
				blank_map.push_back({ i, j }); // ๋นˆ ์นธ ์ง€๋„ ์ƒ์„ฑ
			}
		}
	}

	vector<vector<pair<int, int>>> wall_combinations = building_walls(); // ๋นˆ ์นธ์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
	int max_safe_area = 0; // ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ

	for (const auto& walls : wall_combinations) { // ๋ฒฝ 3๊ฐœ๋ฅผ ์„ธ์šด ์—ฌ๋Ÿฌ ์กฐํ•ฉ ๋ฐ˜๋ณต
		// ์ดˆ๊ธฐ ๋งต ๋ณต์‚ฌํ•ด์„œ ๊ทธ ๋งต์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๋Š” ์—ฌ๋Ÿฌ ์กฐํ•ฉ ์ค‘ ํ•œ ๊ฒฝ์šฐ ์ ์šฉ
		vector<vector<int>> map_copy = initial_map;
		for (const auto& wall : walls) { // ํ•œ ๊ฒฝ์šฐ์—์„œ ์„ธ์šด ๋ฒฝ 3๊ฐœ์˜ ๊ฐ๊ฐ ์œ„์น˜์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
			map_copy[wall.first][wall.second] = WALL; // ๋ฒฝ ์„ธ์šฐ๊ธฐ
		}

		bfs(map_copy); // ๋ฐ”์ด๋Ÿฌ์Šค ์ฃผ๋ณ€ ์นธ ๋นˆ ์นธ์ด๋ผ๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆ์ง

		int safe_area = calculate_safe_area(map_copy); // ์•ˆ์ „์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ
		max_safe_area = max(max_safe_area, safe_area);
	}

	cout << max_safe_area << endl;
	return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

BFS๋ฅผ ์ด์‚ฐ์ˆ˜ํ•™ ์‹œ๊ฐ„์— ๋ฐฐ์›Œ์„œ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์˜ ์›๋ฆฌ๋ฅผ ์•Œ๊ณ ๋Š” ์žˆ์—ˆ์ง€๋งŒ,,, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์— ์ง์ ‘ ์ ์šฉํ•ด ๋ณธ ๊ฒƒ์€ ์ฒ˜์Œ์ด์—ˆ์Šต๋‹ˆ๋‹ค.
bfs ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ, ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํƒ์ƒ‰ ์ง€์ ์„ ์ •ํ•œ ํ›„, ์‹œ์ž‘ ์ ์—์„œ๋ถ€ํ„ฐ ๊ฐ™์€ ๋ ˆ๋ฒจ์˜ ๋ชจ๋“  ์ง€์ ์„ ํƒ์ƒ‰ํ•œ ํ›„, ๊ทธ ๋‹ค์Œ ์ธ์ ‘ ์ง€์ ์„ ํƒ์ƒ‰ํ•˜๋„๋ก ํ•˜์—ฌ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๋งŒ ํ์— ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. BFS๋ฅผ ์ด์šฉํ•˜์—ฌ ์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
vector์—์„œ resize๋Š” ์ฒ˜์Œ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋Š”๋ฐ, ์ด๋ฅผ ํ†ตํ•ด initial_map ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ N์œผ๋กœ ์„ค์ •ํ•˜๊ณ , ๊ฐ ์š”์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ M์ธ ๋ฒกํ„ฐ๋กœ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 2์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋˜ํ•œ queue๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ, ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ๋“ค์„ ์ฐจ๋ก€๋กœ ์ฐธ์กฐํ•˜์—ฌ ๊ทธ ์นธ์—์„œ ์ธ์ ‘ํ•œ ์นธ๋“ค์ด ๋นˆ ์นธ์ธ์ง€ ํ™•์ธํ•˜๋ฉด์„œ ๋ฐฉํ–ฅ์„ ํƒ์ƒ‰ํ•ด ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋ฉด ๊ทธ ์นธ๋„ ๋ฐ”์ด๋Ÿฌ์Šค ์นธ์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค ๋ฐ”๋€Œ๋Š” ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•˜์˜€๋Š”๋ฐ, ๊ทธ ๋•Œ ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด front๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ตฌํ˜„ ๋ฌธ์ œ... ์‰ฝ๊ณ  ์žฌ๋ฏธ์žˆ๋Š” ๊ฒƒ๋งŒ ์žˆ๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ... ์ œ ์˜ค๋งŒ๊ณผ ํŽธ๊ฒฌ์ด์—ˆ๊ตฐ์š”...
์ˆ˜ํ˜„ ๋‹˜์€ bfs๋กœ ํ‘ธ์…จ๋„ค์š”! ๋ฒฝ์„ ์„ธ์šฐ๋Š” ์กฐํ•ฉ์„ combinations ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜์…จ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ์žฌ๊ท€์ ์œผ๋กœ ์‹œ๋„ํ•˜๋Š” ๋ฐฉ์‹์ธ ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ํƒ์ƒ‰ํ•ด ์ตœ์ ํ•ด๋ฅผ ์ฐพ๊ณ , ๋ถˆํ•„์š”ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋ฐฐ์ œํ•ด ๊ณ„์‚ฐ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด๋ฒˆ pr๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š” ใ…Ž.ใ…Ž

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ํ’€์–ด๋ณด๊ณ  ๋ฆฌ๋ทฐํ•˜๋ ค๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ ๋„ˆ๋ฌด ๋Šฆ๊ฒŒ ๋ฆฌ๋ทฐ๋‹ฌ์•˜๋„ค์š”. ์ด์ œ๋ถ€ํ„ฐ ์ข€ ๋” ๋น ๋ฅด๊ฒŒ ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
๊ตฌํ˜„ํ• ๋•Œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ์ดํ›„์˜ ๋งต์„ ์ถœ๋ ฅํ•˜๋ฉด์„œ ํ‘ธ์‹ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด๋Ÿฐ ๋””๋ฒ„๊น… ๋ฐฉ๋ฒ• ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค.
์ €๋„ ์ด๋Ÿฐ ๊ตฌํ˜„๋ฌธ์ œ ํ’€๋•Œ ๋””๋ฒ„๊น…ํ•˜๋ฉด์„œ ํ’€์–ด์•ผ ์‹ค์ˆ˜๊ฐ€ ์ ๋”๋ผ๊ตฌ์š”๐Ÿ˜„

์ €๋Š”

  1. ๋ชจ๋“  ๊ฒฝ์šฐ๋กœ ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๊ธฐ
  2. ๋ฐ”์ด๋Ÿฌ์Šค ํผํŠธ๋ฆฌ๊ณ  ์•ˆ์ „์˜์—ญ ๊ตฌํ•˜๊ธฐ -> ์ตœ๋Œ“๊ฐ’์ด๋ฉด ๊ฐฑ์‹ 
  3. ์„ธ์šด ๋ฒฝ 3๊ฐœ ๋ณต๊ตฌํ•˜๊ธฐ
    ๋ฅผ ์ค‘์ ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์—์„œ ์กฐ๊ธˆ ๋‹ค๋ฅด๋„ค์š”!
๋˜ ์ˆ˜ํ˜„๋‹˜์€ ๊ธฐ์กด ๋งต์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์…จ๋Š”๋ฐ, ์ €๋Š” ๊ธฐ์กด ๋งต์—์„œ ๋ฒฝ์„ 3๊ฐœ ์„ธ์šฐ๊ณ  ์•ˆ์ „์˜์—ญ์„ ๊ณ„์‚ฐํ•œ ์ดํ›„ ๋‹ค์‹œ ์„ธ์šด ๋ฒฝ์„ ์ง€์šฐ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

const int WAYS = 4;
const int WAY[4][2] = { { -1, 0 },
                        { 1, 0 },
                        { 0, -1 },
                        { 0, 1 }
};

int map[8][8], visited[8][8], N, M, safeArea;
vector<pair<int, int>> blanks, virus;

void dfs(int y, int x) {
    for (int i = 0; i < WAYS; i++) {
        int ny = y + WAY[i][0];
        int nx = x + WAY[i][1];
        if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue;
        if (visited[ny][nx] == 1 || map[ny][nx] == 1) continue;
        visited[ny][nx] = 1;
        dfs(ny, nx);
    }
}

int Spread() {
    memset(visited, 0, sizeof(visited));
    for (pair<int, int> a : virus) {
        visited[a.first][a.second] = 1;
        dfs(a.first, a.second);
    }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (map[i][j] == 0 && visited[i][j] == 0) cnt++;
        }
    }
    return cnt;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
            if (map[i][j] == 0) blanks.push_back({ i, j });
            if (map[i][j] == 2) virus.push_back({ i, j });
        }
    }
    //1. ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๊ธฐ
    int l = blanks.size();
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < i; j++) {
            for (int k = 0; k < j; k++) {
                map[blanks[i].first][blanks[i].second] = 1;
                map[blanks[j].first][blanks[j].second] = 1;
                map[blanks[k].first][blanks[k].second] = 1;

                //2. ๋ฐ”์ด๋Ÿฌ์Šค ํ™•์‚ฐ์‹œํ‚ค๊ณ  ์•ˆ์ „์˜์—ญ์„ ๊ณ„์‚ฐ
                safeArea = max(safeArea, Spread());

                //3. ๋ฒฝ ์›์ƒ๋ณต๊ตฌ
                map[blanks[i].first][blanks[i].second] = 0;
                map[blanks[j].first][blanks[j].second] = 0;
                map[blanks[k].first][blanks[k].second] = 0;
            }
        }
    }
    cout << safeArea << "\n";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants