zoukankan      html  css  js  c++  java
  • Knight Moves

    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

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 int x, y;
     7 int x0, y0;
     8 int l;
     9 
    10 struct node{
    11     int x, y;
    12     int step;
    13 }pos,q;
    14 
    15 int dir[8][2] = {
    16     {2,1},
    17     {1,2},
    18     {2,-1},
    19     {1,-2},
    20     {-1,-2},
    21     {-2,-1},
    22     {-2,1},
    23     {-1,2},
    24 };
    25 
    26 int vis[310][310];
    27 void bfs(int x0, int y0){
    28     memset(vis,0,sizeof(vis));
    29     queue<node> que;
    30     pos.x = x0;
    31     pos.y = y0;
    32     pos.step = 0;
    33     vis[x0][y0] = 1;
    34     que.push(pos);
    35     while(!que.empty()){
    36         pos = que.front();
    37         que.pop();
    38         vis[pos.x][pos.y] = 1;
    39         if(pos.x == x && pos.y == y){
    40             cout << pos.step << endl;
    41             return ;
    42         }
    43         for(int i = 0; i < 8; i++){
    44             int next_x = pos.x + dir[i][0];
    45             int next_y = pos.y + dir[i][1];
    46             if(next_x >= 0 && next_x < l && next_y >= 0 && next_y < l && vis[next_x][next_y] == 0){
    47                 q.x = next_x;
    48                 q.y = next_y;
    49                 q.step = pos.step + 1;
    50                 vis[next_x][next_y] = 1;
    51                 que.push(q);
    52             }
    53         }
    54     }
    55 }
    56 
    57 int main(){
    58     int n;
    59     cin >> n;
    60     while(n--){
    61         cin >> l;
    62         cin >> x0 >> y0;
    63         cin >> x >> y;
    64         bfs(x0, y0);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    Oracle 11g设置IP访问限制
    ORA-01940 无法删除当前已连接的用户之解决方案
    如何终止正在进行expdp导出数据的任务
    Oracle权限管理详解
    linux yum配置代理
    命令别名设置: alias, unalias
    Linux 桌面双击运行脚本
    变量内容的删除、取代与替换 (Optional)
    linux查看和修改PATH环境变量的方法
    文件系统及程序的限制关系: ulimit
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6705176.html
Copyright © 2011-2022 走看看