https://programmers.co.kr/learn/courses/30/lessons/81302?language=cpp
#include <string>
#include <vector>
#include <cstring>
using namespace std;
struct MOVE { int x, y; };
MOVE mv[4] = { { 1,0 },{ 0,1 },{ -1,0 },{ 0,-1 } };
bool flag = true;
void dfs(int visited[5][5], vector<string> tmp, int i, int j, int count) {
if (count== 2) {
return;
}
for (int k = 0; k<4; k++) {
if (flag == false) return;
int movei = i + mv[k].x;
int movej = j + mv[k].y;
if (movei >= 0 && movei <5 && movej >= 0 && movej <5 && visited[movei][movej] == -1) {
if (tmp[movei][movej] == 'P') {
flag = false;
return;
}
else if (tmp[movei][movej] == 'X') {
continue;
}
else {
visited[movei][movej] = 1;
dfs(visited,tmp, movei, movej, count + 1);
}
}
}
}
vector<int> solution(vector<vector<string>> places) {
vector<int> answer;
for (int k = 0; k<places.size(); k++) {
vector<string> tmp = places[k];
flag = true;
for (int i = 0; i<5; i++) {
for (int j = 0; j<5; j++) {
if (tmp[i][j] == 'P') {
int visited[5][5];
memset(visited, -1, sizeof(visited));
visited[i][j] = 1;
dfs(visited,tmp, i, j, 0);
if (flag == false) {
answer.push_back(0);
break;
}
}
}
if (flag == false) break;
}
if (flag == true) {
answer.push_back(1);
}
}
return answer;
}
'프로그래머스' 카테고리의 다른 글
네트워크 C++ (0) | 2022.01.23 |
---|---|
N으로 표현 C++ (0) | 2022.01.23 |
징검다리 건너기 C++ (0) | 2021.10.06 |
도둑질 C++ (0) | 2021.10.06 |
셔틀버스 C++ (0) | 2021.10.03 |