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];

void bfs()
{
    int xx,yy;
    head=0;                                               //队头  下标为0的元素  第一个 head
    end=1;                                            // 下一次要放的元素
    q[end].pre=0;             //要放的元素 de 前一个结点 即、
    q[end].x=sx;
    q[end].y=sy;
    visited[sx][sy]=1;
    while (head<end)
    {
        head++;                                    // 开始往下边走                          head++
         for (int i=0;i<8;i++)
        {
            xx=q[head].x+dic[i][0];
            yy=q[head].y+dic[i][1];
            if (xx>=0&&xx<n&&yy>=0&&yy<n&&visited[xx][yy]==0)
            {
                end++;
                q[end].x=xx;
                q[end].y=yy;
                q[end].pre=head;
                visited[xx][yy]=1;
                if (xx==ex&&yy==ey) return;
            }
        }
    }
}

void getflo()
{
    int floor=0;
    while (q[end].pre!=0)    // 倒回去             因为 保留了路径
    {
        floor++;
        end=q[end].pre;
    }
    printf("%d\n",floor);
}

int main()
{
    int ncase;
    scanf("%d",&ncase);
    while (ncase--)
    {
        memset(visited,0,sizeof(visited));
        //memset(q,0,sizeof(q));
        scanf("%d",&n);
        scanf("%d %d",&sx,&sy);
        scanf("%d %d",&ex,&ey);
        if (sx==ex&&sy==ey)
        {
            printf("0\n");
            continue;
        }
        else
        {
            bfs();
            getflo();
        }
    }
    return 0;
}

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

时间: 2024-10-22 14:02:18

poj 1915 BFS 利用 pre 计算步数------------------路径的相关文章

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

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 1

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 1279 Art Gallery(利用极角计算半平面交)

题意:给出n个点的坐标描述一个多边形画廊.在画廊平面上找到一片表面,从该区域能够看到画廊墙壁上的每一个点: 思路:将这片表面称为多边形的核.核中一点与多边形边界上任意一点的连线都在多边形内部.凸多边形的核为其本身,凹多边形的核为其内部的一部分或不存在: 将多边形的n个顶点转化为n条边的直线方程:逆时针用多边形的边剖分多边形所在平面,保留向里的部分,舍去向外的部分,剩下的即为核: 利用叉积公式计算核面积,即为所求面积: #include<cstdio> #include<cstring&g

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

这一题主要用到了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