-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCC ‘18 S3
148 lines (138 loc) · 4.81 KB
/
CCC ‘18 S3
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
public static void RoboThieves() {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
Node[][] grid = new Node[N][M];
int sRow = 0;
int sCol = 0;
for(int i = 0; i < N; i++) {
String input= sc.next();
for(int s = 0; s < input.length(); s++) {
if(input.charAt(s) != 'W' && input.charAt(s) != 'C') {
grid[i][s] = new Node(input.charAt(s), true);
} else {
grid[i][s] = new Node(input.charAt(s), false);
}
if(input.charAt(s) == 'S') {
sRow = i;
sCol = s;
}
}
}
Set<Character> conveyors = new HashSet<>();
conveyors.add('U');
conveyors.add('D');
conveyors.add('L');
conveyors.add('R');
//for each camara node, check four directions around it, set all node statuus to false if a node can be seen by that camera
for(int r = 0; r < N; r++) {
for(int c = 0; c < M; c++) {
if(grid[r][c].val == 'C') {
int row = r;
int col = c;
while(col+1 < M-1) {
col++;
if(grid[row][col].val == 'W') {
break;
} else if(conveyors.contains(grid[row][col].val)) {
continue;
} else {
grid[row][col].walkable = false;
}
}
col = c;
while(col-1 > 0) {
col--;
if(grid[row][col].val == 'W') {
break;
} else if(conveyors.contains(grid[row][col].val)) {
continue;
} else {
grid[row][col].walkable = false;
}
}
col = c;
while(row+1 < N-1) {
row++;
if(grid[row][col].val == 'W') {
break;
} else if(conveyors.contains(grid[row][col].val)) {
continue;
} else {
grid[row][col].walkable = false;
}
}
row = r;
while(row-1 > 0) {
row--;
if(grid[row][col].val == 'W') {
break;
} else if(conveyors.contains(grid[row][col].val)) {
continue;
} else {
grid[row][col].walkable = false;
}
}
}
}
}
// bfs
walk(grid, sRow, sCol, 0);
for(int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (grid[i][j].val == '.') {
if (grid[i][j].dist == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(grid[i][j].dist);
}
}
}
}
}
public static void walk (Node[][] grid, int row, int col, int step) {
if(!grid[row][col].walkable || grid[row][col].dist <= step) {
return;
}
grid[row][col].dist = step;
// check conveyors
if(grid[row][col].val == 'U') {
walk(grid, row-1, col, step);
return;
}
if(grid[row][col].val == 'D') {
walk(grid, row+1, col, step);
return;
}
if(grid[row][col].val == 'L') {
walk(grid, row, col-1, step);
return;
}
if(grid[row][col].val == 'R') {
walk(grid, row, col+1, step);
return;
}
// bfs recursion
if(grid[row-1][col].walkable) {
walk(grid, row-1, col, step+1);
}
if(grid[row+1][col].walkable) {
walk(grid, row+1, col, step+1);
}
if(grid[row][col-1].walkable) {
walk(grid, row, col-1, step+1);
}
if(grid[row][col+1].walkable) {
walk(grid, row, col+1, step+1);
}
}
static class Node{
char val; //W - wall, S - start, C - camera, LRUD - conveyors, . - empty
boolean walkable;
int dist; //how many steps from start
public Node(char v, boolean w){
val = v;
walkable = w;
dist = Integer.MAX_VALUE;
}
}