package com.company;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
final int DIRECTION_FROM_UP = 0;
final int DIRECTION_FROM_RIGHT = 1;
final int DIRECTION_FROM_DOWN = 2;
final int DIRECTION_FROM_LEFT = 3;
char[][] pathGrid;
int rowsLength ;
int columnsLength;
int directionsNum = 4;
public int[] solution(String[] grid) {
List<Integer> answerList = new ArrayList<Integer>();
rowsLength = grid.length;
columnsLength = grid[0].length();
boolean[][][] pathCheckGrid = new boolean[directionsNum][rowsLength][columnsLength];
pathGrid = new char[rowsLength][columnsLength];
for (int i = 0; i < rowsLength; i++) {
for (int j = 0; j < columnsLength; j++) {
pathGrid[i][j] = grid[i].charAt(j);
}
}
startLight(pathCheckGrid , answerList);
Collections.sort(answerList);
return answerList.stream().mapToInt(i -> i).toArray();
}
private void startLight(boolean[][][] pathCheckGrid, List<Integer> answerList) {
for (int i = 0; i < rowsLength; i++) {
for (int j = 0; j < columnsLength; j++) {
for (int k = 0; k < directionsNum; k++) {
int pathLength = checkPath(k, i, j, pathCheckGrid);
if(pathLength != 0)
answerList.add(pathLength);
}
}
}
}
private int checkPath(int direction, int row, int column, boolean[][][] pathCheckGrid) {
int count = 0;
boolean MOVE_UP = true;
boolean MOVE_DOWN = false;
boolean MATRIX_ROW = true;
boolean MATRIX_COLUMN = false;
if(pathCheckGrid[direction][row][column])
return 0;
pathCheckGrid[direction][row][column] = true;
count++;
while(true){
char pathGridChar = pathGrid[row][column];
if(direction == DIRECTION_FROM_UP){
if (pathGridChar == 'S') {
row = changeMatrixPosition(row, MOVE_UP, MATRIX_ROW);
} else if (pathGridChar == 'L') {
column = changeMatrixPosition(column, MOVE_UP, MATRIX_COLUMN);
direction = DIRECTION_FROM_LEFT;
} else if (pathGridChar == 'R') {
column = changeMatrixPosition(column, MOVE_DOWN, MATRIX_COLUMN);
direction = DIRECTION_FROM_RIGHT;
}
}
else if(direction == DIRECTION_FROM_RIGHT){
if (pathGridChar == 'S') {
column = changeMatrixPosition(column, MOVE_DOWN, MATRIX_COLUMN);
} else if (pathGridChar == 'L') {
row = changeMatrixPosition(row, MOVE_UP, MATRIX_ROW);
direction = DIRECTION_FROM_UP;
} else if (pathGridChar == 'R') {
row = changeMatrixPosition(row, MOVE_DOWN, MATRIX_ROW);
direction = DIRECTION_FROM_DOWN;
}
}
else if(direction == DIRECTION_FROM_DOWN){
if (pathGridChar == 'S') {
row = changeMatrixPosition(row, MOVE_DOWN, MATRIX_ROW);
} else if (pathGridChar == 'L') {
column = changeMatrixPosition(column, MOVE_DOWN, MATRIX_COLUMN);
direction = DIRECTION_FROM_RIGHT;
} else if (pathGridChar == 'R') {
column = changeMatrixPosition(column, MOVE_UP, MATRIX_COLUMN);
direction = DIRECTION_FROM_LEFT;
}
}
else if(direction == DIRECTION_FROM_LEFT){
if (pathGridChar == 'S') {
column = changeMatrixPosition(column, MOVE_UP, MATRIX_COLUMN);
} else if (pathGridChar == 'L') {
row = changeMatrixPosition(row, MOVE_DOWN, MATRIX_ROW);
direction = DIRECTION_FROM_DOWN;
} else if (pathGridChar == 'R') {
row = changeMatrixPosition(row, MOVE_UP, MATRIX_ROW);
direction = DIRECTION_FROM_UP;
}
}
if(!pathCheckGrid[direction][row][column]){
pathCheckGrid[direction][row][column] = true;
count ++;
}
else
break;
}
return count;
}
private int changeMatrixPosition(int positionIndex, boolean upOrDown, boolean rowOrColumn) {
int rowStartIndex = 0;
int rowEndIndex = rowsLength - 1;
int columnStartIndex = 0;
int columnEndIndex = columnsLength - 1;
if(upOrDown) {
if (rowOrColumn) {
if(positionIndex < rowEndIndex)
positionIndex += 1;
else
positionIndex = rowStartIndex;
}
else{
if(positionIndex < columnEndIndex)
positionIndex += 1;
else
positionIndex = columnStartIndex;
}
}
else {
if (rowOrColumn) {
if(positionIndex > rowStartIndex)
positionIndex -= 1;
else
positionIndex = rowEndIndex;
}
else{
if(positionIndex > columnStartIndex)
positionIndex -= 1;
else
positionIndex = columnEndIndex;
}
}
return positionIndex;
}
}
좋은 코드가 있어서 가져왔다. [방향 전환 , 좌표 이동 부분을 잘 살펴볼 것]
import java.util.*;
class Solution {
static int[] di = {-1,0,1,0};
static int[] dj = {0,1,0,-1};
static boolean[][][] visit;
static int n, m;
public int[] solution(String[] grid) {
ArrayList<Integer> result = new ArrayList<>();
n = grid.length;
m = grid[0].length();
visit = new boolean[n][m][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
if(!visit[i][j][k]) {
result.add(move(grid, i, j, k));
}
}
}
}
Collections.sort(result);
int[] answer = new int[result.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = result.get(i);
}
return answer;
}
static int move(String[] grid, int x, int y, int d) {
int count = 0;
while(true) {
if(visit[x][y][d]) break;
count++;
visit[x][y][d] = true;
if(grid[x].charAt(y) == 'L') {
d = d == 0 ? 3 : d-1;
} else if(grid[x].charAt(y) == 'R') {
d = d == 3 ? 0 : d+1;
}
x = (x + di[d] + n) % n;
y = (y + dj[d] + m) % m;
}
return count;
}
}