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

5-g0rnn #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

5-g0rnn #17

wants to merge 1 commit into from

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Nov 1, 2024

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

์—ฐ๊ตฌ์†Œ

๋ฌธ์ œ๊ฐ€ ์–ด๋–ค ์œ ํ˜•์ธ์ง€ ์ˆจ๊ธฐ๊ธฐ ์œ„ํ•ด ๋ฌธ์ œ์˜ ์ด๋ฆ„๋งŒ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

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

1h

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

1. ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ์„ ํ์— ๋„ฃ๋Š”๋‹ค.
  1-1. ๋™์‹œ์— ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ์˜ ๊ฐœ์ˆ˜์™€ ๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค.
2. ๋ฒฝ์„ 3๊ฐœ ์„ค์น˜ํ•œ๋‹ค.
    2-1. ๋ฒฝ์ด ๋ชจ๋‘ ์„ค์น˜๋˜๋ฉด bfs๋ฅผ ํ†ตํ•ด ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๊ฐ์—ผ์‹œํ‚จ๋‹ค.
3. ์ ์ ˆํ•œ ๊ฐ’์„ ์—ฐ์‚ฐํ•˜์—ฌ ์•ˆ์ „ํ•œ ๊ตฌ์—ญ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ผ๋‹ค.

์œ„์˜ ์ˆ˜๋„์ฝ”๋“œ๋Š” ์ฝ”๋“œ์˜ ์ „๋ฐ˜์ ์ธ ํ๋ฆ„์ž…๋‹ˆ๋‹ค. ์•„๋งˆ ๋„ˆ๋ฌด ์ถ”์ƒ์ ์ธ ๋‚ด์šฉ์ด๋ผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ์—” ๋ณ„ ๋„์›€์ด ์•ˆ๋ ๋“ฏํ•ฉ๋‹ˆ๋‹ค.

ํ•™์Šต์„ ์œ„ํ•ด ๋ฌธ์ œ๋ฅผ ์‹œ๋„ํ•ด๋ณด๊ณ  ํ•ด์„ค์„ ๋ด์ฃผ์„ธ์šฉ!

์ด ๋ฌธ์ œ๋Š” bfs์™€ dfs, ๋ฐฑ ํŠธ๋ ˆํ‚น์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ์˜ ํ๋ฆ„์— ๋งž๊ฒŒ dfs/๋ฐฑํŠธ๋ž˜ํ‚น, bfs์ˆœ์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฒฝ ์„ค์น˜ํ•˜๊ธฐ

์ €๋Š” ์—ฌ๊ธฐ์„œ ๋ง‰ํ˜€์„œ ๊ตฌ๊ธ€๋งํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒฝ์„ ์„ค์น˜ํ•˜๋”๊ตฐ์š”.!

void dfs(int wall) {
	if (wall == 3) {
		bfs();
		return;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (lab[i][j] == EMPTY) {
				lab[i][j] = WALL;
				dfs(wall + 1);
				lab[i][j] = EMPTY;
			}
		}
	}
}

์ด๋Š” ์ถ”์ƒํ™”ํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค.

dfs -> ๋ฐ”์ด๋Ÿฌ์Šค ์ „์—ผ์‹œํ‚ค๊ธฐ or ๋ฒฝ์„ 1๊ฐœ์”ฉ ์„ค์น˜ํ•˜๊ธฐ

dfs๋Š” ์œ„์™€ ๊ฐ™์ด ๋‘๊ฐ€์ง€ ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์ •ํ–ˆ์œผ๋ฉด dfs๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฏฟ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

for๋ฌธ ๋‚ด๋ถ€์—์„œ dfs๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ด์œ ๋Š” ๋ฒฝ์„ 1๊ฐœ์”ฉ ์„ค์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋งจ ์œ„ if๋ฌธ์—์„œ๋Š” ๋ฒฝ์„ ๋ชจ๋‘ ์„ค์น˜ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „์—ผ์‹œํ‚ค๊ธฐ ์œ„ํ•ด bfs๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

๋ฐ”์ด๋Ÿฌ์Šค ์ „์—ผ ์‹œํ‚ค๊ธฐ

