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

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;
}


 

8805943

2013-08-02 16:09:55

Accepted

1372

109MS

320K

1852 B

C++

 

判重了的。。。只是把数组开大了。。第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[100][100];
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;
}

//AC


 

8809964

2013-08-02 21:05:33

Accepted

1372

31MS

280K

1920 B

C++

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值