Computer Science & Engineering/알고리즘
거리두기 확인하기
YeJinii
2021. 9. 10. 22:23
https://programmers.co.kr/learn/courses/30/lessons/81302#fn1
코딩테스트 연습 - 거리두기 확인하기
[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]
programmers.co.kr
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int dx[12]={-2,-1,-1,-1,0,0,0,0,1,1,1,2};
int dy[12]={0,-1,0,1,-2,-1,1,2,-1,0,1,0};
vector<int> solution(vector<vector<string>> places) {
vector<int> answer;
bool check= false;
for(int t=0; t<places.size(); t++){
check = false;
for(int x=0; x<5; x++){
for(int y=0; y<5; y++){
if(places[t][x][y]=='P'){//사람이 앉아있으면
for(int i=0; i<12; i++){
int nx = x+dx[i];
int ny = y+dy[i];
if(0<=nx&&0<=ny&&nx<5&&ny<5){ //범위 안에 있는데...
if(places[t][nx][ny]=='P'){//사람이 있는데
if(abs(dx[i])+abs(dy[i])==1){//맨하튼 거리가 1이면..
check= true; break;
}
else{//맨하튼 거리가 2이면
if(abs(dx[i])==1&&abs(dy[i])==1){//대각선상
if(places[t][nx][y]=='X'&&places[t][x][ny]=='X'){
continue;
}else{
check = true; break;
}
}
else{//두칸 위아래옆
if(places[t][x+dx[i]/2][y+dy[i]/2]!='X'){
check = true; break;
}
}
}
}
}
}
}
if(check) break;
}
if(check) break;
}
if(check) answer.push_back(0);
else answer.push_back(1);
}
return answer;
}