zoukankan      html  css  js  c++  java
  • BZOJ 1085: [SCOI2005]骑士精神( IDDFS + A* )

     一开始写了个 BFS 然后就 T 了...

    这道题是迭代加深搜索 + A*

    ------------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
     
    #define rep( i , n ) for( int i = 0 ; i < n ; ++i )
    #define clr( x , c ) memset( x , c , sizeof( x ) )
     
    using namespace std;
     
    const int N = 5;
    const int goal[ N ][ N ] = { { 1 , 1 , 1 , 1 , 1 } ,
                                 { 0 , 1 , 1 , 1 , 1 } , 
                                 { 0 , 0 , 2 , 1 , 1 } ,
                                 { 0 , 0 , 0 , 0 , 1 } , 
                                 { 0 , 0 , 0 , 0 , 0 } };
    const int dir[ 8 ][ 2 ] = { { 2 , -1 } , { 2 , 1 } , { -2 , -1 } , { -2 , 1 } ,
                                { -1 , 2 } , { 1 , 2 } , { -1 , -2 } , { 1 , -2 } };
     
    #define check( x , y ) ( 0 <= x && x < N && 0 <= y && y < N )
     
    int t[ N ][ N ] , ans;
    bool flag;
     
    bool f( int d ) {
    rep( i , N )
       rep( j , N ) if( goal[ i ][ j ] != t[ i ][ j ] ) 
           if( ++d > ans ) return false;
    return true;
    }
     
    void dfs( int x , int y , int d ) {
    if( d == ans && !memcmp( t , goal , sizeof t ) ) {
       flag = true;
       return ;
    }
    if( flag ) return;
    rep( i , 8 ) {
    int dx = x + dir[ i ][ 0 ] , dy = y + dir[ i ][ 1 ];
    if( ! check( dx , dy ) ) continue;
    swap( t[ dx ][ dy ] , t[ x ][ y ] );
    if( f( d ) ) dfs( dx , dy , d + 1 );
    swap( t[ dx ][ dy ] , t[ x ][ y ] );
    }
    }
     
    #define ok( x ) ( x == '0' || x == '1' || x == '*' )
     
    int main() {
    freopen( "test.in" , "r" , stdin );
    int r;
    scanf( "%d" , &r );
    while( r-- ) {
    int x , y;
    rep( i , N ) 
    rep( j , N ) {
    char c = getchar();
    while( ! ok( c ) ) c = getchar();
    if( c == '*' )
       t[ x = i ][ y = j ] = 2;
    else 
       t[ i ][ j ] = c - '0';
    }
    flag = false;
    for( ans = 1 ; ans <= 15 ; ans++ ) {
    dfs( x , y , 0 );
    if( flag ) break;
    }
    printf( "%d " , flag ? ans : -1 );
    }
    return 0;
    }

      

    ------------------------------------------------------------------------------

    1085: [SCOI2005]骑士精神

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1109  Solved: 601
    [Submit][Status][Discuss]

    Description

    在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位。在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵坐标相差为1的格子)移动到空位上。 给定一个初始的棋盘,怎样才能经过移动变成如下目标棋盘: 为了体现出骑士精神,他们必须以最少的步数完成任务。

    Input

    第一行有一个正整数T(T<=10),表示一共有N组数据。接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位。两组数据之间没有空行。

    Output

    对于每组数据都输出一行。如果能在15步以内(包括15步)到达目标状态,则输出步数,否则输出-1。

    Sample Input

    2
    10110
    01*11
    10111
    01001
    00000
    01011
    110*1
    01110
    01010
    00100

    Sample Output

    7
    -1

    HINT

    Source

  • 相关阅读:
    世界编程大赛第一名编写的程序3D世界
    bool与BOOL
    防浪涌电路
    用户至上,体验第一
    VC菜菜鸟创建多线程任务HelloWorld
    Google,a good dog
    算法学习之路
    巧用VC工程下的rc文件
    堆与栈
    关于信息量的压缩
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4632648.html
Copyright © 2011-2022 走看看