uva 439

来源:互联网 发布:战队图标制作软件 编辑:程序博客网 时间:2024/05/15 06:39

//水题 但是多次输入清楚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 namespacestd;


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();

    }

}


0 0
原创粉丝点击