zoukankan      html  css  js  c++  java
  • poj 1915 Knight Moves

    Knight Moves
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 26068   Accepted: 12288

    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

    Source

    TUD Programming Contest 2001, Darmstadt, Germany
    一定要注意下表是从0开始的!
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxn=3000;
     7 
     8 struct node
     9 {
    10     int x;int y;int step;
    11 }now,next;
    12 queue<node>que;
    13 
    14 int mov[8][2]={{-1, -2},{-1, 2},{-2, -1},{-2, 1},{1, -2},{1, 2},{2, -1},{2, 1}};
    15 
    16 int n,m;int qq;int a,b,L;
    17 
    18 bool vis[maxn][maxn];
    19 
    20 int ans=0;
    21 
    22 void bfs(int x,int y)
    23 {
    24     now.x=x;
    25     now.y=y;
    26     now.step=0;
    27     que.push(now);
    28     while(!que.empty())
    29     {
    30         now=que.front();
    31         que.pop();
    32         for(int i=0;i<8;i++)
    33         {
    34             int xx=now.x+mov[i][0];
    35             int yy=now.y+mov[i][1];
    36             if( xx==a && yy==b )
    37             {
    38                 ans=now.step+1;
    39                 return;
    40             }
    41             else if(yy>=0&&xx>=0&&xx<L&&yy<L&&!vis[xx][yy])
    42             next.x=xx,vis[xx][yy]=1,next.y=yy,next.step=now.step+1,que.push(next);
    43         }
    44     }    
    45 }
    46 
    47 int main()
    48 {
    49     cin>>qq;
    50     for(int i=1;i<=qq;i++)
    51     {
    52         ans=0;
    53         memset(vis,0,sizeof(vis));
    54         while(!que.empty())que.pop();
    55         cin>>L;
    56         scanf("%d%d%d%d",&n,&m,&a,&b);
    57         if(n==a&&m==b)
    58         {
    59             cout<<0<<endl;continue;
    60         }
    61         
    62         else bfs(n,m);
    63         printf("%d
    ",ans);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    xe5 android tts(Text To Speech) [转]
    xe5 android sample 中的 SimpleList 是怎样绑定的 [转]
    xe5 android 控制蓝牙[转]
    xe5 android 调用照相机获取拍的照片[转]
    XE5 Android 开发数据访问手机端[转]
    XE5 Android 开发实现手机打电话和发短信 [转]
    让VCL的皮肤用在手机程序里 让安桌程序不山寨[转]
    XE5 Android 开发数据访问server端[转]
    XE5 Android 开发实现手机打电话和发短信[转]
    Delphi XE5的Android开发平台搭建[转]
  • 原文地址:https://www.cnblogs.com/sssy/p/6809874.html
Copyright © 2011-2022 走看看