程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 1372 Knight Moves (簡單bfs,囧!)

hdu 1372 Knight Moves (簡單bfs,囧!)

編輯:C++入門知識

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved