Skip to content

Commit 2f1f88a

Browse files
authored
Merge pull request #147 from AlgorithmWithGod/khj20006
[20250219] BOJ / G4 / 페그 솔리테어 / 권혁준
2 parents 437e6d3 + 25dd9d7 commit 2f1f88a

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
```cpp
2+
3+
#include <iostream>
4+
#include <vector>
5+
using namespace std;
6+
7+
int dx[4] = {1,0,-1,0};
8+
int dy[4] = {0,1,0,-1};
9+
char A[5][9]{};
10+
vector<pair<int,int>> P;
11+
bool vis[8]{};
12+
int cnt, mv;
13+
14+
bool ok(int x, int y) {return 0<=x&&x<5 && 0<=y&&y<9;}
15+
bool isNumber(int x, int y) {return '0'<=A[x][y] && A[x][y]<='9';}
16+
17+
void bck(int c, int m){
18+
if(c < cnt || (c == cnt && m < mv)) cnt = c, mv = m;
19+
if(!c) return;
20+
for(int i=0;i<P.size();i++) if(!vis[i]){
21+
auto &[x,y] = P[i];
22+
int px = x, py = y;
23+
for(int k=0;k<4;k++){
24+
int nx = x+dx[k], ny = y+dy[k];
25+
if(!ok(nx,ny) || !isNumber(nx,ny)) continue;
26+
int nnx = nx+dx[k], nny = ny+dy[k];
27+
if(!ok(nnx,nny) || A[nnx][nny] != '.') continue;
28+
29+
int rem = A[nx][ny] - '0';
30+
vis[rem] = 1;
31+
A[nnx][nny] = A[x][y];
32+
A[nx][ny] = '.';
33+
A[x][y] = '.';
34+
x = nnx, y = nny;
35+
bck(c-1, m+1);
36+
x = px, y = py;
37+
vis[rem] = 0;
38+
A[x][y] = A[nnx][nny];
39+
A[nx][ny] = '0' + rem;
40+
A[nnx][nny] = '.';
41+
}
42+
}
43+
}
44+
45+
int main(){
46+
cin.tie(0)->sync_with_stdio(0);
47+
48+
int T;
49+
for(cin>>T;T--;){
50+
cnt = 0, mv = 0;
51+
P = vector<pair<int, int>>();
52+
for(int i=0;i<8;i++) vis[i]=0;
53+
for(int i=0;i<5;i++) for(int j=0;j<9;j++) {
54+
cin>>A[i][j];
55+
if(A[i][j] == 'o') {
56+
P.emplace_back(i,j), A[i][j] = '0' + (cnt);
57+
cnt++;
58+
}
59+
}
60+
bck(cnt, 0);
61+
cout<<cnt<<' '<<mv<<'\n';
62+
}
63+
64+
}
65+
66+
```

0 commit comments

Comments
 (0)