zoukankan      html  css  js  c++  java
  • [POJ]1915 Knight Moves

    Knight Moves
    Time Limit: 1000MS Memory Limit: 30000K
    Total Submissions: 27202 Accepted: 12858
    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

    简单的bfs
    提交了四五次WA
    最后发现原因是棋盘从(0,0)开始的 。。
    直接把所有++的话n会出问题..

    发现了一种记录步数的新方法,在结构体里写step记录,不用在print里把pre遍历一边再计数。

    //Writer:GhostCai && His Yellow Duck
    
    #include<iostream>
    #include<cstring>
    #include<queue> 
    #define MAXN 4000
    using namespace std;
    
    int t,n,sx,sy,aimx,aimy;
    bool vis[MAXN][MAXN];
    
    struct point{
        int x,y,step;
    }node,r;
    
    int dx[8]={1,2,2,1,-1,-2,-2,-1};
    int dy[8]={2,1,-1,-2,-2,-1,1,2};
    point pre[MAXN][MAXN];
    bool flag;
    
    
    void bfs(int x,int y){
        if(x==aimx&&y==aimy){
            cout<<0<<endl;
            return;
        }
        vis[x][y]=1;
        node.x = 0;
        node.y = 0;
        node.step = 0;
        pre[x][y]=node;
        node.x = x;
        node.y = y;
        queue<point> Q;
        Q.push(node) ;
        while(!Q.empty() && !flag){
            r=Q.front() ;
            Q.pop() ;
            int nx,ny;
            for(int i=0;i<=7;i++){
                nx=r.x + dx[i];
                ny=r.y + dy[i];
                if(nx<0||nx>=n||ny<0||ny>=n) continue;
                if(vis[nx][ny]) continue;
                vis[nx][ny]=1;
                pre[nx][ny]=r;
                node.x = nx;
                node.y = ny;
                node.step = r.step + 1;
                Q.push(node);
                if(nx==aimx&&ny==aimy){
                    cout<<node.step<<endl ;
                    flag=1;
                } 
            }
        }
    }
    
    int main(){
        cin>>t;
        while(t--){
            memset(vis,0,sizeof(vis));
            flag=0;
            cin>>n;
            cin>>sx>>sy;
            cin>>aimx>>aimy;
    //      n++;
    //      sx++;
    //      sy++;
    //      aimx++;
    //      aimy++;
            bfs(sx,sy);
        }
        return 0;
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247546.html

  • 相关阅读:
    js一次性删除一个数组中多个元素
    js防抖,节流
    js 生成一个永不重复的ID
    mavon-editor 使用方法以及回显
    导出---后台返回二进制流文件数据,前端转换格式进行下载
    vue 监控enter键触发
    上传视频到阿里云
    前端图片压缩
    向后台传输表情时,手机自带输入法emoji表情的输入,提交及显示——前端解决方案
    vue 之this.$router.push、replace、go的区别
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247546.html
Copyright © 2011-2022 走看看