๋ฌธ์ ๋งํฌ
BFS ๋ฌธ์ ์ด๋ค. y,x๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํด์ 2์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ค.
#include<bits/stdc++.h>
using namespace std;
const int max_n = 104;
int dy[4]={-1, 0, 1, 0};
int dx[4]={0, 1, 0, -1};
int n,m,a[max_n][max_n],visited[max_n][max_n],y,x;
int main()
{
scanf("%d %d", &n, &m);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%1d", &a[i][j]);
}
}
queue<pair<int,int>> q;
visited[0][0]=1;
q.push({0,0});
while(q.size())
{
tie(y,x)=q.front(); q.pop();
for(int i=0; i<4; i++)
{
int ny = y + dy[i];
int nx = x + dx[i];
if(ny<0 || ny>=n || nx<0 || nx>=m || a[ny][nx]==0) continue;
if(visited[ny][nx]) continue;
visited[ny][nx] = visited[y][x] + 1;
q.push({ny, nx});
}
}
printf("%d", visited[n-1][m-1]);
return 0;
}
๋ฌธ์ ๋งํฌ
connected component ๊ฐ์๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
DFS, BFS ๋ชจ๋ ๊ตฌํ ๊ฐ๋ฅํ๋ฐ DFS๊ฐ ์กฐ๊ธ ๋ ์ฝ๋ ๊ธธ์ด๋ ์งง๋ค
#include<iostream>
using namespace std;
int dy[4]={-1,0,1,0};
int dx[4]={0,1,0,-1};
int m,n,k,y,x,ret,ny,nx,t;
int a[51][51];
bool visited[51][51];
void dfs(int y, int x)
{
visited[y][x]=1; // ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for(int i=0; i<4; i++)
{
ny = y + dy[i];
nx = x + dx[i];
if(ny<0 || nx<0 || ny>=n || nx>=m) continue;
if(a[ny][nx] == 1 && !visited[ny][nx])
{
dfs(ny, nx);
}
}
return;
}
int main()
{
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while(t--)
{
// ๋ค์ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์ํด ์ด๊ธฐํํด์ค๋ค
fill(&a[0][0], &a[0][0] + 51*51, 0);
fill(&visited[0][0], &visited[0][0] + 51*51, 0);
ret = 0;
cin >> m >> n >> k;
for(int i=0; i<k; i++)
{
cin >> x >> y;
a[y][x]=1;
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[i][j]==1 && !visited[i][j])
{
dfs(i,j);
ret++; // ํ๋์ component์ ๋ํ dfs๊ฐ ๋๋๋ฉด ret++
}
}
}
cout << ret << '\n';
}
return 0;
}
๋ฌธ์ ๋งํฌ
์์ ์๊ด์์ด 3๊ฐ์ ๋ฒฝ์ ์ธ์ด๋ค : combination
#include<bits/stdc++.h>
using namespace std;
int a[10][10], visited[10][10], n, m, ret;
vector<pair<int, int>> virusList, wallList; // ์ขํ๊ฐ ๊ธฐ๋ฐ
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
void dfs(int y, int x){
for(int i = 0; i < 4; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny < 0 || ny >= n || nx < 0 || nx >= m || visited[ny][nx] || a[ny][nx] == 1) continue;
visited[ny][nx] = 1;
dfs(ny, nx);
}
return;
}
// ๋ฒฝ์ ์ธ์ฐ๋ ์ฌ๋ฌ ๊ฒฝ์ฐ์ ์๋ง๋ค ๋ฐ์ด๋ฌ์ค๋ฅผ ํผ๋จ๋ฆฌ๋๋ฐ ๋งค ๊ฒฝ์ฐ๋ง๋ค ์ด๊ธฐํํด์ค์ผ ํ๋ค
int solve(){
fill(&visited[0][0], &visited[0][0] + 10 * 10, 0); // ์ด๊ธฐํ
for(pair<int, int> b : virusList){
visited[b.first][b.second] = 1;
dfs(b.first, b.second);
}
int cnt = 0; // ์์ ์์ญ ์นด์ดํธ
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j] == 0 && !visited[i][j]) cnt++;
}
}
return cnt;
}
int main(){
cin >> n >> m;
for(int i = 0; i <n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
if(a[i][j] == 2) virusList.push_back({i, j});
if(a[i][j] == 0) wallList.push_back({i, j});
}
}
// wallList.size() C 3 (combination)
for(int i = 0; i < wallList.size(); i++){
for(int j = 0; j < i; j++){
for(int k = 0; k < j; k++){
a[wallList[i].first][wallList[i].second] = 1;
a[wallList[j].first][wallList[j].second] = 1;
a[wallList[k].first][wallList[k].second] = 1;
ret = max(ret, solve());
// ์์๋ณต๊ตฌ
a[wallList[i].first][wallList[i].second] = 0;
a[wallList[j].first][wallList[j].second] = 0;
a[wallList[k].first][wallList[k].second] = 0;
}
}
}
cout << ret << "\n";
return 0;
}
๋ฌธ์ ๋งํฌ
dfs, bfs ๋ก ํด๋ ๋๋ค. ํ์๋ง ํ์ํ๋ฏ๋ก. ๊ทผ๋ฐ ๊ฐ์ค์น๊ฐ ๊ฐ๊ณ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค๋ฉด bfs๋ก ํด์ผํ๋ค~
#include <bits/stdc++.h>
using namespace std;
int a[104][104], visited[104][104];
int dy[]={-1,0,1,0}, dx[] = {0,1,0,-1};
int n,m,cnt,cnt2;
vector <pair<int,int>>v;
void go(int y,int x){
visited[y][x] = 1;
if(a[y][x]==1){ //์ด๋ฏธ ์น์ฆ๊ฐ ์๋ค๋ฉด
v.push_back({y,x});
return; // ์ข
๋ฃ
}
for(int i=0; i<4; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny<0 || ny>=n || nx<0 || nx>=m || visited[ny][nx])
continue;
go(ny,nx);
}
return;
}
int main(){
cin >> n >> m;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>a[i][j];
}
}
while(true){
cnt2 =0; // ๋ชจ๋ ๋
น๊ธฐ ํ ์๊ฐ ์ ์ ๋จ์์๋ ์น์ฆ์กฐ๊ฐ์ด ๋์ฌ ์๋ ์นธ์ ๊ฐ์
fill(&visited[0][0],&visited[0][0] + 104 * 104,0);
v.clear();
go(0,0);
for(pair<int, int> b : v){
cnt2++;
a[b.first][b.second] = 0;
}
bool flag = 0; // ์น์ฆ๊ฐ ๋ค ๋
น์๋ฒ๋ฆฐ ์ํ
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j] != 0) flag = 1; // ์น์ฆ๊ฐ ํ๋๋ผ๋ ์๋ค๋ฉด
}
}
cnt++; // ์น์ฆ๊ฐ ๋ชจ๋ ๋
น์์ ์์ด์ง๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ
if(!flag) break;
}
cout<< cnt << "\n" << cnt2 << '\n';
}
๋ณํ์ด๊ฐ ๋ถ๋ณด๋ค ๋นจ๋ฆฌ ๋ฐ์ณ๋์ฌ ๋์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
๊ฐ์ค์น๊ฐ ๊ฐ์ ๊ทธ๋ํ์์์ ์ต๋จ๊ฑฐ๋ฆฌ ๐ BFS
๋ถ์ ์ต๋จ๊ฑฐ๋ฆฌ์ ์งํ์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๋น๊ตํด์, (๋ถ๋ณด๋ค) ์งํ์ด๊ฐ ๋ ๋น ๋ฅด๋ค๋ฉด ์ ์งํ๋๋ก ํ๋ค.
๋ํ ๋ถ์ ์ฌ๋ฌ๊ฐ์ผ ์ ์๊ณ , ์งํ์ ํ ๋ช ์ผ๋ก ๊ณ ์ ๋์ด์๋ค.
๋ถ์ด ์์ ์๋ค๋ฉด fire_check
๋ผ๋ 2์ฐจ์ ๋ฐฐ์ด์ 0์๋ก ์ ์๋๋ค.
๊ทธ๋์ ์ด๊ธฐ๊ฐ์ผ๋ก ํฐ ๊ฐ(์ฌ๊ธฐ์ INF
)์ผ๋ก ์ด๊ธฐํ๋ฅผ ํด์ค์ผ ํ๋ค.
#include<iostream>
#include<queue>
#include<algorithm>
#include<tuple>
using namespace std;
const int INF = 987654321;
char a[1004][1004]; // ์
๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ ๋ฏธ๋ก๋งต
int fire_check[1004][1004], person_check[1004][1004];
int dy[4] = { 1,0,-1,0 };
int dx[4] = { 0,1,0,-1 };
int n, m, sx, sy, ret, x, y;
// ์ค๋ฒํ๋ก์ฐ ์ฒดํฌ
bool in(int a, int b) {
return 0 <= a && a < n && 0 <= b && b < m;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
queue<pair<int, int>> q;
cin >> n >> m;
fill(&fire_check[0][0], &fire_check[0][0] + 1004 * 1004, INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] = 'F') {
fire_check[i][j] = 1;
q.push({ i,j });
}
else if (a[i][j] == 'J') {
sy = i;
sx = j;
}
}
}
// ๋ถ์ ๊ฐ ์ง์ ์์ ๋ค ๋ฐฉํฅ์ผ๋ก ํ์ฐ๋๋ค
while (q.size()) {
tie(y, x) = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (!in(ny, nx)) continue;
if (fire_check[ny][nx] != INF || a[ny][nx] == '#') continue;
fire_check[ny][nx] = fire_check[y][x] + 1;
q.push({ ny,nx });
}
}
person_check[sy][sx] = 1;
q.push({ sy,sx });
while (q.size()){
int y = q.front().first;
int x = q.front().second;
q.pop();
// ์งํ์ด๋ ๋ฏธ๋ก์ ๊ฐ์ฅ์๋ฆฌ์ ์ ํ ๊ณต๊ฐ์์ ํ์ถํ ์ ์๋ค
if (x == m - 1 || y == n - 1 || x == 0 || y == 0) {
ret = person_check[y][x];
break;
}
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (!in(ny, nx)) continue;
if (person_check[ny][nx] || a[ny][nx] == '#') continue;
// ๋ถ์ด ์งํ์ด๋ณด๋ค ๋น ๋ฅด๊ฑฐ๋ ๊ฐ๋ค๋ฉด ์งํ์ด๋ ๊ฐ ์ ์๋ค
if (fire_check[ny][nx] <= person_check[y][x] + 1) continue;
// ์งํ์ด๊ฐ ๊ฐ ์ ์๋ค๋ฉด
person_check[ny][nx] = person_check[y][x] + 1;
q.push({ ny,nx });
}
}
if (ret != 0) cout << ret << '\n';
else cout << "IMPOSSIBLE\n";
return 0;
}