Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a53f1e1

Browse files
이지영: [PG] 방문 길이_240913
1 parent 9746f83 commit a53f1e1

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
static int N, x, y;
6+
static Pos[][] g;
7+
// U, R, D, L
8+
static int[] dx = {-1, 0, 1, 0};
9+
static int[] dy = {0, 1, 0, -1};
10+
static class Pos {
11+
int x, y;
12+
boolean[] visited;
13+
public Pos(int x, int y, boolean[] visited){
14+
this.x = x;
15+
this.y = y;
16+
this.visited = visited;
17+
}
18+
@Override
19+
public String toString(){
20+
return "("+this.x+","+this.y+")";
21+
}
22+
}
23+
24+
public int solution(String dirs) {
25+
int answer = 0;
26+
N = 11;
27+
g = new Pos[N][N];
28+
for(int i=0; i<N; i++){
29+
for(int j=0; j<N; j++){
30+
g[i][j] = new Pos(i, j, new boolean[4]);
31+
}
32+
}
33+
// for(int i=0; i<N; i++){
34+
// System.out.println(Arrays.toString(g[i]));
35+
// }
36+
37+
x = N/2;
38+
y = N/2;
39+
40+
for(int i=0; i<dirs.length(); i++){
41+
char dir = dirs.charAt(i);
42+
if(dir == 'U'){
43+
answer += move(0);
44+
} else if(dir == 'R'){
45+
answer += move(1);
46+
} else if(dir == 'D'){
47+
answer += move(2);
48+
} else if(dir == 'L'){
49+
answer += move(3);
50+
}
51+
}
52+
// for(int i=0; i<N; i++){
53+
// for(int j=0; j<N; j++){
54+
// System.out.print(g[i][j]+" ");
55+
// System.out.println(Arrays.toString(g[i][j].visited));
56+
// }
57+
// }
58+
59+
60+
return answer;
61+
}
62+
public static boolean inRange(int x, int y){
63+
return x>=0 && x<N && y>=0 && y<N;
64+
}
65+
public static int move(int d){
66+
int nx = x + dx[d];
67+
int ny = y + dy[d];
68+
// System.out.println("("+x+","+y+") -> "+nx+", "+ny);
69+
int cnt = 0;
70+
if(inRange(nx, ny)){
71+
int nd = (d+2) % 4;
72+
// (nx,ny)로 향하는 방향과 (x,y)에서 나가는 방향의 방문여부 확인
73+
if(!g[nx][ny].visited[nd] && !g[x][y].visited[d]){
74+
g[nx][ny].visited[nd] = true;
75+
g[x][y].visited[d] = true;
76+
cnt++;
77+
}
78+
x = nx;
79+
y = ny;
80+
}
81+
return cnt;
82+
}
83+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /