-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathStickerCollectorRobot_UVa11831.java
99 lines (86 loc) · 1.91 KB
/
StickerCollectorRobot_UVa11831.java
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
package v118;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class StickerCollectorRobot_UVa11831 {
static int R, C;
static char[][] arena;
static int counter;
static int x, y;
static int dir;
static int[] dx = new int[]{-1,0,1,0};
static int[] dy = new int[]{0,1,0,-1};
public static void rotate(int k)
{
dir = ((dir+k)%4+4)%4;
}
public static void move()
{
int i = x +dx[dir];
int j = y +dy[dir];
if(!valid(i,j))
return;
x = i;
y = j;
if(arena[x][y]=='*')
{
counter++;
arena[x][y] = '.';
}
}
public static boolean valid(int i, int j)
{
if(i==-1 || j==-1 || i==R || j==C || arena[i][j]=='#')
return false;
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while(true)
{
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
int S = Integer.parseInt(st.nextToken());
if(R==0)
break;
arena = new char[R][C];
for(int i = 0; i < R; i++)
{
String row = br.readLine();
for(int j = 0; j < C; j++)
if(row.charAt(j) == '#' ||row.charAt(j) == '*'||row.charAt(j)=='.')
arena[i][j] = row.charAt(j);
else
{
switch(row.charAt(j))
{
case 'N':dir=0;break;
case 'L':dir=1;break;
case 'S':dir=2;break;
default: dir = 3;
}
x = i;
y = j;
arena[i][j] = '.';
}
}
counter = 0;
String inst = br.readLine();
for(int i = 0; i < S; i++)
{
char c = inst.charAt(i);
switch(c)
{
case 'D': rotate(1);break;
case 'E': rotate(-1);break;
default: move();
}
}
sb.append(counter+"\n");
}
System.out.print(sb);
}
}