BFS 방식으로 시작 위치에서 상하좌우로 퍼저나가며 제일 먼저 라벨을 찾는 Queue가 있다면
STOP 하는 방식으로 작성
동일하게 라벨에서 도착지점도 동일한 코드로 진행
제한 사항 중 라벨 또는 출구 지점을 찾지 못할 경우가 있는데
단순 사방으로 벽이 막혔다고 생각했다가 벽이 가로 또는 세로로 이어져 공간이 분리된 경우가 있을 수 있다는 점.
int mapRowLength = 0;
int mapColLength = 0;
String[] map = null;
int moveCount = 0;
Queue<Map> queue = null;
boolean[][] markingLocation = null;
public int solution(String[] maps) {
this.mapRowLength = maps.length;
this.mapColLength = maps[0].length();
this.map = maps;
int[] startLocation = new int[2];
int[] leverLocation = new int[2];
for(int i = 0; i < mapRowLength; i++){
if(this.map[i].indexOf('S') >= 0) {
startLocation[0] = i;
startLocation[1] = this.map[i].indexOf('S');
}
if(this.map[i].indexOf('L') >= 0) {
leverLocation[0] = i;
leverLocation[1] = this.map[i].indexOf('L');
}
}
int answer = -1;
findLocation('L', startLocation);
if(this.moveCount > 0){
answer = this.moveCount;
findLocation('E', leverLocation);
if(this.moveCount > 0) answer += this.moveCount;
else answer = -1;
}
System.out.println("answer = " + answer);
return answer;
}
void findLocation(char setPlaceName, int[] setCrrentLocation){
this.markingLocation = new boolean[mapRowLength][mapColLength];
this.markingLocation[setCrrentLocation[0]][setCrrentLocation[1]] = true;
this.queue = new LinkedList<>();
this.moveCount = 0;
addQueue(0, setPlaceName, setCrrentLocation);
while (this.moveCount == 0) {
if(queue.size() == 0) break;
Map poll = queue.poll();
int[] currentLocation = (int[]) poll.get("currentLocation");
char placeName = (char) poll.get("placeName");
int moveCount = (int) poll.get("moveCount");
moveLeft(moveCount, placeName, cloneArray(currentLocation));
moveRight(moveCount, placeName, cloneArray(currentLocation));
moveUp(moveCount, placeName, cloneArray(currentLocation));
moveBottom(moveCount, placeName, cloneArray(currentLocation));
}
}
void moveLeft(int moveCount, char placeName, int[] currentLocation){
int currentCol = currentLocation[1];
if(currentCol == 0) return;
else {
currentCol--;
currentLocation[1] = currentCol;
}
commomProcess(moveCount, placeName, currentLocation);
}
void moveRight(int moveCount, char placeName, int[] currentLocation){
int currentCol = currentLocation[1];
if(currentCol == mapColLength-1) return;
else {
currentCol++;
currentLocation[1] = currentCol;
}
commomProcess(moveCount, placeName, currentLocation);
}
void moveUp(int moveCount, char placeName, int[] currentLocation){
int currentRow = currentLocation[0];
if(currentRow == 0) return;
else {
currentRow--;
currentLocation[0] = currentRow;
}
commomProcess(moveCount, placeName, currentLocation);
}
void moveBottom(int moveCount, char placeName, int[] currentLocation){
int currentRow = currentLocation[0];
if(currentRow == mapRowLength-1) return;
else {
currentRow++;
currentLocation[0] = currentRow;
}
commomProcess(moveCount, placeName, currentLocation);
}
void commomProcess(int moveCount, char placeName, int[] currentLocation){
int currentRow = currentLocation[0];
int currentCol = currentLocation[1];
if(this.markingLocation[currentRow][currentCol] == true) return;
if(this.map[currentRow].charAt(currentCol) == 'X') return;
markingLocation[currentRow][currentCol] = true;
moveCount++;
if(this.map[currentRow].charAt(currentCol) == placeName){
if(this.moveCount == 0) this.moveCount = moveCount;
}else{
addQueue(moveCount, placeName, currentLocation);
}
}
void addQueue(int moveCount, char placeName, int[] currentLocation){
Map job = new HashMap();
job.put("currentLocation", currentLocation);
job.put("placeName", placeName);
job.put("moveCount", moveCount);
this.queue.add(job);
}
int[] cloneArray(int[] array){
int[] cloneArray = new int[array.length];
System.arraycopy(array, 0, cloneArray, 0, 2);
return cloneArray;
}