const checkOk=(placeArray)=>{
const dx=[-1, 0, 1, 0];
const dy=[0, -1, 0, 1];
placeArray = placeArray.map((el) => el.split(''));
let queue=[];
// P 자리 먼저 찾기
for (let i=0;i<5;i++){
for (let j=0;j<5;j++){
if (placeArray[i][j]==='P'){
queue.push([i,j]);
}
}
}
// 칸막이인지
// 하나라도 맨해튼 거리 어기면 바로 while 탈출
let willReturn=1;
while (queue.length){
let [currentX, currentY]=queue.shift();
for (let i=0;i<4;i++){
let nextX=currentX+dx[i];
let nextY=currentY+dy[i];
if (nextX>=0&&nextX<5&&nextY>=0&&nextY<5){
// 칸막이면 넘기기
if (placeArray[nextX][nextY]==='X'){
continue;
}
// 자리면 바로 탈출
if (placeArray[nextX][nextY]==='P'){
willReturn=0;
break;
}
// 맨해튼거리 고려 시작
for (let j=0;j<4;j++){
let nextX2=nextX+dx[j];
let nextY2=nextY+dy[j];
let manDistance=Math.abs(nextX2-currentX)+Math.abs(nextY2-currentY)
console.log('manDistance', manDistance)
if (nextX2<0||nextY2<0||nextX2>=5||nextY2>=5){
continue;
}
if (nextX2===currentX&&nextY2===currentY){
continue;
}
if (manDistance<=2){
if (placeArray[nextX2][nextY2]==='P'){
willReturn=0;
break;
}
}
}
}else{
continue;
}
}
}
return willReturn;
}
function solution(places) {
let answer=[];
for (let i=0;i<5;i++){
answer.push(checkOk(places[i]));
}
console.log('answer', answer)
return answer;
}