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

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions g0rnn/DFS,BFS/5-g0rnn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

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

using namespace std;

int n, m;
int safe = 0, virus = 0;
int maxSafe = INT_MIN;
vector<vector<int>> lab;
queue<pair<int, int>> q;
int offset[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool canMove(const vector<vector<int>> &tmp, int x, int y)
{
return x >= 0 && x < m && y >= 0 && y < n && tmp[y][x] == EMPTY;
}

void bfs()
{
int all_virus = 0;
vector<vector<int>> tmp_lab = lab;
queue<pair<int, int>> tmpq = q;
while (!tmpq.empty())
{
int x = tmpq.front().first;
int y = tmpq.front().second;
all_virus++;
tmpq.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));
}
}
}

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

Choose a reason for hiding this comment

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

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

}

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;
}
}
}
}
Comment on lines +62 to +74
Copy link
Collaborator

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ์„œ ๋ฒฝ์„ ์„ธ์šฐ๊ณ  dfs๋ฅผ ๋Œ๋ฆฌ๊ณ  ๋‚˜์„œ ๋ฒฝ์„ ๋‹ค์‹œ ์ œ๊ฑฐํ•˜๋Š” ์ด์œ ๋ฅผ ํ˜ผ์ž ์ƒ๊ฐํ•ด๋ดค์œผ๋ฉด ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์ €๋ฒˆ ๋ฏธํŒ…์—์„œ ์„ค๋ช…์„ ์ž˜ ํ•ด์ฃผ์…”์„œ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค~


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

cin >> n >> m;
lab = vector<vector<int>>(n, vector<int>(m));
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++;
}
}
Comment on lines +84 to +97
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๋กœ ๋‘๊ณ  ํ’‰๋‹ˆ๋‹ค. ์ข‹์•„์š”.


dfs(0);
cout << maxSafe;

return 0;
}
1 change: 1 addition & 0 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
| 2์ฐจ์‹œ | 2024.10.03 | ํŠธ๋ฆฌ | [LCA2](https://school.programmers.co.kr/learn/courses/30/lessons/176963) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/7 |
| 3์ฐจ์‹œ | 2024.10.04 | ๋ฌธ์ž์—ด | [์žƒ์–ด๋ฒ„๋ฆฐ ๊ด„ํ˜ธ](https://www.acmicpc.net/problem/1541) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/9 |
| 4์ฐจ์‹œ | 2024.10.11 | ๋ธŒ๋ฃจํŠธํฌ์Šค | [์˜ํ™”๊ฐ๋… ์ˆŒ](https://www.acmicpc.net/problem/1436) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/15 |
| 5์ฐจ์‹œ | 2024.11.01 | BFS/DFS | [์—ฐ๊ตฌ์†Œ](https://www.acmicpc.net/problem/14502) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/17 |

---