sicily 1048 Inverso
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.
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
分析:
本题主要考察广度搜索方法的应用。首先,注意到本题是操作次数(即生成图的边)最重要,其次才是顶点顺序,那么就要采用以边为搜索线索的广度搜索,而非深搜,深搜会WA。
另外,本题最好采用二进制表示棋盘状态,操作简便而且节省运行时间,代码也更简洁,而9种操作对应的是当前状态数和特定数字的异或。另外,状态矩阵是必需的,不然可能会超时。
PS:此题天雷滚滚的地方是,答案必需是1-9的数字,也就是说输入是wwwwwwwww的时候,输出应该是11,卡了我很长时间才搞对。
代码:
1 // Problem#: 1048 2 // Submission#: 1853201 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include <iostream> 7 #include <string> 8 #include <queue> 9 #include <cstring> 10 using namespace std; 11 12 #define END 0 13 14 struct node{ 15 int v; 16 string f; 17 node( int n ){ 18 v = n; 19 f = ""; 20 } 21 node( int n, string s ){ 22 v = n; 23 f = s; 24 } 25 }; 26 27 int field[10]={0,432,504,216,438,511,219,54,63,27}; 28 bool visit[1<<9]; 29 30 inline int toi( string s ){ 31 int re = 0; 32 for( int i=0 ; i<9 ; i++ ){ 33 re <<= 1; 34 re |= s[i]=='b' ? 1 : 0 ; 35 } 36 return re; 37 } 38 39 void bfs( int a ){ 40 queue<node> buffer; 41 buffer.push(node(a)); 42 visit[a] = true; 43 while( !buffer.empty() ){ 44 node temp = buffer.front(); 45 buffer.pop(); 46 if( temp.v==END ){ 47 cout << temp.f << endl; 48 return ; 49 } 50 for( int i=1 ; i<=9 ; i++ ){ 51 int t = temp.v^field[i]; 52 if( !visit[t] ){ 53 visit[t] = true; 54 string s = temp.f; 55 s += '0'+i; 56 buffer.push(node(t,s)); 57 } 58 } 59 } 60 } 61 62 int main(){ 63 int n; 64 string str; 65 int start; 66 cin >> n; 67 while( n-- ){ 68 cin >> str; 69 start = toi(str); 70 memset(visit,false,sizeof(visit)); 71 if( start==0 ) cout << 11 << endl; 72 else bfs(start); 73 } 74 return 0; 75 }