FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/4/4.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
831
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
4
/
4.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
78 lines (67 loc) · 2.35 KB
Breadcrumbs
python-for-coding-test
/
4
/
4.java
Copy path
File metadata and controls
78 lines (67 loc) · 2.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import
java
.
util
.*;
public
class
Main
{
public
static
int
n
,
m
,
x
,
y
,
direction
;
// 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
public
static
int
[][]
d
=
new
int
[
50
][
50
];
// 전체 맵 정보
public
static
int
[][]
arr
=
new
int
[
50
][
50
];
// 북, 동, 남, 서 방향 정의
public
static
int
dx
[] = {-
1
,
0
,
1
,
0
};
public
static
int
dy
[] = {
0
,
1
,
0
, -
1
};
// 왼쪽으로 회전
public
static
void
turn_left
() {
direction
-=
1
;
if
(
direction
== -
1
)
direction
=
3
;
}
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
// N, M을 공백을 기준으로 구분하여 입력받기
n
=
sc
.
nextInt
();
m
=
sc
.
nextInt
();
// 현재 캐릭터의 X 좌표, Y 좌표, 방향을 입력받기
x
=
sc
.
nextInt
();
y
=
sc
.
nextInt
();
direction
=
sc
.
nextInt
();
d
[
x
][
y
] =
1
;
// 현재 좌표 방문 처리
// 전체 맵 정보를 입력 받기
for
(
int
i
=
0
;
i
<
n
;
i
++) {
for
(
int
j
=
0
;
j
<
m
;
j
++) {
arr
[
i
][
j
] =
sc
.
nextInt
();
}
}
// 시뮬레이션 시작
int
cnt
=
1
;
int
turn_time
=
0
;
while
(
true
) {
// 왼쪽으로 회전
turn_left
();
int
nx
=
x
+
dx
[
direction
];
int
ny
=
y
+
dy
[
direction
];
// 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
if
(
d
[
nx
][
ny
] ==
0
&&
arr
[
nx
][
ny
] ==
0
) {
d
[
nx
][
ny
] =
1
;
x
=
nx
;
y
=
ny
;
cnt
+=
1
;
turn_time
=
0
;
continue
;
}
// 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
else
turn_time
+=
1
;
// 네 방향 모두 갈 수 없는 경우
if
(
turn_time
==
4
) {
nx
=
x
-
dx
[
direction
];
ny
=
y
-
dy
[
direction
];
// 뒤로 갈 수 있다면 이동하기
if
(
arr
[
nx
][
ny
] ==
0
) {
x
=
nx
;
y
=
ny
;
}
// 뒤가 바다로 막혀있는 경우
else
break
;
turn_time
=
0
;
}
}
System
.
out
.
println
(
cnt
);
}
}
Back
|
FazBrowse Home
|
New Git URL