์ด๋Š” bfs๋กœ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋ชจ๋“  bfs๋Š” ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊ธฐ์–ตํ•œ๋‹ค๋ฉด bfs๋„ ์‚ฌ์‹ค ์–ด๋ ค์šด๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค.
์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ if (lab[i][j] == VIRUS) ๋กœ ๊ฒ€์‚ฌํ•˜๊ณ  ๋ฐ”์ด๋Ÿฌ์Šค ์นธ์„ ํ์— ๋„ฃ์Šต๋‹ˆ๋‹ค. (ํ•ด๋‹น๋ฌธ์ œ์—์„œ๋Š” ์ขŒ๋ฃŒ๊ฐ’์„ ๋„ฃ๊ธฐ ์œ„ํ•ด pair๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.)

ํ์— ์ „์ฒ˜๋ฆฌ(๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ์‚ฝ์ž…!)๊ฐ€ ๋œ ํ›„์—” while(!q.empty())๋ฅผ ํ†ตํ•ด ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒŒ ๊ตญ๋ฃฐ์ž…๋‹ˆ๋‹ค.

๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์— ์ฒ˜์Œ ๋“ค์–ด์™”๋‹ค๋ฉด ๋ฐฉ๋ช…๋ก์„ ์ ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด ํ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ pop์„ ํ†ตํ•ด ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ์— ๊ฐ’์„ ๋„ฃ๋Š” ๊ฒƒ๋Š” ํƒ์ƒ‰(๊ฒ€์‚ฌ)ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•ญ๋ชฉ์„ ๋„“๊ฒŒ ์ „๊ฐœํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ˜๋ณต๋ฌธ์ด ํ•œ๋ฒˆ ์‹คํ–‰๋œ๋‹ค๋ฉด ํ•ด๋‹น ํ•ญ๋ชฉ์„ ํ์—์„œ ์ œ๊ฑฐํ•ด์•ผ๊ฒ ์ฃ ? ๐Ÿ˜Ž ๋ฌผ๋ก  ๋งˆ์ง€๋ง‰์— ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

์•„๋ฌดํŠผ ๋ฐฉ๋ช…๋ก์„ ์ž‘์„ฑํ•˜๊ณ  ๋‚˜๋ฉด ๊ฐ ๋ฐฉํ–ฅ์— ๋งž๊ฒŒ ๊ฒ€์‚ฌํ•ด์ค๋‹ˆ๋‹ค.

		for (int dir = 0; dir < 4; dir++) {
			int nx = x + offset[dir][0];
			int ny = y + offset[dir][1];
			if (canMove(tmp_lab, nx, ny)) {
				tmp_lab[ny][nx] = VISITED;
				tmpq.push(make_pair(nx, ny));
			}
		}

์œ„ ์ฝ”๋“œ๋„ ๊ตญ๋ฃฐ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ฐฉํ–ฅ(dir์€ ๋ฐฉํ–ฅ์„ ๋œปํ•จ)๋งˆ๋‹ค ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ์ด๋™ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

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

๋ฐฑํŠธ๋ ˆํ‚น์„ ๋ณต์Šตํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์•ˆ์ „ํ•œ ๊ตฌ์—ญ์„ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์น™์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ ์ด์ค‘ for๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ์ผ์ผ์ด ๊ฒ€์‚ฌํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค!!
์ด ๋ฐฉ๋ฒ•(์ค‘์ฒฉ for)์ด ๋ช…๋ฃŒํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ๋” ์‰ฌ์šธ๊ฒ๋‹ˆ๋‹ค!!

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

์ €ํ•œํ…Œ๋Š” ์ข€ ์–ด๋ ค์šด ๋ฌธ์ œ์˜€๋„ค์š”...
(๋ณธ๊ฐ€ ๋””๋ฒ„ํ”„๋กœ 2์‹œ๊ฐ„ ์ด์ƒ ๊ณ ๋ฏผํ–ˆ์Šต๋‹ˆ๋‹ค.)

2์ฐจ์› ๋ฐฐ์—ด ๊ณต๊ฐ„์ด ์ž‘๊ฒŒ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฑธ๋ณด๊ณ 

๋นˆ ๊ณต๊ฐ„์„ ๋ฒฝ์œผ๋กœ 3๊ฐœ ๋งŒ๋“ค๊ณ  (<--๋ธŒ๋ฃจํŠธํฌ์Šค),
๋ฒฝ 3๊ฐœ๋ฅผ ์ถ”๊ฐ€ํ•œ 2์ฐจ์› ๋ฐฐ์—ด์„ ํƒ์ƒ‰ ํ•ด์•ผ๊ฒ ๋‹ค (<--BFS)๏ฟฝ
๊ทธ๋ฆฌ๊ณ  ๊ฐ BFS์—์„œ๋Š” temp๊ฐ’๋“ค์„ ์จ์•ผ๊ฒ ๋‹ค ๋ผ๋Š” ๋ฐฉ๋ฒ• ๊นŒ์ง€๋Š” ์ƒ๊ฐํ•ด๋ƒˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ DFS๋กœ ๋ฒฝ 3๊ฐœ๋ฅผ ๋ธŒ๋ฃจํŠธํฌ์Šค ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ๊ฒŒ ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋„ค์š”.
๊ท ํ˜ธ๋‹˜ ์ฝ”๋“œ ์Šฌ์ฉ ๋ดค๋Š”๋ฐ ์Šฅ ๋ณด์ž๋งˆ์ž '์•„!' ํ•˜๋ฉฐ ์ดํ•ดํ•ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

#define EMPTY 0
#define WALL 1
#define VIRUS 2
#define VISITED 3

using namespace std;

int N, M;
int safeZone = 0, virusZone = 0, maxSafeZone = 0;
vector<vector<int>> lab;
queue<pair<int, int>> q;

int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

bool isMoveable(vector<vector<int>> &tempLab, int x, int y) {
    return 0 <= x && x < M && 0 <= y && y < N && tempLab[y][x] == EMPTY;
}

void bfs() {
    int cntVirus = 0;
    vector<vector<int>> tempLab = lab;
    queue<pair<int, int>> tempQ = q;

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

        for (int dir = 0; dir < 4; dir++) {
            int y_ = y + offset[dir][0];
            int x_ = x + offset[dir][1];

            if (isMoveable(tempLab, x_, y_)) {
                tempLab[y_][x_] = VISITED;
                tempQ.push(make_pair(y_, x_));
            }
        }
    }
    maxSafeZone = max(maxSafeZone, safeZone - 3 - cntVirus + virusZone);
}

void dfs(int wall) {
    if (wall == 3) {
        bfs();
        return;
    }

    for (int y = 0; y < N; y++) {
        for (int x = 0; x < M; x++) {
            if (lab[y][x] == EMPTY) {
                lab[y][x] = WALL;
                dfs(wall + 1);
                lab[y][x] = EMPTY;
            }
        }
    }
}

int main() {
    // 1) input
    cin >> N >> M;

    int temp;
    lab = vector<vector<int>>(N, vector<int>(M));
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < M; x++) {
            cin >> temp;
            lab[y][x] = temp;

            if (temp == VIRUS) {
                q.push(make_pair(y, x));
                virusZone++;
            }
            if (temp == EMPTY) safeZone++;
        }
    }

    // 2) solution
    dfs(0);

    cout << maxSafeZone;
}

Comment on lines +49 to +51
// ํ์—์„œ ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ all_virus๋ฅผ ๋นผ๋ฉด ๊ธฐ์กด virus๋ฅผ ๋‹ค์‹œ ๋”ํ•ด์•ผํ•œ๋‹ค.
// ๋”ฐ๋ผ์„œ empty์˜ ๊ฐœ์ˆ˜ - ์ถ”๊ฐ€ํ•œ wall๊ฐœ์ˆ˜ - ๋ชจ๋“  virus ์ˆ˜ + ๊ธฐ์กด virus ์ˆ˜
maxSafe = max(maxSafe, safe - 3 - all_virus + virus);
Copy link
Collaborator

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ๊ฐ€ ํ•ต์‹ฌ์ธ ๋“ฏ ํ•˜๋„ค์š”.
์ฃผ์„ ์„ค๋ช…์ด ์นœ์ ˆํ•ด์„œ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ตฟ๊ตฟ๊ตฟ

Comment on lines +84 to +97
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> lab[i][j];
if (lab[i][j] == VIRUS)
{
q.push(make_pair(j, i)); // (x, y) ๊ผด๋กœ ์ €์žฅ
virus++;
}
if (lab[i][j] == EMPTY)
safe++;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

TMI: ์ €๋Š” ์ด๋Ÿฐ ์ขŒํ‘œ๋ฌธ์ œ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ for๋ฌธ๋„ y, x๋กœ ๋‘๊ณ  ํ’‰๋‹ˆ๋‹ค. ์ข‹์•„์š”.

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.

2 participants