poj 2243 Knight Moves bfs

简介:

水题,裸的bfs,基本没有要注意的

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
const int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,1,2,-1};
int vis[9][9];
struct node
{
    int x,y;
};
queue<node> q;
char aim[3],stt[3];
node st,am,now,temp;
int i;
int main()
{
    while(~scanf("%s%s",stt,aim))
    {
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        st.x=stt[0]-'a';st.y=stt[1]-'1';
        am.x=aim[0]-'a';am.y=aim[1]-'1';
        vis[st.x][st.y]=1;
        q.push(st);
        while(!q.empty()&&!vis[am.x][am.y])
        {
            now=q.front();q.pop();
            for(i=0;i<8;i++)
            {
                temp.x=now.x+dir[i][0];
                temp.y=now.y+dir[i][1];
                if(temp.x<0||temp.x>7||temp.y<0||temp.y>7||vis[temp.x][temp.y])continue;
                vis[temp.x][temp.y]=vis[now.x][now.y]+1;
                q.push(temp);
            }
        }
        printf("To get from %s to %s takes %d knight moves.\n",stt,aim,vis[am.x][am.y]-1);
    }
}


 

目录
相关文章
|
6月前
Knight Moves(POJ2243)
Knight Moves(POJ2243)
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
59 0
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
UVA439-骑士的移动 Knight Moves(BFS)
UVA439-骑士的移动 Knight Moves(BFS)
UVA439-骑士的移动 Knight Moves(BFS)
|
测试技术
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
|
算法 Go
HDU-1548,A strange lift(Dijkstra)
HDU-1548,A strange lift(Dijkstra)
HDOJ/HDU 1372 Knight Moves(经典BFS)
HDOJ/HDU 1372 Knight Moves(经典BFS)
134 0
|
测试技术
Knight Moves(骑士移动) (bfs) POJ-2243
题目:Knight Moves (骑士移动)