Skip to content
Open
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
127 changes: 127 additions & 0 deletions s13121312/๊ตฌํ˜„/14502.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include<bits/stdc++.h>
using namespace std;
int n, m;
int arr[10][10];
bool vis[10][10]; //๋ฒฝ ์„ธ์›Œ์ง„์ ์žˆ๋˜๊ณณ ์ฒดํฌ
int ans = 0; //์•ˆ์ „ ์˜์—ญ ์ตœ๋Œ€ ํฌ๊ธฐ
vector<pair<int,int> > builtwall; //๋ฒฝ์œผ๋กœ ์ง€์ •๋œ ์ขŒํ‘œ ์ €์žฅ
int dx[] = {1,0,-1, 0};
int dy[] = {0, 1, 0, -1};
int ansarr[10][10]; //๋””๋ฒ„๊น…์šฉ ์ •๋‹ต arr



void input() {
cin >> n >>m;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
cin >> arr[i][j];
}
}
}

//๋ฐ”์ด๋Ÿฌ์Šค ํผ๋œจ๋ฆฌ๊ธฐ
int bfs(int tmp[10][10]) {

bool chk[10][10];
for(int i=0;i<n;i++) fill(chk[i], chk[i]+m, false);

for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(tmp[i][j] == 2) {
queue<pair<int, int> > q;
q.push({i, j});
chk[i][j] = true;

while(!q.empty()) {
auto cur = q.front();
q.pop();
for(int d=0;d<4;d++) {
int nx = cur.first + dx[d];
int ny = cur.second + dy[d];
if(nx<0 || nx>=n || ny<0 || ny>=m)continue;
if(chk[nx][ny] || tmp[nx][ny] == 1)continue;

tmp[nx][ny] = 2;
q.push({nx,ny});
chk[nx][ny] = true;
}
}
}
}
}


int con = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(tmp[i][j] == 0)con++;
}
}

return con;
}

//๋ฒฝ 3๊ฐœ ์„ธ์šฐ๊ธฐ
void buildwall(int idx, int depth) {

int x = idx/m, y =idx%m; //



if(depth == 3) {
//tmp๋กœ ๋ฒฝ์ƒ๊ธด๊ฑฐ + ์›๋ž˜ arr๋ถ™์ด๊ธฐ
int tmp[10][10];

for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
tmp[i][j] = arr[i][j];
}
}
//builtwall์ขŒํ‘œ๋กœ ๋ฒฝ ๋งŒ๋“ค๊ธฐ
tmp[builtwall[0].first][builtwall[0].second] = 1;
tmp[builtwall[1].first][builtwall[1].second] = 1;
tmp[builtwall[2].first][builtwall[2].second] = 1;

// ans = max(ans, bfs(tmp));
int tm =bfs(tmp);
if(ans < tm) {

ans = tm;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
ansarr[i][j] = tmp[i][j];
}
}
}
return;
}

if(idx >= n*m)return;

if(arr[x][y] == 0) {
builtwall.push_back({x, y});
buildwall(idx+1, depth+1);
builtwall.pop_back();
}
//// ์•ˆ ์„ธ์šฐ๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ
buildwall(idx+1, depth);


}


int main() {

input();
buildwall(0, 0);
cout << ans << '\n';
// for(int i=0;i<n;i++) {
// for(int j=0;j<m;j++) {
// cout << ansarr[i][j] << ' ';
// }
// cout << '\n';
// }

return 0;
}
74 changes: 74 additions & 0 deletions s13121312/๊ตฌํ˜„/2206.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include<bits/stdc++.h>
using namespace std;
char arr[1005][1005];
int n, m;
int dist[1005][1005][2];
//[0] ๋ฒฝ์„ ํ•˜๋‚˜๋„ ์•ˆ ๋ถ€์ˆ˜๊ณ  (x,y)๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋น„์šฉ
//[1] ๋ฒฝ์„ ํ•˜๋‚˜๋งŒ ๋ถ€์ˆ˜๊ณ  (x,y)๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋น„์šฉ, (x,y)๊ฐ€ ๋ฒฝ์ด๋ผ์„œ ๋ถ€์ˆ˜๋Š” ๊ฒฝ์šฐ ํฌํ•จ
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

void input() {
cin >> n >> m;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
cin >> arr[i][j];
}
}
}

int bfs() {

for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
dist[i][j][0] = dist[i][j][1] = -1;
}
}
//(0,0) -> (n-1,m-1)
queue<tuple<int, int, int>> q;
q.push({0, 0, 0});
dist[0][0][0] = dist[0][0][1] = 1;

while(!q.empty()) {
int x, y, broken;
tie(x,y,broken) = q.front();

if(x == n-1 && y ==m-1) {
return dist[x][y][broken];
}
q.pop();
int nxtdist = dist[x][y][broken] + 1;
for(int i=0;i<4;i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx<0 || nx>=n ||ny<0 || ny>=m)continue;
if(arr[nx][ny] == '0' && dist[nx][ny][broken] == -1) { //์˜จ์  ์—†๊ณ  ๋ฒฝ์ด์•„๋‹ˆ๋ฉด
dist[nx][ny][broken] = nxtdist;
q.push({nx,ny,broken});
}
if(!broken && arr[nx][ny] == '1' && dist[nx][ny][broken] == -1) { // broken์ด 0(๋ฒฝ์„ ํ•œ ๋ฒˆ๋„ ํ•œ ๋ถ€์ˆจ) ๋‹ค์Œ์— ๊ฐˆ๊ฒŒ ๋ฒฝ์ด๊ณ  ๊ฐ„ ์  ์—†์œผ๋ฉด
dist[nx][ny][1] = nxtdist;
q.push({nx, ny, 1});
}

/*์•„๋ž˜์™€ ๊ฐ™์ดํ•˜๋ฉด broken์„ ๋”ฐ๋กœ ์ฒดํฌ์•ˆํ•ด์ฃผ๋‹ˆ๊นŒ ์—ฌ๋Ÿฌ๋ฒˆ ๋ถ€์ˆ  ์ˆ˜ ์žˆ์Œ */
// if(arr[nx][ny] == '1' && dist[nx][ny][broken] == -1) { //๋‹ค์Œ์— ๊ฐˆ๊ฒŒ ๋ฒฝ์ด๊ณ  ๊ฐ„ ์ ์—†์œผ๋ฉด
// dist[nx][ny][1] = nxtdist;
// q.push({nx, ny, 1});
// }
}
}
return -1; //๋‹ต์„ ๋ชป์ฐพ์œผ๋ฉด -1 ๋ฐ˜ํ™˜

}

int main() {

input();

cout << bfs();



return 0;
}