-
Notifications
You must be signed in to change notification settings - Fork 0
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
5-g0rnn #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฌ๊ธฐ์ ๋ฒฝ์ ์ธ์ฐ๊ณ dfs๋ฅผ ๋๋ฆฌ๊ณ ๋์ ๋ฒฝ์ ๋ค์ ์ ๊ฑฐํ๋ ์ด์ ๋ฅผ ํผ์ ์๊ฐํด๋ดค์ผ๋ฉด ์ดํดํ๊ธฐ ์ด๋ ค์ ์ ๊ฒ ๊ฐ์๋ฐ ์ ๋ฒ ๋ฏธํ
์์ ์ค๋ช
์ ์ ํด์ฃผ์
์ ์ฝ๊ฒ ์ดํดํ ์ ์์์ต๋๋ค. |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TMI: ์ ๋ ์ด๋ฐ ์ขํ๋ฌธ์ ํท๊ฐ๋ฆฌ์ง ์๊ธฐ ์ํด์ for๋ฌธ๋ y, x๋ก ๋๊ณ ํ๋๋ค. ์ข์์. |
||
|
||
dfs(0); | ||
cout << maxSafe; | ||
|
||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๊ธฐ๊ฐ ํต์ฌ์ธ ๋ฏ ํ๋ค์.
์ฃผ์ ์ค๋ช ์ด ์น์ ํด์ ์ฝ๊ฒ ์ดํดํ ์ ์์์ต๋๋ค. ๊ตฟ๊ตฟ๊ตฟ