zoukankan      html  css  js  c++  java
  • 广搜 广搜 poj 3984

    ***求最短路径,然后再输出路径,

    BFS+路径输出***

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cmath>
    
    using namespace std;
    typedef long long LL;
    #define oo 0x3f3f3f3f
    #define N 100
    
    int maps[10][10];
    int dir[4][2]= {{1,0}, {0,1}, {-1,0}, {0,-1}}, vis[N][N], step[N][N];
    
    struct node
    {
        int x, y, s;
    
    } a, b;
    
    int k;
    
    void BFS(int x, int y)
    {
        k=0;
        queue<node> q;
        a.x=x;
        a.y=y;
        a.s=0;
        q.push(a);
        vis[0][0]=1;
        step[0][0]=0;
        k++;
    
        while(!q.empty())
        {
            a=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                b.x=a.x+dir[i][0];
                b.y=a.y+dir[i][1];
                if(b.x>=0&&b.x<5&&b.y>=0&&b.y<5&&maps[b.x][b.y]==0&&!vis[b.x][b.y])
                {
                    b.s=a.s+1;
                    q.push(b);
                    step[b.x][b.y]=b.s;
                    vis[b.x][b.y]=1;
                }
            }
        }
    }
    
    void put(int x, int y)
    {
        if(x==0 && y==0)
        {
            printf("(%d, %d)
    ", x, y);
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if(nx>=0 && nx<5 && ny>=0 && ny<5 && step[x][y]==step[nx][ny]+1 && vis[nx][ny])
            {
                put(nx, ny);
                printf("(%d, %d)
    ", x, y);
            }
        }
    }
    
    int main()
    {
        for(int i=0; i<5; i++)
            for(int j=0; j<5; j++)
                scanf("%d", &maps[i][j]);
        memset(vis, 0, sizeof(vis));
        memset(step, 0, sizeof(step));
        BFS(0, 0);
        put(4, 4);
        return 0;
    }
  • 相关阅读:
    jquery 初篇
    python作用域和js作用域的比较
    javascript作用域
    第三篇、dom操作续
    dom事件
    第二篇 dom内容操作之value
    第三篇、变量
    第二篇、常量
    Node.js
    测试用例
  • 原文地址:https://www.cnblogs.com/9968jie/p/5666006.html
Copyright © 2011-2022 走看看