1048. Inverso
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The game of ‘Inverso’ is played on a 3x3 grid of colored fields (each field is either black or white). Each field is numbered as follows:
1 2 3
4 5 6
7 8 9
The player can click on a field, which will result in the inversion of that field and all its direct neighboring fields (i.e. the color changes from black to white or vice-versa). Hence,
Clicking field 1 inverts fields 1, 2, 4, 5
Clicking field 2 inverts fields 1, 2, 3, 4, 5, 6
Clicking field 3 inverts fields 2, 3, 5, 6
Clicking 4 inverts fields 1, 2, 4, 5, 7, 8
Clicking 5 inverts fields all fields
Clicking 6 inverts fields 2, 3, 5, 6, 8, 9
Clicking 7 inverts fields 4, 5, 7, 8
Clicking 8 inverts fields 4, 5, 6, 7, 8, 9
Clicking 9 inverts fields 5, 6, 8, 9
The aim of the game is to find the shortest sequence of clicks to make all fields white from a given start coloring of the grid.
Input
The first line contains a number N (0≤N≤10000) of runs. The following N lines each contain a string of nine letters ‘b’ (black) or ‘w’ (white) for the color of the fields 1 to 9. This is the initial coloring of the grid.
Output
For each input string the shortest word in the letters ‘1’… ‘9’ (for clicking field one, …, nine) which makes all fields white. If there is more than one shortest word then the lexicographically smallest one must be printed (‘1234’ is smaller than ‘1342’).
Sample Input
3 bbwbwbwbw bwwwbwbwb bbbbwbbbw
Sample Output
2459 267 356789
看了别人的代码,自己打了一遍,BFS,找出所有状态(2^9 = 512);
#include <stdio.h>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
int d[10];
int ans[512];
vector<char> ans_step[512];
int opp[9][9] = {1, 2, 4, 5, 0, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 0, 0, 0,
2, 3, 5, 6, 0, 0, 0, 0, 0,
1, 2, 4, 5, 7, 8, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9,
2, 3, 5, 6, 8, 9, 0, 0, 0,
4, 5, 7, 8, 0, 0, 0, 0, 0,
4, 5, 6, 7, 8, 9, 0, 0, 0,
5, 6, 8, 9, 0, 0, 0, 0, 0};
bool ok() {
for (int i = 0; i < 9; i++) {
if (d[i] == 1)
return false;
}
return true;
}
struct step {
int num;
vector<char> v;
step(){}
step(int n) {
num = n;
}
step(int n, char next_step) {
num = n;
v.push_back(next_step);
}
step(int n, vector<char> temp) {
num = n;
for (int i = 0; i < (int)temp.size(); i++) {
v.push_back(temp[i]);
}
}
};
int change(bool test[], int op) {
bool test_next_step[10];
for (int i = 0; i < 9; i++) {
test_next_step[i] = test[i];
}
for (int i = 0; i < 9 && opp[op][i]; i++) {
test_next_step[opp[op][i] - 1] = test[opp[op][i] - 1] ^ 1;
}
int sum = 0;
for (int i = 0; i < 9; i++) {
sum += test_next_step[i] * (int)pow(2, i);
}
return sum;
}
void BFS() {
queue<step> q;
q.push(step(0));
int size;
step temp, next;
while (!q.empty()) {
size = q.size();
while (size--) {
temp = q.front();
q.pop();
bool test[10];
int temp_num = temp.num;
for (int i = 0; i < 9; i++) {
test[i] = temp_num % 2;
temp_num /= 2;
}
for (int i = 8; i >= 0; i--) {
int next_num = change(test, i);
if (ans_step[next_num].empty()) {
for (int j = 0; j < (int)temp.v.size(); j++) {
ans_step[next_num].push_back(temp.v[j]);
}
ans_step[next_num].push_back(i + '1');
q.push(step(next_num, ans_step[next_num]));
}
}
}
}
}
int main() {
int case_num;
BFS();
ans_step[0].clear();
ans_step[0].push_back('1');
ans_step[0].push_back('1');
char temp[10];
scanf("%d\n", &case_num);
while (case_num--) {
int index = 0;
gets(temp);
for (int i = 0; i < 9; i++) {
index += (temp[i] == 'w' ? 0 : 1) * (int)pow(2, i);
}
for (int i = (int)ans_step[index].size() - 1; i >= 0; i--) {
printf("%c", ans_step[index][i]);
}
printf("\n");
}
return 0;
}