https://www.acmicpc.net/problem/3055
3055번: 탈출
사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제
www.acmicpc.net
음 조금 생각해야하는 bfs 문제 조금 시간이 걸리네요 1시간 10분?
#include <iostream>
#include <vector>
#include <queue>
#include<string>
using namespace std;
struct Point {
int r, c;
bool bWater = false;
int cnt=0;
};
void Prints(vector<vector<char>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j] ;
}
cout << endl;
}
cout << endl;
}
int main() {
int r, c;
vector<vector<char>>maps;
cin >> r >> c;
vector<bool>tmpbVisted(c, false);
vector<vector<bool>>bVisted(r, tmpbVisted);
Point biber;
Point gosum;
queue<Point>q;
for (int i = 0; i < r; i++) {
string tmpStr;
cin >> tmpStr;
vector<char> tmpMap;
for (int j = 0; j < c; j++) {
char tmpChar=tmpStr[j];
if (tmpChar == 'X') {
bVisted[i][j] = true;
}
if (tmpChar == '*') {
Point tmpPoint;
tmpPoint.c = j;
tmpPoint.r = i;
tmpPoint.bWater = true;
bVisted[i][j] = true;
q.push(tmpPoint);
}
if (tmpChar=='D') {
biber.c = j;
biber.r = i;
}
if (tmpChar=='S') {
gosum.c = j;
gosum.r = i;
bVisted[i][j] = true;
gosum.bWater = false;
}
tmpMap.push_back(tmpChar);
}
maps.push_back(tmpMap);
}
q.push(gosum);
int dirc[] = { 0,0,1,-1 };
int dirr[] = { 1,-1,0,0 };
bool bEnd = false;
bool bNotgo = false;
while (!q.empty()) {
Point s = q.front();
q.pop();
if (bEnd == true) {
break;
}
for (int i = 0; i < 4; i++) {
int tmpdirc = s.c + dirc[i];
int tmpdirr = s.r + dirr[i];
if (tmpdirc==-1|| tmpdirr==-1|| tmpdirc>=c|| tmpdirr>=r) {
continue;
}
//홍수 퍼짐
if (s.bWater == true&& maps[tmpdirr][tmpdirc] != 'X'&& maps[tmpdirr][tmpdirc] != 'D'&&maps[tmpdirr][tmpdirc]!='*') {
maps[tmpdirr][tmpdirc] = '*';
bVisted[tmpdirr][tmpdirc] = true;
Point tmpPoint;
tmpPoint.c = tmpdirc;
tmpPoint.r = tmpdirr;
tmpPoint.bWater = true;
q.push(tmpPoint);
}
//고슴도치 이동
else if (s.bWater == false && bVisted[tmpdirr][tmpdirc] == false) {
bVisted[tmpdirr][tmpdirc] = true;
Point tmpPoint;
maps[tmpdirr][tmpdirc] = 'S';
tmpPoint.c = tmpdirc;
tmpPoint.r = tmpdirr;
tmpPoint.bWater = false;
tmpPoint.cnt = s.cnt + 1;
q.push(tmpPoint);
if (tmpdirc == biber.c&&tmpdirr == biber.r) {
cout<<tmpPoint.cnt;
i = 4;
bEnd = true;
continue;
}
}
}
}
if (bEnd==false) {
cout << "KAKTUS";
}
return 0;
}
'알고리즘 공부' 카테고리의 다른 글
백준 단지번호붙이기 (0) | 2023.07.07 |
---|---|
백준 벽 부수고 이동하기 (0) | 2023.07.07 |
백준 토마토 (0) | 2023.07.04 |
백준- 말이되고픈원숭이 (0) | 2023.06.28 |
백준 - 미로찾기 (0) | 2023.06.28 |