poj 1915 http://poj.org/problem?id=1915

/**<  */#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <ctype.h>
#define N 310

using namespace std;

int d[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
int vis[N][N], l, ex, ey;

struct node
{
    int x, y, step;
};

int BFS(int x, int y)
{
    queue<node>Q;
    int i;
    node now, next;
    now.x = x;
    now.y = y;
    now.step = 0;
    vis[now.x][now.y] = 1;
    Q.push(now);
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(now.x == ex && now.y == ey)
            return now.step;
        for(i = 0 ; i < 8 ; i++)
        {
            next.x = now.x + d[i][0];
            next.y = now.y + d[i][1];
            next.step = now.step + 1;
            if(next.x >= 0 && next.x < l && next.y >= 0 && next.y < l && !vis[next.x][next.y])
            {
                vis[next.x][next.y] = 1;
                Q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    int t, sx, sy;
    scanf("%d", &t);
    while(t--)
    {
        memset(vis, 0, sizeof(vis));
        scanf("%d", &l);
        scanf("%d%d", &sx, &sy);
        scanf("%d%d", &ex, &ey);
        if(sx == ex && sy == ey)
            printf("0\n");
        else
            printf("%d\n", BFS(sx, sy));
    }
    return 0;
}
时间: 2024-08-07 11:59:06

poj 1915 http://poj.org/problem?id=1915的相关文章

Roadblocks http://poj.org/problem?id=3255

Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shorte

poj 1651 http://poj.org/problem?id=1651

http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6188   Accepted: 3777 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. Dur

http://poj.org/problem?id=1330

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17818   Accepted: 9455 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ3278http://poj.org/problem?id=3278

http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<queue> #define max(a, b)(a > b ? a : b) #define N 100010 using namespace s

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

POJ 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that

poj 3320 Jessica&#39;s Reading Problem(尺取法)

Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The au