-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhideNSeekNew.java
261 lines (243 loc) · 7.3 KB
/
hideNSeekNew.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Classz21;
import java.util.*;
/**
*
* @author moyangwang
*/
public class hideNSeekNew {
public static boolean [][] clone(boolean [][] arr){
boolean [][] result = new boolean[arr.length][arr[0].length];
for(int i = 0; i<arr.length; i++){
result[i]=arr[i].clone();
}
return result;
}
public static void print (int [][]arr){
for(int i = 0; i<arr.length;i++){
for(int u = 0; u<arr[0].length;u++){
System.out.print(arr[i][u]+" ");
}
System.out.println();
}
}
public static void print(int [] arr){
for(int i = 0; i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static int fact(int num){
if(num==1){
return 1;
}else{
return fact(num-1)*num;
}
}
public static ArrayList<Integer> notHere(int [] arr, int a){
ArrayList<Integer> list = new ArrayList();
for(int i = 0; i<=a; i++){
list.add(arr[i]);
}
ArrayList<Integer> res = new ArrayList();
for(int i = 1; i<=arr.length; i++){
if(!list.contains(i)){
res.add(i);
}
}
return res;
}
public static int nextPos(int [] arr, int a){
int cur = arr[a];
ArrayList<Integer> list = new ArrayList();
for(int i = 0; i<a; i++){
list.add(arr[i]);
}
for(int i = cur+1; i<=arr.length; i++){
if(!list.contains(i)){
return i;
}
}
return -1;
}
public static int [] ne(int [] arr, int a){
int [] ne = arr.clone();
ne[a] = nextPos(ne,a);
ArrayList<Integer> n = notHere(ne,a);
Collections.sort(n);
for(int i = a+1; i<arr.length; i++){
ne[i]=n.get(i-a-1);
}
return ne;
}
public static boolean order(int [] arr, int a){
ArrayList<Integer> list = new ArrayList();
for(int i = a; i<arr.length; i++){
list.add(arr[i]);
}
ArrayList<Integer> old = (ArrayList<Integer>) list.clone();
Collections.sort(list);
ArrayList<Integer> backward = new ArrayList();
for(int i = list.size()-1; i>=0; i--){
backward.add(list.get(i));
}
if(backward.equals(old)){
return true;
}
return false;
}
public static int [][] perm(int num){
int [][] res = new int [fact(num)][num];
int [] cur = new int [num];
for(int i = 1; i<=num; i++){
cur[i-1]=i;
}
res[0]=cur;
for(int i = 1; i<fact(num); i++){
for(int u = 0; u<num; u++){
if(order(cur,u)){
cur = ne(cur, u-1);
res[i]=cur;
break;
}
}
}
return res;
}
// public static boolean next_permutation(int [] a) {
// int p=0, q=0;
// for (p = a.length-2; p>=0; p--) {
// if (a[p]<a[p+1]) {
// break;
// }
// }
// if (p<0 ) {
// return false;
// }
// for (q = a.length-1; q > p; q--) {
// if (a[q]>a[p]) {
// break;
// }
// }
// int t = a[p];
// a[p] = a[q];
// a[q] = t;
// for (int l = p+1, r=a.length-1; l<=r; l++, r--) {
// int t2 = a[l];
// a[l]=a[r];
// a[r]=t2;
// }
// return true;
// }
public static void print (ArrayList<int[]> list){
for(int i = 0; i<list.size();i++){
print(list.get(i));
}
}
public static void main (String [] args){
Scanner input = new Scanner(System.in);
int th = input.nextInt();
int tw = input.nextInt();
int hn = input.nextInt();
String [] dir = new String [th];
for(int i = 0; i<th; i++){
dir[i]=input.next();
}
int [] rv = new int [hn+1];
int [] cv = new int [hn+1];
int cou = 0;
for(int i =0; i<th; i++){
for(int u = 0; u<tw; u++){
if(dir[i].charAt(u)=='G'||dir[i].charAt(u)=='H'){
rv[cou]=i;
cv[cou]=u;
cou++;
}
}
}
int [][] value = new int [hn+1][hn+1];
for(int num = 0; num<=hn; num++){
Queue<point> pt = new LinkedList();
int [][] val = new int [th][tw];
point poi = new point(rv[num],cv[num]);
pt.add(poi);
int result = 1000000;
while(!pt.isEmpty()){
point p = pt.poll();
int cw = p.w;
int ch = p.h;
int count = val[ch][cw]+1;
if(ch>0){
if(dir[ch-1].charAt(cw)!='X'){
if(val[ch-1][cw]==0||val[ch-1][cw]>count){
val[ch-1][cw]=count;
pt.add(new point(ch-1,cw));
}
}
}
if(ch<th-1){
if(dir[ch+1].charAt(cw)!='X'){
if(val[ch+1][cw]==0||val[ch+1][cw]>count){
val[ch+1][cw]=count;
pt.add(new point(ch+1,cw));
}
}
}
if(cw<tw-1){
if(dir[ch].charAt(cw+1)!='X'){
if(val[ch][cw+1]==0||val[ch][cw+1]>count){
val[ch][cw+1]=count;
pt.add(new point(ch,cw+1));
}
}
}
if(cw>0){
if(dir[ch].charAt(cw-1)!='X'){
if(val[ch][cw-1]==0||val[ch][cw-1]>count){
val[ch][cw-1]=count;
pt.add(new point(ch,cw-1));
}
}
}
}
for(int u = num+1; u<=hn; u++){
int rr = rv[u];
int cc = cv[u];
int v = val[rr][cc];
value[num][u]=v;
value[u][num]=v;
}
}
//print(value);
int [][] perm = perm(hn+1);
int result = 1000000;
for(int i = 0; i<perm.length; i++){
int [] cur = perm[i];
int temp = 0;
for(int u = 1; u<cur.length; u++){
int begin = cur[u-1]-1;
int end = cur[u]-1;
temp+=value[begin][end];
}
if(temp<result){
result = temp;
}
}
System.out.println(result);
}
static class point{
public int w;
public int h;
public point (int a, int b){
h = a;
w = b;
}
public String toString(){
return w+" "+h;
}
}
}