Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created May 20, 2020 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayaysir/38d8cbc4b4720b03651fce448a804218 to your computer and use it in GitHub Desktop.
Save ayaysir/38d8cbc4b4720b03651fce448a804218 to your computer and use it in GitHub Desktop.
public class FourDirection {
public static int[][] getDirection(int select) {
if(select == 3) {
// 방향 설정: 반시계 방향 (ENSW)
return new int[][]{
{0, -1, 0, 1},
{1, 0, -1, 0}
};
} else if(select == 2) {
// 방향 설정: 시계 방향 (ESWN)
return new int[][]{
{0, 1, 0, -1},
{1, 0, -1, 0}
};
} else {
// 방향 설정: 간편한 사용법 (NSEW)
return new int[][]{
{-1, 1, 0, 0},
{0, 0, 1, -1}
};
}
}
public static void main(String[] args) {
String[][] map = {
{".", ".", ".", ".", "."},
{".", ".", "N", ".", "."},
{".", "W", "옷", "E", "."},
{".", ".", "S", ".", "."},
{".", ".", ".", ".", "."}
};
int r = map.length, c = map[0].length;
// 배열 표시
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
System.out.println();
for(int d = 1; d <= 3; d++) {
int[][] dir = getDirection(d);
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
if(map[i][j].equals("옷")) {
for(int w = 0; w < 4; w++) {
// 4번 돌면서 ii, jj에 사방향을 지정
int ii = i + dir[0][w];
int jj = j + dir[1][w];
// 배열 인덱스가 마이너스가 되거나 행열을 초과하는 현상을 방지
if(ii < 0 || ii == r || jj < 0 || jj == c) {
continue;
}
System.out.print(map[ii][jj]);
}
}
}
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment