zoukankan      html  css  js  c++  java
  • Knight Moves

    Description

    Background  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?  The Problem  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. 

    Input

    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.

    Output

    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.

    Sample Input

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

    Sample Output

    5
    28
    0

    题意:给一个m,n。m指几组数据,n指在这组数据中map【n】【n】,然后给起点,和终点,问最少几步完成,日字形走路,就是象棋中的马
    tip:广搜


     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int fx,fy,lx,ly,n,m;
     8 int map[305][305],dis[305][305];///前者放答案,后者为标记数组
     9 int x[]={1,-1,1,-1,2,-2,2,-2};
    10 int y[]={2,-2,-2,2,1,-1,-1,1};///八个方向
    11 
    12 void bfs()
    13 {
    14     queue<int> q;///队列,放可以到达的点
    15     q.push(fx);
    16     q.push(fy);
    17 
    18     memset(map,0,sizeof(map));
    19     memset(dis,0,sizeof(dis));
    20     dis[fx][fy]=1;
    21 
    22     int vx,vy,tx,ty;
    23     while(!q.empty())
    24     {
    25         vx=q.front();
    26         q.pop();
    27         vy=q.front();
    28         q.pop();
    29         if(vx==lx&&vy==ly)///到达终点
    30             break;
    31 
    32         for(int i=0;i<8;i++)
    33         {
    34             tx=vx+x[i];
    35             ty=vy+y[i];
    36             if(!dis[tx][ty]&&tx<m&&tx>=0&ty<m&&ty>=0)
    37             {
    38                 q.push(tx);
    39                 q.push(ty);
    40                 dis[tx][ty]=1;///标记,,,有
    41                 map[tx][ty]=map[vx][vy]+1;///相当于递归作用
    42             }
    43         }
    44 
    45     }
    46 }
    47 
    48 int main()
    49 {
    50     cin>>n;
    51     while(n--)
    52     {
    53         cin>>m;
    54         cin>>fx>>fy>>lx>>ly;
    55         bfs();
    56         cout<<map[lx][ly]<<endl;
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    EPUB书籍阅读器插件分享
    网页端压缩解压缩插件JSZIP库的使用
    让编辑器支持word的复制黏贴,支持截屏的黏贴
    MYSQL GTID position
    Google SRE
    MySQL大小写敏感
    SpringMVC model 多余字段 忽略
    To B Vs To C
    滴滴 CTO 架构师 业务 技术 战役 时间 赛跑 超前 设计
    Spring Boot 集成Swagger
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4773135.html
Copyright © 2011-2022 走看看