poj 1915 BFS


 1 /*
2 题意:国际象棋起点到终点移动的最少步数
3
4 题解:水题,BFS就过了,有人用双向BFS
5 */
6 #include <cstdio>
7 #include <cstring>
8 #include <queue>
9
10 using namespace std;
11
12 int dir[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
13
14 bool gra[305][305];
15
16 struct node
17 {
18 int x,y,step;
19 bool operator==(const node & t)const
20 {
21 if (x == t.x && y == t.y)
22 return true;
23 else
24 return false;
25 }
26 }Q[90005];
27 int front,tail;
28 int bfs(int l, node start, node end)
29 {
30 front = tail = 0;
31 Q[tail++] = start;
32 gra[start.x][start.y] = true;
33 while (front < tail)
34 {
35 node now = Q[front++];
36 if (now == end)
37 return now.step;
38 for(int i=0; i<8; i++)
39 {
40 int nx = now.x + dir[i][0];
41 int ny = now.y + dir[i][1];
42 if (0 <= nx && nx < l && 0 <= ny && ny < l && !gra[nx][ny])
43 {
44 node tmp = now;
45 tmp.step = now.step + 1;
46 tmp.x = nx;
47 tmp.y = ny;
48 Q[tail++] = tmp;
49 gra[nx][ny] = true;
50 }
51 }
52 }
53 }
54
55 int main(void)
56 {
57 int t;
58 scanf("%d",&t);
59 while (t--)
60 {
61 int l;
62 scanf("%d",&l);
63 node start;
64 node end;
65 scanf("%d%d%d%d",&start.x,&start.y,&end.x,&end.y);
66 start.step = 0;
67 memset(gra,false,sizeof(gra));
68 printf("%d\n",bfs(l,start,end));
69 }
70 return 0;
71 }

双向BFS实现:

  1 /*
2 题意:国际象棋起点到终点移动的最少步数
3
4 题解:参考网上其它人的代码写了个双向bfs,也许这个双向用得不太好,没快多少
5 */
6 #include <cstdio>
7 #include <cstring>
8 #include <queue>
9
10 #define FOR 2
11 #define BACK 3
12
13 using namespace std;
14
15 int dir[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
16
17 int gra[305][305];
18 int steps[305][305];
19
20 struct node
21 {
22 int x,y;
23 bool operator==(const node & t)const
24 {
25 if (x == t.x && y == t.y)
26 return true;
27 else
28 return false;
29 }
30 }fQ[45005],bQ[45005];
31 int ffront,ftail,bfront,btail;
32 int bi_bfs(int l, node start, node end)
33 {
34 if (start == end)
35 return 0;
36 ffront = ftail = bfront = btail = 0;
37 fQ[ftail++] = start;
38 bQ[btail++] = end;
39 gra[start.x][start.y] = FOR;
40 gra[end.x][end.y] = BACK;
41 while (ffront < ftail && bfront < btail)
42 {
43 if (ftail - ffront <= btail - bfront)
44 {
45 node now = fQ[ffront++];
46 for(int i=0; i<8; i++)
47 {
48 int nx = now.x + dir[i][0];
49 int ny = now.y + dir[i][1];
50 if (0 <= nx && nx < l && 0 <= ny && ny < l)
51 {
52 if (!gra[nx][ny])
53 {
54 node tmp;
55 tmp.x = nx;
56 tmp.y = ny;
57 steps[nx][ny] = steps[now.x][now.y] + 1;
58 fQ[ftail++] = tmp;
59 gra[nx][ny] = FOR;
60 }
61 else if (gra[nx][ny] == BACK)
62 return steps[nx][ny] + steps[now.x][now.y] + 1;
63 }
64 }
65 }
66 else
67 {
68 node now = bQ[bfront++];
69 for(int i=0; i<8; i++)
70 {
71 int nx = now.x + dir[i][0];
72 int ny = now.y + dir[i][1];
73 if (0 <= nx && nx < l && 0 <= ny && ny < l)
74 {
75 if (!gra[nx][ny])
76 {
77 node tmp;
78 tmp.x = nx;
79 tmp.y = ny;
80 steps[nx][ny] = steps[now.x][now.y] + 1;
81 bQ[btail++] = tmp;
82 gra[nx][ny] = BACK;
83 }
84 else if (gra[nx][ny] == FOR)
85 return steps[nx][ny] + steps[now.x][now.y] + 1;
86 }
87 }
88 }
89 }
90 }
91
92 int main(void)
93 {
94 int t;
95 scanf("%d",&t);
96 while (t--)
97 {
98 int l;
99 scanf("%d",&l);
100 node start;
101 node end;
102 scanf("%d%d%d%d",&start.x,&start.y,&end.x,&end.y);
103 memset(steps,0,sizeof(steps));
104 memset(gra,0,sizeof(gra));
105 printf("%d\n",bi_bfs(l,start,end));
106 }
107 return 0;
108 }

时间: 2024-10-20 13:06:22

poj 1915 BFS的相关文章

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 双向 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 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: 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

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

POJ 1915

这一题主要用到了BFS广度优先算法 若马的当前位置为(x,y),那么下一步就有8种可能. (x+2 , y+1) , (x+1 , y+2 ) , (x-1 , y+2) , (x-2 , y+1) (x+2 , y -1) , (x+1 ,  y-2 ) , (x-1 , y-2) , (x-2 , y-1) 这8种可能一个一个地去尝试,尝试过的用visit二维数组标记(小心数组越界),直到找到为止. #include <iostream>#include <string.h>u

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列

//BFS #include <iostream> #include <queue> using namespace std; bool used[8][8]; int move[8][2]={1,2, -1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1}; struct position { int i,j; int step; position(int a,int b,int c) { i=a; j=b; step=c; } }; int mai