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 ('000000' is smaller than '000000 ').
Sample Input
3bbwbwbwbwbwwwbwbwbbbbbwbbbw
Sample output
2459 267356789
Question Analysis:
Perform the breadth traversal as shown in the figure, but set a flag so that the traversal will not be repeated. Pay attention to the output of the wwwwwwwww 11, which has been suspended for half an hour.
/**/# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h> using namespace STD; int data [9] [10] = {1, 2, 4, 5, 0}, {1, 2, 3, 4, 5, 0}, {2, 3, 6, 0 },{, 0}, {,}, {, 0, 0}, {, 0}, {5, 6, 8, 9, 0 }}; typedef struct status {unsigned int A; vector <int> record;} status; inline bool judge (int A) {if (a = 0) return true; return false;} int main () {int N; CIN> N; For (INT xx = 0; XX <n; XX ++) {unsigned int gra = 0; For (INT I = 0; I <9; I ++) // W is 0, B is 1 {char X; CIN> X; if (x = 'B') gra + = POW (2.0, I) ;}if (GRA = 0) {cout <11 <Endl; continue ;} status TMP; TMP. A = GRA; bool flag [513]; memset (flag, 0, sizeof (FLAG); queue <status> QE; QE. push (TMP ); Flag [gra] = true; while (1) {status wh = QE. front (); flag [Wh. a] = true; QE. pop (); If (Judge (Wh. a) {for (vector <int >:: size_type I = 0; I <Wh. record. size (); I ++) cout <Wh. record [I] + 1; cout <Endl; break;} For (INT I = 0; I <9; I ++) {status tt; TT. A = Wh. a; TT. record = Wh. record; For (Int J = 0; j <9 & Data [I] [J]! = 0; j ++) {unsigned int QW = POW (2.0, data [I] [J]-1); TT. A = TT. A ^ QW;} // end for Jif (flag [TT. a] = true) continue; TT. record. push_back (I); QE. push (TT);} // end for I} // end while }}