728x90
반응형
https://www.acmicpc.net/problem/2174
2174번: 로봇 시뮬레이션
첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순
www.acmicpc.net
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Main {
public static class Node{
int x;
int y;
int dir;
public Node(int x,int y,int dir){
this.x=x;
this.y=y;
this.dir=dir;
}
}
public static int A;
public static int B;
public static int N;
public static int M;
// 북으로 가면 Y축 증가 남으로 가면 Y축 감소
public static int dX[]={0,1,0,-1};
public static int dY[]={1,0,-1,0};
public static Node []robot;
public static int check[][];
public static boolean isTrue=false;//충돌 판단
public static void DFS(int idx,String order,int repeat) {
for (int i = 0; i < repeat; i++) {
//System.out.println(robot[idx].x+" "+robot[idx].y);
//F일때만 전진 가능
if (order.equals("F")) {
//전진
int nX = robot[idx].x + dX[robot[idx].dir];
int nY = robot[idx].y + dY[robot[idx].dir];
// 충돌여부 판단
if (nX >= 1 && nX <= A && nY >= 1 && nY <= B) {
if (check[nX][nY] != 0) {
int crashIdx = check[nX][nY];
isTrue = true;
System.out.println("Robot " + idx + " crashes into robot " + crashIdx);
return;
} else {
check[robot[idx].x][robot[idx].y] = 0;
check[nX][nY] = idx;
robot[idx] = new Node(nX, nY, robot[idx].dir);
}
} else {
isTrue = true;
System.out.println("Robot " + idx + " crashes into the wall");
return;
}
//회전만
} else if (order.equals("R")) {
int nDir = (robot[idx].dir + 1) % 4;
//닝겐..nX,nY를 하는게 전진이잖어..
//int nX = robot[idx].x + dX[nDir];
// int nY = robot[idx].y + dY[nDir];
robot[idx]=new Node(robot[idx].x,robot[idx].y,nDir);
} else if (order.equals("L")) {
int nDir = (robot[idx].dir + 3) % 4;
/*
int nX = robot[idx].x + dX[nDir];
int nY = robot[idx].y + dY[nDir];
*/
robot[idx]=new Node(robot[idx].x,robot[idx].y,nDir);
}
}
}
public static void main(String args[]) throws NumberFormatException, IOException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st=new StringTokenizer(br.readLine());
A=Integer.parseInt(st.nextToken());
B=Integer.parseInt(st.nextToken());
check=new int[A+1][B+1];
st=new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
M=Integer.parseInt(st.nextToken());
robot=new Node[N+1];
// N이면 0 E이면 1 W이면 3 S이면 2
for(int i=1;i<=N;i++){
st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
String dir= st.nextToken();
int direction=0;
if(dir.equals("N")){
direction=0;
}else if(dir.equals("E")){
direction=1;
}else if(dir.equals("S")){
direction=2;
}else if(dir.equals("W")){
direction=3;
}
robot[i]=new Node(x,y,direction);
check[x][y]=i;
}
for(int i=0;i<M;i++){
st=new StringTokenizer(br.readLine());
int idx=Integer.parseInt(st.nextToken());
String order= st.nextToken();
int repeat=Integer.parseInt(st.nextToken());
DFS(idx,order,repeat);
if(isTrue){
break;
}
}
if(!isTrue){
System.out.println("OK");
}
}
}
|
cs |
14503번에 로봇청소기라는 문제가 있는데 그 문제와 매우 유사하다. 근데 주의해야할 점이 좌표이다!
계속 이 상태로 생각하고 풀어서
북 동 남 서로 움직인다고 했을 때
dx[]={-1,0,1,0}
dy[]={0,1,0,-1}
이라고 생각했다.
하지만!
실제로는 이랬다.
그러다 보니
북동남서 일때
dx[]={0,1,0,-1}
dy[]={1,0,-1,0}
이였다!
728x90
반응형
'알고리즘 > 시뮬레이션' 카테고리의 다른 글
[java] 이코테_구현) 문자열 재정렬 (1) | 2023.01.30 |
---|
댓글