uva 439

3 篇文章 0 订阅

//水题 但是多次输入清楚queue元素误用if(!queue.empty()),应为while,浪费很多时间,注意

#include<iostream>

#include<queue>

#include<algorithm>

#include<map>

#include<string.h>

#include<math.h>

#include<stdio.h>

#include <iomanip>

#include <stack>

#include <vector>

#include <sstream>

using namespace std;


typedef pair<int,int>p;

queue<p>que;


int chessboard[8][8];

string start,ending;

int sx,sy;

int ex,ey;


int dx[] = { -1, -1, 1, 1, -2, -2, 2, 2 };

int dy[] = { 2, -2, 2, -2, 1, -1, 1, -1 };

void solve()

{

    int x,y;

    int i,j;

    chessboard[sx][sy]=0;

    while(!que.empty())

    {

        p temp = que.front();

        que.pop();

        if(temp.first==ex && temp.second==ey)

            break;

        for(i=0;i<8;i++)

        {

            x=temp.first+dx[i];

            y =temp.second+dy[i];

            if(0<=x && x<=7 && 0<=y && y<=7)

            {

                que.push(p(x,y));

                chessboard[x][y] = chessboard[temp.first][temp.second]+1;

            }

        }

    }

    cout<<"To get from "<<start<<" to "<<ending<<" takes "<<chessboard[ex][ey]<<" knight moves."<<endl;


}

int main()

{

    while(cin>>start>>ending)

    {

        //input

        while(!que.empty())

            que.pop();

        memset(chessboard,0,sizeof(chessboard));

        sx = start[0]-'a';

        sy = start[1]-'1';

        ex = ending[0]-'a';

        ey = ending[1]-'1';

        que.push(p(sx,sy));

        solve();

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值