zoukankan      html  css  js  c++  java
  • 917:Knight Moves

    题目链接:http://noi.openjudge.cn/ch0205/917/

    原题应该是hdu 1372

    总时间限制: 1000ms  内存限制: 65536kB
    描述
    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.

    输入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

    来源TUD Programming Contest 2001, Darmstadt, Germany

    题目大意:

    输入n表示有一个n*n的棋盘,输入开始坐标和结束坐标,问一个骑士朝着棋盘的8个方向走马字步,从起点到终点最少需要多少步。

    首先输入T表示有T个测试样例,然后输入n表示棋盘规模,然后输出起点和终点的坐标(坐标从0开始到n-1)

    输出马移动的最少步数,起点和终点相同则输出0.

    算法分析:

    这个题明显用广搜效率比较高。深搜的话,要搜索完所有路径然后才知道最优解。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<queue>
     5 using namespace std;
     6 
     7 struct obj
     8 {
     9     int xx,yy,step;
    10 };
    11 
    12 int T,n;
    13 queue<struct obj> q;
    14 struct obj S,E;
    15 int used[303][303];
    16 
    17 int dx[8]={-2,-1,1,2,2,1,-1,-2};//???????????????????8???? 
    18 int dy[8]={1,2,2,1,-1,-2,-2,-1};
    19 void BFS();
    20 
    21 int main(int argc, char *argv[])
    22 {
    23     freopen("917.in","r",stdin);
    24     scanf("%d",&T);
    25     while(T)
    26     {
    27         T--;
    28         scanf("%d",&n);
    29         scanf("%d%d%d%d",&S.xx,&S.yy,&E.xx,&E.yy);
    30         S.step=0;
    31         E.step=-1;
    32         
    33         if(S.xx==E.xx&&S.yy==E.yy) printf("0
    ");
    34         else 
    35         {
    36             memset(used,0,sizeof(used));
    37             BFS();
    38             if(E.step==-1) printf("no way!
    ");
    39             else printf("%d
    ",E.step);
    40         }
    41     }
    42     return 0;
    43 }
    44 
    45 void BFS()
    46 {
    47     int i,txx,tyy;
    48     struct obj temp;
    49     
    50     while(!q.empty()) q.pop();
    51     
    52     used[S.xx][S.yy]=1;
    53     q.push(S);
    54     while(!q.empty())
    55     {
    56         for(i=0;i<8;i++)
    57         {
    58             txx=q.front().xx+dx[i];
    59             tyy=q.front().yy+dy[i];
    60             if(txx>=0&&txx<n&&tyy>=0&&tyy<n&&used[txx][tyy]==0)
    61             {
    62                 temp.xx=txx;
    63                 temp.yy=tyy;
    64                 temp.step=q.front().step+1;
    65                 q.push(temp);
    66                 used[txx][tyy]=1;
    67                 if(temp.xx==E.xx&&temp.yy==E.yy)
    68                 {
    69                     E.step=temp.step;
    70                     return;
    71                 }
    72             }
    73         }
    74         q.pop();
    75     }
    76 }
  • 相关阅读:
    HTML5 JS 实现浏览器全屏(F11的效果)
    SpringMVC学习笔记之二(SpringMVC高级参数绑定)
    二十三种设计模式总结
    系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密
    Mybatis学习笔记之二(动态mapper开发和spring-mybatis整合)
    Mybatis学习笔记之一(环境搭建和入门案例介绍)
    Java中clone方法的使用
    列举Java中常用的包、类和接口
    Spring中bean的注入方式
    [ SSH框架 ] Spring框架学习之三(AOP开发和注解的使用)
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7268331.html
Copyright © 2011-2022 走看看