zoukankan      html  css  js  c++  java
  • 【广搜】Knight Moves

    题目描述

    Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? 
    Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov. 
    For people not familiar with chess, the possible knight moves are shown in Figure 1. 

    输入

    The input begins with the number n of scenarios on a single line by itself. 
    Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

    输出

    For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

    样例输入

    3
    8
    0 0
    7 0
    100
    0 0
    30 50
    10
    1 1
    1 1
    

    样例输出

    5
    28
    0



    【题目】

    在[0,300]范围的棋盘上,给定起点,终点和8中行走方式,求从起点到终点的最少步数。

    【思路】

    给定了起始状态和结束状态,求最少步数。显然是用BFS,为了节省时间,我选择了双向BFS。双向BFS即从起点向终点搜,从终点向起点搜,扩展各自的状态,直到出现两者扩展的状态重合。一个优化,每次选择结点少的扩展。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int dir[8][2] = {
     4         {-2,-1} , {-2,1},
     5     {-1,-2},           {-1,2},
     6     {1,-2},            {1,2},
     7         {2,-1},   {2,1}
     8 };
     9 struct qtp{
    10     int x,y;
    11 }q[2][100005];
    12 int test,ans,n,L[2],R[2];
    13 int dis[2][301][301];
    14 int vis[2][301][301];
    15 int expand(int k){
    16     int t,x,y,d,tx,ty;
    17     x = q[k][L[k]].x;
    18     y = q[k][L[k]].y;
    19     d = dis[k][x][y];
    20     for(int i=0;i<8;i++){
    21         tx = x + dir[i][0];
    22         ty = y + dir[i][1];
    23         if( 0<=tx && tx<=n && 0<=ty && ty<=n && !vis[k][tx][ty] ){
    24             vis[k][tx][ty] = 1;
    25             R[k] ++ ;
    26             q[k][R[k]].x = tx ;
    27             q[k][R[k]].y = ty ;
    28 
    29             dis[k][tx][ty] = d+1;
    30             if ( vis[1-k][tx][ty] ){
    31                 ans = dis[k][tx][ty] + dis[1-k][tx][ty];
    32                 return 1;
    33             }
    34         }
    35     }
    36     return 0;
    37 }
    38 void BFS(){
    39     if( q[0][1].x == q[1][1].x && q[0][1].y == q[1][1].y ){
    40         ans = 0;
    41         return ;
    42     }
    43     vis[0][q[0][1].x][q[0][1].y] = 1 ;
    44     vis[1][q[1][1].x][q[1][1].y] = 1 ;
    45     L[0] = R[0] = 1 ;
    46     L[1] = R[1] = 1;
    47     while ( L[0] <= R[0] && L[1] <= R[1] ){
    48         if( R[0] - L[0] < R[1] - L[1] ){
    49             if ( expand(0) ) return ;
    50             L[0] ++ ;
    51         }else{
    52             if ( expand(1) ) return ;
    53             L[1] ++ ;
    54         }
    55     }
    56 }
    57 int main()
    58 {
    59 
    60     for( cin>>test ; test ; test-- ){
    61         memset(vis,0,sizeof vis);
    62         memset(q,0,sizeof q );
    63         memset(dis,0,sizeof dis) ;
    64         cin >> n ;
    65         n--;
    66         cin >> q[0][1].x >> q[0][1].y ;
    67         cin >> q[1][1].x >> q[1][1].y ;
    68         BFS();
    69         cout << ans << endl;
    70     }
    71     return 0;
    72 }
    Knight Moves
  • 相关阅读:
    伪元素 first-letter
    html语义化 -------<fieldset>和<legend>
    《ASP.NET MVC4 WEB编程》学习笔记------ViewBag、ViewData和TempData的使用和区别
    《ASP.NET MVC4 WEB编程》学习笔记------.net mvc实现原理ActionResult/View
    《ASP.NET MVC4 WEB编程》学习笔记------RenderBody,RenderPage,RenderSection
    《转》Visual Studio 2010 终极定制安装精简方法
    《转》IIS中配置通配符应用程序映射
    IIS安装时,添加/编辑应用程序扩展名映射 确定按钮不可用。
    异常:操作可能会破坏运行时稳定性
    petri网学习心得
  • 原文地址:https://www.cnblogs.com/Osea/p/11215695.html
Copyright © 2011-2022 走看看