Skip to content

Commit d38fcd1

Browse files
authored
[20250204] BOJ / 골드1 / 로봇 청소기 / 권혁준
1 parent 840acbe commit d38fcd1

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st;
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static int nextInt() {return Integer.parseInt(st.nextToken());}
15+
static long nextLong() {return Long.parseLong(st.nextToken());}
16+
static void bwEnd() throws Exception {bw.flush();bw.close();}
17+
18+
// Additional field
19+
static int[] dx = {1,0,-1,0};
20+
static int[] dy = {0,1,0,-1};
21+
static int INF = (int)1e8 + 1;
22+
23+
static int H, W;
24+
static int[][] dist;
25+
static char[][] arr;
26+
static int[] X;
27+
static int[] Y;
28+
static int ans;
29+
30+
static void initialize() {
31+
dist = new int[11][11];
32+
arr = new char[H][W];
33+
X = new int[11];
34+
Y = new int[11];
35+
for(int i=0;i<=10;i++) Arrays.fill(dist[i], INF);
36+
ans = INF;
37+
}
38+
39+
static boolean inside(int x, int y) {
40+
return 0<=x&&x<H && 0<=y&&y<W;
41+
}
42+
43+
static void bfs(int s) {
44+
boolean[][] vis = new boolean[H][W];
45+
vis[X[s]][Y[s]] = true;
46+
Queue<int[]> Q = new LinkedList<>();
47+
Q.offer(new int[] {X[s],Y[s],0});
48+
while(!Q.isEmpty()) {
49+
int[] now = Q.poll();
50+
int x = now[0], y = now[1], t = now[2];
51+
if('A' <= arr[x][y] && arr[x][y] <= 'Z') {
52+
dist[s][arr[x][y]-'A'] = t;
53+
}
54+
for(int i=0;i<4;i++) {
55+
int xx = x+dx[i], yy = y+dy[i];
56+
if(inside(xx,yy) && !vis[xx][yy] && arr[xx][yy] != 'x') {
57+
Q.offer(new int[] {xx,yy,t+1});
58+
vis[xx][yy] = true;
59+
}
60+
}
61+
}
62+
}
63+
64+
static void exSearch(int cnt, int choose, int now, int prev, int time, int limit) {
65+
if(cnt == limit) {
66+
ans = Math.min(ans, time);
67+
return;
68+
}
69+
for(int i=1;i<=limit;i++) {
70+
if((choose & (1<<i)) == 0) exSearch(cnt+1,choose | (1<<i), i, now, time + dist[now][i], limit);
71+
}
72+
}
73+
74+
static void solve() throws Exception{
75+
76+
initialize();
77+
78+
int num = 1;
79+
char temp = 'B';
80+
for(int i=0;i<H;i++) {
81+
String line = br.readLine();
82+
for(int j=0;j<W;j++) {
83+
arr[i][j] = line.charAt(j);
84+
if(arr[i][j] == '*') {
85+
arr[i][j] = temp++;
86+
X[num] = i;
87+
Y[num] = j;
88+
num++;
89+
}
90+
else if(arr[i][j] == 'o') {
91+
arr[i][j] = 'A';
92+
X[0] = i;
93+
Y[0] = j;
94+
}
95+
}
96+
}
97+
98+
for(int i=0;i<num;i++) bfs(i);
99+
100+
for(int i=1;i<num;i++) exSearch(1,(1<<i),i,0,dist[0][i],num-1);
101+
102+
if(ans >= INF) bw.write("-1\n");
103+
else bw.write(ans+"\n");
104+
105+
}
106+
107+
public static void main(String[] args) throws Exception {
108+
109+
nextLine();
110+
W = nextInt();
111+
H = nextInt();
112+
while(H != 0) {
113+
solve();
114+
nextLine();
115+
W = nextInt();
116+
H = nextInt();
117+
}
118+
119+
120+
bwEnd();
121+
}
122+
123+
}
124+
125+
```

0 commit comments

Comments
 (0)