当前位置:编程学习 > C/C++ >>

hdu 1372 Knight Moves (简单bfs,囧!)

Knight Moves
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4983    Accepted Submission(s): 3046

 

Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

 

Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

 

Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".

 

Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

Source
University of Ulm Local Contest 1996


Recommend
Eddy

题意:就是国际象棋么。告诉起点和终点,问最少几步可以跳过去。

分析:求最优解什么的一般选bfs啦。。但是这题判重的数组开小了,导致我一开始以为这题不能判重。。。囧。。。太弱了。。!!!!!!!

感想:

1、scanf跳过空格读字符。。根本不需想太多。直接scanf(“%c  %c",&c1,&c2);中间有空格哦。。。这样就可以了撒。

2、关于判重问题的思考。多么痛的领悟。。。T^T。。。。。判重数组到底开多大每次注意思考,避免再犯。。。。。

3、易做图,你怎么可以忘了a-'a'+1才等于1啊?!

4、开始数组开的vis[8][8],数组从0开始,就没有(8,8)这个坐标。而这题的坐标是从1—n处理的。自然第5组数据过不了啊。。

      只要开成vis[10][10]就够了。我下面那个判重的开成100就有点浪费空间了哈哈。。。。

5、以后记住判重数组要稍微开大一点!!!!!!!!!

 

代码:

没判重的。。。

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int dx[]={-1,-2,-2,-1,1,2,2,1};
int dy[]={-2,-1,1,2,2,1,-1,-2};
char s,e;
int ss,ee;
int vis[8][8];
struct node
{
    int x,y;
    int steps;
}tt;

bool in(node &a)
{
    if(a.x>=1&&a.x<=8&&a.y>=1&&a.y<=8)
        return true;
    else return false;
}

int bfs()
{
    tt.steps=0;
    queue<node> q;
    while(!q.empty())
        q.pop();
    q.push(tt);
    node cur,next;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur.y==e-'a'+1&&cur.x==ee)
        {
            //printf("test: %d\n",cur.steps);
            return cur.steps;
        }
        else
        {
            for(int i=0;i<8;i++)
            {
                next.x=cur.x+dx[i];
                next.y=cur.y+dy[i];
                if(/*!vis[next.x][next.y]&&*/in(next))
                {
                    if(next.x==ee&&next.y==e-'a'+1)
                        return cur.steps+1;
                    //vis[next.x][next.y]=1;
                    next.steps=cur.steps+1;
                    //printf("djw: %d %d %d\n",next.x,next.y,next.steps);
                    q.push(next);
                }
            }
        }
    }
    return 0;
}

int main()
{
    while(scanf("%c%d %c%d",&s,&ss,&e,&ee)!=EOF)
    {
        getchar();
        memset(vis,0,sizeof(vis));
        //printf("test: %c%d %c%d\n",s,ss,e,ee);
        tt.x=ss;
        tt.y=s-'a'+1;
        vis[tt.x][tt.y]=1;
        //printf("%d %d\n",tt.x,tt.y);
        //printf("test: %d %d\n",ee,e-'a'+1);
        if(s==e&&ss==ee) printf("To get from %c%d to %c%d takes %d knight moves.\n",s,ss,e,ee,0);
        else printf("To get from %c%d to %c%d takes %d knight moves.\n",s,ss,e,ee,bfs());
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,