bfs (mark the entire board)

1004 Connect Four even chess

 

Time limit: 1 s
Space limitations: 128000 KB
Topic grade: Gold Gold
 
 
 
Title Description Description

On board a 4 * 4 placed the pieces 14, of which seven pieces of white, black pieces 7, there are two blank area, any one of Othello child can be moved vertically and horizontally in four directions to the adjacent space, called the line moves one step, black and white sides alternately playing chess, you can either go first , if at some point make any color pieces to form four first-line (including the slash), such a state as the goal chess game.

 
 

 

Enter a description Input Description
Read from the file into a chess initial 4 * 4, B is represented by black stones, white stones is represented by W, a space zone indicated by O.
Output Description Output Description

Chess moves to the target number of steps with a minimum number of steps.

Sample input Sample Input

BWBO
WBWB
BWBW
WBWO

Sample Output Sample Output

5

Data range and prompt Data Size & Hint

hi

There iterative deepening search methods to be explored.

#include<bits/stdc++.h>
#define rep(i , n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
char m[4][4];
int dir[4][2] = {{1 , 0},{-1 , 0},{0 , 1},{0, -1}};
struct node{
    char M[4][4];
    int dis ;
    char last;
    node(charm [ . 4 ] [ . 4 ], int D = 0 , char C = ' B ' ) 
    { 
        REP (I, . 4 ) 
        { 
            REP (J, . 4 ) 
            { 
                M [I] [J] = m [I] [J] ; 
            } 
        } 
        DIS = D; // mobile steps 
        Last = C; // tag plate of the chess move 
    } 
}; 

String CON ( char m [ . 4 ] [ . 4 ])// comparison with the previous board after the movement is the same as the checkerboard map <string, int> with binding 
{
     String S = "" ; 
    REP (I, . 4 ) 
    REP (J, . 4 ) 
    S + = m [I] [ J];
     return S; 
} 

BOOL Judge ( char m [ . 4 ] [ . 4 ]) // determines whether or even four sub chess 
{ 
    REP (I, . 4 ) 
    { 
        iF (m [I] [ 0 ] == m [I ] [ . 1 ] && m [I] [ . 1 ] == m [I] [ 2 ] && m [I] [ 2 ] == m [I] [ . 3])
            return true ;
        if(m[0][i] == m[1][i] && m[1][i] == m[2][i] && m[2][i] == m[3][i])
            return true ;
    }
    if(m[0][0] == m[1][1] && m[1][1] == m[2][2] && m[2][2] == m[3][3])
        return true ;
    if(m[3][0] == m[2][1] && m[2][1] == m[1][2] && m[1][2] == m[0][3])
        return true ;
    return false ;
}
map<string , int>vis;

int main()
{
    rep(i , 4)
    rep(j , 4)
    cin >> m[i][j];
    queue<node>q;
    q.push(node(m , 0 , 'O'));
    vis[con(m)] = 1 ;
    while(!q.empty())
    {
        node t = q.front() ;
        q.pop() ;
        if(judge(t.M))
        {
            cout << t.dis <<endl ;
            return 0 ;
        }
        int x[2] , y[2];
        int num = -1 ;
        rep(i , 4)
        {
            rep(j , 4)
            {
                if(t.M[i][j] == 'O')
                {
                    x[++num] = i;
                    y[num] = j ;
                }
            }
        }
        rep(i , 2)
        {
            rep(j , 4)
            {
                int xx = x[i] + dir[j][0];
                int yy = y[i] + dir[j][1];
                char temp[4][4];
                rep(i , 4)
                rep(j , 4)
                temp[i][j] = t.M[i][j];
                if(xx >= 0 && xx < 4 && yy >= 0 && yy < 4 &&t.M[xx][yy]!='O' && t.M[xx][yy] != t.last) //空与空不移,不和上一次移过的移
                {
                    temp[x[i]][y[i]] = temp[xx][yy];
                    temp[xx][yy] = ' O ' ; 
                } 
                String S = CON (TEMP);
                 IF (VIS [S]!) // same whether the board after the previous shift End 
                { 
                    VIS [S] = . 1 ; 
                    q.push (Node (TEMP, T .dis + . 1 , TEMP [X [I]] [Y [I]]));
                     IF (Judge (TEMP)) 
                    { 
                        COUT << t.dis + . 1 << endl;
                         return  0 ; 
                    } 
                } 
            } 
        }
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11256457.html