超超超简单的bfs——POJ-1915

Knight Moves

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26102   Accepted: 12305

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

下棋,从一个坐标到另一个坐标最少几步,第一个输入T个样例第一行一个数L表示棋盘大小然后两行两个坐标
 1 #include<stdio.h>
 2 #include<queue>
 3 #include<string.h>
 4 using namespace std;
 5 int p[333][333];
 6 int main()
 7 {
 8     int T;
 9     scanf("%d", &T);
10     while (T--)
11     {
12         memset(p, -1, sizeof(p));
13         queue<int>q;
14         int L;
15         scanf("%d", &L);
16         int a, b, x, y;
17         scanf("%d%d%d%d", &a, &b, &x, &y);
18         p[a][b] = 0;
19         q.push(a * 1000 + b);//一个坐标转为一个数——前三位是横坐标后三位是纵坐标
20         while (!q.empty())
21         {
22             int t = q.front();
23             q.pop();
24             a = t / 1000;
25             b = t % 1000;
26             if (a == x&&b == y)
27             {
28                 printf("%d\n", p[a][b]);
29                 break;
30             }
31             if (a - 1 >= 0 && b + 2 < L&&p[a - 1][b + 2] == -1)
32             {
33                 q.push((a - 1) * 1000 + (b + 2));
34                 p[a - 1][b + 2] = p[a][b] + 1;
35             }//
36             if (a + 1 < L && b + 2 < L&&p[a + 1][b + 2] == -1)
37             {
38                 q.push((a + 1) * 1000 + (b + 2));
39                 p[a + 1][b + 2] = p[a][b] + 1;
40             }//
41             if (a - 2 >= 0 && b + 1 < L&&p[a - 2][b + 1] == -1)
42             {
43                 q.push((a - 2) * 1000 + (b + 1));
44                 p[a - 2][b + 1] = p[a][b] + 1;
45             }//
46             if (a - 2 >= 0 && b - 1 >=0&&p[a - 2][b - 1] == -1)
47             {
48                 q.push((a - 2) * 1000 + (b - 1));
49                 p[a - 2][b - 1] = p[a][b] + 1;
50             }//
51             if (a + 2 <L && b + 1 < L&&p[a + 2][b + 1] == -1)
52             {
53                 q.push((a + 2) * 1000 + (b + 1));
54                 p[a + 2][b + 1] = p[a][b] + 1;
55             }//
56             if (a + 2<L && b - 1 >=0&&p[a + 2][b - 1] == -1)
57             {
58                 q.push((a + 2) * 1000 + (b - 1));
59                 p[a + 2][b - 1] = p[a][b] + 1;
60             }//
61             if (a - 1 >= 0 && b - 2 >=0&&p[a - 1][b - 2] == -1)
62             {
63                 q.push((a - 1) * 1000 + (b - 2));
64                 p[a - 1][b - 2] = p[a][b] + 1;
65             }//
66             if (a + 1<L && b - 2>=0&&p[a + 1][b - 2] == -1)
67             {
68                 q.push((a + 1) * 1000 + (b - 2));
69                 p[a + 1][b - 2] = p[a][b] + 1;
70             }//八个可能的位置
71         }
72     }
73 }
时间: 2024-11-05 06:28:08

超超超简单的bfs——POJ-1915的相关文章

超超超简单的bfs——POJ-3278

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 89836   Accepted: 28175 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

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

[WinForm] VS2010发布、打包安装程序(超全超详细)

1. 在vs2010 选择"新建项目"→" 其他项目类型"→" Visual Studio Installer→"安装项目": 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1."应用程序文件夹"表示要安装的应用程序需要添加的文件: 2."用户的'程序'菜单"表示:应用程序安装完,用户的"开始菜单"中的显示的内容,一般在这个文件夹中,需要再创建一个文件用来存放

超全超详细的HTTP状态码大全

超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息. 400-499 用于指出客户端的错误. 500-599 用于支持服务器错误. 一些常见的状态代码为: 200 – 服务器成功返回网页 404 – 请求的网页不存在 503 – 服务器暂时不可用 以下提供了 HTTP 状

CSS3实现的超酷超炫的立体开关效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

poj 1915 双向 BFS 利用数组 a[x][y] = a[cp.x][cp.y] + 1; b[x][y] = b[cp.x][cp.y] + 1;保留步数

#include<iostream>#include<queue> using namespace std; struct point{    int x, y;};point bufa[8] ={    {-2, 1}, {-1, 2}, {1, 2}, {2, 1},    {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; int n, a[305][305], b[305][305]; int rule(int x,int y)//判断是否符合棋盘

poj 1915 BFS 利用 pre 计算步数------------------路径

// BFS #include <stdio.h> #include <string.h> int visited[301][301]; // visited 已经访问过了 int dic[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int head,end,n,ex,ey,sx,sy; struct quene { int x,y,pre; // pre 前一个结点 }q[100000]; vo

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

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

Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21362 Accepted: 9926 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