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. 

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。 node

另外,本題最好採用二進制表示棋盤狀態,操做簡便並且節省運行時間,代碼也更簡潔,而9種操做對應的是當前狀態數和特定數字的異或。另外,狀態矩陣是必需的,否則可能會超時。 ios

PS:此題天雷滾滾的地方是,答案必需是1-9的數字,也就是說輸入是wwwwwwwww的時候,輸出應該是11,卡了我很長時間才搞對。 spa

代碼:

// Problem#: 1048
// Submission#: 1853201
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <queue>
#include <cstring>
using namespace std;

#define END 0

struct node{
    int v;
    string f;
    node( int n ){
        v = n;
        f = "";
    }
    node( int n, string s ){
        v = n;
        f = s;
    }
};

int field[10]={0,432,504,216,438,511,219,54,63,27};
bool visit[1<<9];

inline int toi( string s ){
    int re = 0;
    for( int i=0 ; i<9 ; i++ ){
        re <<= 1;
        re |= s[i]=='b' ? 1 : 0 ;
    }
    return re;
}

void bfs( int a ){
    queue<node> buffer;
    buffer.push(node(a));
    visit[a] = true;
    while( !buffer.empty() ){
        node temp = buffer.front();
        buffer.pop();
        if( temp.v==END ){
            cout << temp.f << endl;
            return ;
        }
        for( int i=1 ; i<=9 ; i++ ){
            int t = temp.v^field[i];
            if( !visit[t] ){
                visit[t] = true;
                string s = temp.f;
                s += '0'+i;
                buffer.push(node(t,s));
            }
        }
    }
}

int main(){
    int n;
    string str;
    int start;
    cin >> n;
    while( n-- ){
        cin >> str;
        start = toi(str);
        memset(visit,false,sizeof(visit));
        if( start==0 ) cout << 11 << endl;
        else bfs(start);
    }
    return 0;
}
相關文章
相關標籤/搜索