๋ฌธ์ :
๋ถ์ :
c++ ์ฝ๋ :
//
// 14502_lab.cpp
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2021/02/07.
//
#include <stdio.h>
#include <iostream>
#include <utility>
#include <vector>
#include <queue>
#include <math.h>
#include <string.h>
using namespace std;
int arr[8][8];
int c_arr[8][8];
int N, M;
int zero_cnt;
int ans;
bool check[64]; //๋ฒฝ(1)์ ๋ง๋ค์ด ๋ดค๋์ง ์๋์ง ๊ฒ์ฌ
bool visit[8][8]; //๋ฐ์ด๋ฌ์ค(2)๋ฅผ ๋ง๋ค์ด ๋ดค๋์ง ์๋์ง ๊ฒ์ฌ
vector<pair<int, int>> virusArr;
vector<pair<int, int>> emptyArr;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
void make_map(){
cin >> N >> M;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cin >> arr[i][j];
//cout << arr[i][j] << " ";
if(arr[i][j] == 0){//๋น์นธ
emptyArr.push_back(make_pair(i, j)); // 0 ์ขํ
}else if(arr[i][j] == 2){//๋ฐ์ด๋ฌ์ค
virusArr.push_back(make_pair(i,j)); // 2 ์ข์ตธ
}
}
}
zero_cnt = (int)emptyArr.size();
//cout << "zero_cnt : " << zero_cnt << endl;
}
void make_cmap(){
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
c_arr[i][j] = arr[i][j];
}
}
}
void make_BFS(int x, int y){
queue<pair<int, int>> virusQ;
virusQ.push(make_pair(x, y)); // ์ฒ์ ์์น์ ๋ฐ์ด๋ฌ์ค๋ฅผ ์ง์ด ๋ฃ์
visit[x][y] = true;
while(virusQ.empty() == 0){ //not empty ์ผ ๊ฒฝ์ฐ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๋
ธ๋๊ฐ ์๋ ๊ฒ์ด๋ฏ๋ก 2๋ก ๋ง๋ค์ด ์ค์ผ ํจ
int x = virusQ.front().first;
int y = virusQ.front().second;
virusQ.pop();
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && ny >= 0 && nx < N && ny < M){
if(visit[nx][ny] == false && c_arr[nx][ny] == 0){ // ๋ฐฉ๋ฌธ x์ด๊ณ 0(๋น์นธ)์ด๋ผ๋ฉด
c_arr[nx][ny] = 2; // ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ฃ์ด์ค
visit[nx][ny] = true;
virusQ.push(make_pair(nx, ny));
}
}
}
}
}
int safe_area(){
int cnt = 0;
for(int i =0; i <N; i++){
for(int j = 0; j <M; j++){
if(c_arr[i][j] == 0) cnt++;
}
}
return cnt;
}
void bfs_mk_virus(){
int count = 0;
make_cmap();
memset(visit, false, sizeof(visit));//visit ๋ฐฐ์ด์ false๋ก ๋ชจ๋ ์ด๊ธฐํ
//์ด๊ธฐ ์์
- ๋ฒฝ 1์ ์ธ์์ค
for(int i = 0; i< zero_cnt; i++){
if(count == 3) {
//cout << "get break" << endl;
break;
}
if(check[i] == true){ //1์ด ์๋ ๊ณณ์ด๋ผ๋ฉด
int x = emptyArr[i].first;
int y = emptyArr[i].second;
c_arr[x][y] = 1;// c_arr ์ 1 ์ธ์
count ++;
}
}
for(int i = 0; i < (int)virusArr.size(); i++){ // ๋ฐ์ด๋ฌ์ค ๊ฐฏ์๋งํผ
int x = virusArr[i].first;
int y = virusArr[i].second;
make_BFS(x, y); //๋ฐ์ด๋ฌ์ค ์๋ ์์น์์ ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฐ๋ค.
}
int temp_ans = safe_area();
ans = max(ans, temp_ans);
}
void dfs_mk_wall(int index, int count){ // brute force
if(count == 3){// ๋ฒฝ 3๊ฐ๋ฅผ ๋ค ๋ง๋ค๋ฉด
bfs_mk_virus();
//cout << "made wall " << index << endl;
return;
}
for(int i = index; i < zero_cnt; i++){// 0์ด ์๋ ๋งํผ ๋ฐ๋ณตํ์ฌ ๋ฒฝ(1)์ ์ธ์ด๋ค.
if(check[i] == true){ //๋ฒฝ์ ์ธ์ ๋ ๊ณณ์ด๋ผ๋ฉด
continue; // ๊ฑ ๋๊น
}
check[i] = true;
dfs_mk_wall(i, count + 1); // dfs๋ก ๋ฒฝ์ ์ธ์ด๋ค
check[i] = false; // ๋งํ์ ๊ฒฝ์ฐ ๋๋์ ์จ๋ค.
}
}
int main(){
make_map();
dfs_mk_wall(0,0); //{0,0}์์น ๋ถํฐ 1๋ก ๋ฒฝ ์ธ์ฐ๊ธฐ
cout << ans << endl;
return 0;
}
'Algorithm๐ฐ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1477 ํด๊ฒ์ ์ธ์ฐ๊ธฐ (0) | 2021.04.05 |
---|---|
[๋ฐฑ์ค] ์น์ฆ 2636 bfs (0) | 2021.04.02 |
[๋ฐฑ์ค] 11779 ์ต์ ๋น์ฉ ๊ตฌํ๊ธฐ2 dijkstra (0) | 2021.03.10 |
[๋ฐฑ์ค] 1916 ์ต์๋น์ฉ ๊ตฌํ๊ธฐ dijkstra (0) | 2021.03.10 |
[๋ฐฑ์ค] 1753 ์ต๋จ ๊ฒฝ๋ก dijkstra (0) | 2021.03.10 |
๋๊ธ