一开始写了个 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\n" , 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

BZOJ 1085: [SCOI2005]骑士精神( IDDFS + A* )的更多相关文章

  1. Bzoj 1085: [SCOI2005]骑士精神 (dfs)

    Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: ...

  2. BZOJ 1085 [SCOI2005]骑士精神 【A*启发式搜索】

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2838  Solved: 1663 [Submit][St ...

  3. [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]

    题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS ...

  4. bzoj 1085: [SCOI2005]骑士精神

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...

  5. [BZOJ 1085][SCOI2005]骑士精神(IDA*)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 分析: 首先第一感觉是宽搜,但是空间需要8^15*5*5,明显不够,又鉴于最大深 ...

  6. BZOJ.1085.[SCOI2005]骑士精神(迭代加深搜索)

    题目链接 最小步数这类,适合用迭代加深搜索. 用空格走代替骑士. 搜索时记录上一步防止来回走. 不需要每次判断是否都在位置,可以计算出不在对应位置的骑士有多少个.而且每次复原一个骑士至少需要一步. 空 ...

  7. bzoj 1085 [SCOI2005]骑士精神——IDA*

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 迭代加深搜索. 估价函数是为了预计步数来剪枝,所以要优于实际步数. 没错,不是为了确定 ...

  8. bzoj 1085: [SCOI2005]骑士精神 IDA*

    题目链接 给一个图, 目标位置是确定的, 问你能否在15步之内达到目标位置. 因为只有15步, 所以直接ida* #include<bits/stdc++.h> using namespa ...

  9. BZOJ 1085: [SCOI2005]骑士精神(A*算法)

    第一次写A*算法(这就是A*?如果这就是A*的话,那不就只是搜索的一个优化了= =,不过h函数如果弄难一点真的有些难设计) 其实就是判断t+h(x)(t为当前步数,h(x)为达到当前状态的最小步数) ...

随机推荐

  1. Romantic(裸扩展欧几里德)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. C++的一些编程规范(基于google)

    1.所有头文件都应该使用#define 防止头文件被多重包含,命名格式可以参考<PROJECT>_<PATH>_<FILE>_H 2.使用前置声明尽量减少.h文件中 ...

  3. poj2486 Apple Tree【区间dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4374766.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  4. Extending your SharePoint 2007 site with Microsoft ASP.NET AJAX 3.5

    After ASP.NET 3.5 has been installed you need to modify the web.config file of your MOSS web site wi ...

  5. SharePoint Secure Store Service(SSSS)的使用(一)

    SSS在案例中的应用: SSS介绍 SSS部署 SSS应用 http://www.cnblogs.com/renzh/archive/2013/03/31/2990280.html 创建.部署.应用S ...

  6. Centos6.4安装JDK

    链接地址:http://www.iteye.com/topic/1130311 1.先看看OpenJDK的安装包 $ rpm -qa |grep javatzdata-java-2013b-1.el6 ...

  7. 【转】论文、会议、期刊评价|Indicate paper, conference, Journal

    转自“浙江大学计算机学院软硬件协同设计实验室”:http://multicore.zju.edu.cn/fatlab/Indicate-paper.htm 1           体系结构领域,排名为 ...

  8. android获取系统wifi状态等

    WIFI 获取WIFI状态 WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); ...

  9. Ubuntu实现双网卡双IP双待机

    Ubuntu实现双网卡双IP双待机 待机是借用了手机中的说法,其实是电脑上有两个网卡,一个无线,一个有线的.要实现无线访问外网Google Baidu查资料,有线网卡直接连接开发板.在Ubuntu上配 ...

  10. uva11178 Morley’s Theorem(求三角形的角三分线围成三角形的点)

    Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states that that the ...