POJ 2243:Knight Moves(BFS)

可以理解为象棋中的马走“日”字形,从第一个位置到第二个位置所需的最短步数,简单的BFS

每走一步需判断一次是否到达目标点。

由于BFS写得不多,一直用DFS的思维理解,递归写一直溢出、超时~~

#include"cstdio"
#include"iostream"
#include"cstring"
#include"queue"
using namespace std;
int dx[8]={-1,1,-2,2,-2,2,-1,1};
int dy[8]={-2,-2,-1,-1,1,1,2,2};
int vis[10][10];
struct point
{   int x;
    int y;
    int level;
};
bool judge(point p)
{   if(vis[p.x][p.y]||p.x>8||p.x<=0||p.y>8||p.y<=0) return false;
    return true;
}
int main()
{   char a1,b;
    int ans;
    point p1,p2;
    while(scanf("%c%d %c%d",&a1,&p1.y,&b,&p2.y)!=EOF)
    {   getchar();
        memset(vis,0,sizeof(vis));
        ans=0;
        p1.x=a1-‘a‘+1;p2.x=b-‘a‘+1;
        p1.level=0;
        queue<point> q;
        q.push(p1);
        vis[p1.x][p1.y]=1;
        point a;
        while(!q.empty())
        {
            a=q.front();
            q.pop();
            if(a.x==p2.x&&a.y==p2.y) {ans=a.level;break;}
            for(int i=0;i<8;i++)
            {   point temp;
                temp.x=a.x+dx[i];
                temp.y=a.y+dy[i];
                temp.level=a.level+1;
                if(judge(temp))
                {   vis[temp.x][temp.y]=1;
                    q.push(temp);
                }
            }
        }
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a1,p1.y,b,p2.y,ans);
    }
    return 0;
}

时间: 2024-10-05 05:50:53

POJ 2243:Knight Moves(BFS)的相关文章

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

poj 2243 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11794   Accepted: 6646 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 v

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 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 visit

poj 1915 Knight Moves (bfs搜索)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21919   Accepted: 10223 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

POJ 1915 Knight Moves

Knight Moves Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the mini

poj 1915 Knight Moves 【双向bfs】

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22121   Accepted: 10332 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9

UVa 439 Knight Moves(BFS应用)

题意  求国际象棋中骑士从一个位置移东到另一个位置所需最少步数 基础的BFS应用 #include <bits/stdc++.h> using namespace std; int x[] = { -2, -1, -2, -1, 1, 2, 1, 2}; int y[] = { -1, -2, 1, 2, -2, -1, 2, 1}; int d[15][15], sx, sy, ex, ey; pair<int, int> q[105], t; int bfs() { int c