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

    Knight Moves
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 22486   Accepted: 10511

    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

    广度优先

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 int s;
     6 struct point{
     7     bool ex;
     8     int step;
     9 };
    10 point m[300+5][300+5];
    11 int d[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
    12 bool check(int x,int y){
    13     if(x<0||x>=s||y<0||y>=s||m[x][y].ex)
    14     return true;
    15     return false;
    16 }
    17 int main(){
    18     int n,sx,sy,ex,ey;
    19     cin>>n;
    20     while(n--){
    21         cin>>s;
    22         cin>>sx>>sy;
    23         cin>>ex>>ey;
    24         memset(m,false,sizeof(m));
    25         m[sx][sy].ex=true;
    26         m[sx][sy].step=0;
    27         queue<int> q;
    28         q.push(sx*1000+sy);
    29         int t=0;
    30         while(!q.empty()){
    31             int x=q.front()/1000;
    32             int y=q.front()%1000;
    33             q.pop();
    34             if(x==ex&&y==ey){
    35                 break;
    36             }
    37             int i=0;
    38             for(;i<8;i++){
    39                 int xx=x+d[i][0];
    40                 int yy=y+d[i][1];
    41                 if(check(xx,yy)){
    42                     continue;
    43                 }
    44                 q.push(xx*1000+yy);
    45                 m[xx][yy].ex=true;
    46                 m[xx][yy].step=m[x][y].step+1;
    47             }
    48         }
    49         cout<<m[ex][ey].step<<endl;            
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    ASP.NET MVC 3 新特性
    C#用WebClient下载File时操作超时的问题
    用C# 实现 Zen Cart 的用户密码加密算法
    ASP.NET MVC 局部缓存实现 用户控件缓存 Partial Output Caching
    关于MarshalByRefObject的解释
    文件的上传和下载
    浅谈三维GIS的应用之三维管线
    Python:处理不是经由EXPORT出來的Windows日志
    python :简单邮件发送
    日志文件分割:将包含不同关键字的行写入到不同的文件
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4280341.html
Copyright © 2011-2022 走看看