杭电 HOJ 1372 Knight Moves 解题报告

    马走棋盘,搜索。刚开始想打表,发现棋子在边界的时候很难处理,比如a1到b2的话2步是不行的。

    用队列,时效差一点点,46MS,代码如下:

#include<iostream>
#include<deque>
using namespace std;

struct Point
{
    char x,y,num;
};

int main()
{
    int a,b,i,j;
    Point x,y;
    deque<Point> q;
    char str[2],str2[2];
    char visit[8][8];
    while(cin>>str>>str2)
    {
        memset(visit,0,sizeof(visit));
        x.x=str[0]-'a';
        x.y=str[1]-'1';
        x.num=0;
        q.clear();
        q.push_back(x);
        visit[x.x][x.y]=1;
        a=str2[0]-'a';
        b=str2[1]-'1';

        while(!(x.x==a&&x.y==b))
        {
            y.num=x.num+1;
            if(x.x>=2 && x.y>=1 && !visit[y.x=x.x-2][y.y=x.y-1])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x<6 && x.y>=1 && !visit[y.x=x.x+2][y.y=x.y-1])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x<6 && x.y<7 && !visit[y.x=x.x+2][y.y=x.y+1])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x>=2 && x.y<7 && !visit[y.x=x.x-2][y.y=x.y+1])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x>=1 && x.y>=2 && !visit[y.x=x.x-1][y.y=x.y-2])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x>=1 && x.y<6 && !visit[y.x=x.x-1][y.y=x.y+2])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x<7 && x.y<6 && !visit[y.x=x.x+1][y.y=x.y+2])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }
            if(x.x<7 && x.y>=2 && !visit[y.x=x.x+1][y.y=x.y-2])
            {
                q.push_back(y);
                visit[y.x][y.y]=1;
            }

            q.pop_front();
            x=q.front();
        }
        cout<<"To get from "<<str<<" to "<<str2<<" takes "<<(int)x.num<<" knight moves."<<endl;
    }
}

    忽然发现自己曾经过了这题,代码是网上找的,也贴上吧~思路是一样的,循环部分比我的简洁。

#include<iostream>  
#include<cstdio>  
#include<cmath>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<iomanip>  
#include<string>  
#include<algorithm>  
#include<ctime>  
#include<stack>  
#include<queue>  
#include<vector>  
class Cordinate  
{public:int x,y;};  
using namespace std;  
int dir[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1},chess[10][10];  
bool legal(Cordinate po)  
{if(po.x>=1&&po.x<=8&&po.y>=1&&po.y<=8)return true;return false;}  
int main()  
{

    char a[3],b[3];Cordinate po,pos;int i;  
    while(cin>>a>>b)  
    {  
        queue<Cordinate>q;  
        memset(chess,-1,sizeof(chess));  
        pos.x=a[0]-'a'+1;pos.y=a[1]-'0';  
        chess[pos.x][pos.y]=0;  
        q.push(pos);  
        while(!q.empty())  
        {  
            po=q.front();q.pop();  
            for(i=0;i<8;i++)  
            {  
                pos.x=dir[i][0]+po.x;pos.y=dir[i][1]+po.y;  
                if(legal(pos)&&chess[pos.x][pos.y]==-1) {q.push(pos);chess[pos.x][pos.y]=chess[po.x][po.y]+1;}  
            }  
        }  
        cout<<"To get from "<<a<<" to "<<b<<" takes "<<chess[b[0]-'a'+1][b[1]-'0']<<" knight moves."<<endl;  
    }  
    return 0;  
}  

 

posted @ 2013-02-20 16:19  SF-_-  阅读(262)  评论(0编辑  收藏  举报