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. Can you beat him?

The Problem

Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.

For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.

Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers
{0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a
single line.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

Source

TUD Programming Contest 2001, Darmstadt, Germany

一道非常水的bfs搜索题,思路也很简单,和昨天做的那道 Catch That Cow 思想差不多,本来是很水的一道题,按照自己的思路,一下子就可以敲出代码了,然后测试运用,样例中第二组数据怎么运行都是17,检查了好半天,还是没找到错哪,然后自己又重新敲了一遍代码;再测试数据,样例过了,我提交又是wa,我又仔细检查了好几遍,都是没有找到错误,后来我看了一下别人的代码,我怀疑是我的数组开小了,我的queue数组也只开到1000,visit数组也用的是1000,把queue数组改成了100000终于ac了。。。开始一直wa我都以为是思路错误,但是思路非常的清晰啊,竟然在数组这里犯错误,不应该啊!!!以后不能再犯了,值得注意!!

这道题目的主要思路就是分8个方向进行搜索,搜索到了就输出,没搜索到就入队。

用数组模拟队列比用stl效率高一些;

下面是ac的代码:

#include <cstdio>
#include <cstring>
const int maxn=100000;//开始设立的是1000,导致一直wa啊
typedef struct Queue
{
    int x,y,count;
}Queue;
Queue que[maxn];//用数组模拟队列
bool visit[310][310];
int dir[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};//方向数组
int l,c,d;
void bfs(int x,int y)
{
    memset(visit,false,sizeof(visit));//初始化
    int front=0,rear=0;
    int fx,fy,i;
    que[rear].x=x;//入队
    que[rear].y=y;
    que[rear++].count=0;
    visit[x][y]=true;
    while(front<rear)//队不为空
    {
        Queue q=que[front++];
        if(q.x==c && q.y==d)//搜索到结果
        {
            printf("%d\n",q.count);
            break;
        }
        for(i=0;i<8;i++)
        {
            fx=q.x+dir[i][0];
            fy=q.y+dir[i][1];
            if(fx>=0 && fy >=0 && fx<l && fy <l && !visit[fx][fy])//限制条件,这里也要注意,很多人在这里wa的
            {
                visit[fx][fy]=true;//标记访问
                que[rear].x=fx;//入队
                que[rear].y=fy;
                que[rear++].count=q.count+1;
            }
        }
    }
}
int main()
{
    int t,a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(que,0,sizeof(que));
        scanf("%d",&l);
        scanf("%d%d",&a,&b);
        scanf("%d%d",&c,&d);
        bfs(a,b);
    }
    return 0;
}

思路比较简单,下面是我wa的代码,第二组数据通不过,我也找不到错误。。。

#include <cstdio>
#include <cstring>
typedef struct Queue
{
    int x,y;
    int count;
}Queue;
const int maxn=1000;
int dir[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
bool visit[maxn][maxn];
Queue queue[maxn];
int c,d,l;
void bfs(int x,int y)
{
    int front=0,rear=0;
    int i,fx,fy;
    queue[rear].x=x;
    queue[rear].y=y;
    queue[rear++].count=0;
    visit[x][y]=1;
    while(front<rear)
    {
        Queue q=queue[front++];
        if(q.x==c && q.y==d)
        {
            printf("%d\n",q.count);
            break;
        }
        for(i=0;i<8;i++)
        {
             fx=q.x+dir[i][0];
             fy=q.y+dir[i][1];
            if( fx>= 0 && fy>= 0 && fx <l && fy <l&& !visit[fx][fy])
            {
                visit[fx][fy]=1;
                queue[rear].x=fx;
                queue[rear].y=fy;
                queue[rear++].count=q.count+1;
            }
        }
    }
}
int main()
{
    int t;
    int a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(queue,0,sizeof(queue));
        //memset(visit,0,sizeof(visit));
        scanf("%d",&l);
        scanf("%d%d",&a,&b);
        scanf("%d%d",&c,&d);
        bfs(a,b);
    }
    return 0;
}

poj 1915 Knight Moves (bfs搜索)

时间: 2024-08-13 14:09:32

poj 1915 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 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

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,-

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

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

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

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