forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipfive.cpp
95 lines (79 loc) · 1.9 KB
/
flipfive.cpp
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
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
int board_to_num(vector<vector<char>>& v) {
int total = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(v[i][j] == '*') {
total += pow(2, (i*3)+j);
}
}
}
return total;
}
void charswap(vector<vector<char>>& v, int i, int j) {
if(i < 0 || i >= 3 || j < 0 || j >= 3) {
return;
}
if(v[i][j] == '.') {
v[i][j] = '*';
}
else {
v[i][j] = '.';
}
}
void swapall(vector<vector<char>>& v, int i, int j) {
charswap(v, i, j);
charswap(v, i+1, j);
charswap(v, i-1, j);
charswap(v, i, j+1);
charswap(v, i, j-1);
}
void fill(vector<vector<char>>& v, vector<int>& score, int i, int j, int dist) {
swapall(v, i, j);
int state = board_to_num(v);
if(score[state] <= dist) {
swapall(v, i, j);
return;
}
score[state] = dist;
for(int k = 0; k < 3; k++) {
for(int l = 0; l < 3; l++) {
fill(v, score, k, l, dist+1);
}
}
swapall(v, i, j);
}
void fill_board(vector<vector<char>>& v, vector<int>& score) {
score[0] = 0;
for(int k = 0; k < 3; k++) {
for(int l = 0; l < 3; l++) {
fill(v, score, k, l, 1);
}
}
}
int main() {
int n;
cin >> n;
// Set up the memory
vector<vector<char>> v;
v.resize(3, vector<char>(3, '.'));
// Fill in the distance for each position
vector<int> score;
score.resize(512, 10000);
fill_board(v, score);
// Take in the data, get the distance
for(int i = 0; i < n; i++) {
// Read in the array
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
cin >> v[j][k];
}
}
int dist = score[board_to_num(v)];
cout << dist << endl;
}
}