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[][] = {{-, -}, {-, }, {-, -}, {-, }, {, -}, {, }, {, -}, {, }};
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 = ;
vis[now.x][now.y] = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(now.x == ex && now.y == ey)
return now.step;
for(i = ; i < ; i++)
{
next.x = now.x + d[i][];
next.y = now.y + d[i][];
next.step = now.step + ;
if(next.x >= && next.x < l && next.y >= && next.y < l && !vis[next.x][next.y])
{
vis[next.x][next.y] = ;
Q.push(next);
}
}
}
return -;
} int main()
{
int t, sx, sy;
scanf("%d", &t);
while(t--)
{
memset(vis, , 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 ;
}
上一篇:CentOS6.5 安装mysql5.6.30


下一篇:POJ 题目3661 Running(区间DP)