zoukankan      html  css  js  c++  java
  • 输入一个5乘5的矩阵,输出从左上到右下的路径

    //例如

    //0 1 0 0 0

    //0 1 0 1 0
    //0 0 0 0 0
    //0 1 1 1 0
    //0 0 0 1 0
    #include<iostream>
    #include<queue>
    using namespace std;
    int map[5][5];
    int dx[4]= {1,-1,0,0};
    int dy[4]= {0,0,-1,1};
    int front=0,rear=1;
    struct node
    {
        int x,y,pre;
    } q[100];
    void print(int i)//递归输出过程
    {
        if(q[i].pre!=-1)
        {
            print(q[i].pre);
            cout<<"("<<q[i].x<<", "<<q[i].y<<")"<<endl;
        }
    }
    void bfs(int x1,int y1)//广搜
    {
        q[front].x=x1;
        q[front].y=y1;
        q[front].pre=-1;
        while(front<rear)//当队列不空
        {
            for(int i=0; i<4; i++)//搜索可达的路径
            {
                int a=dx[i]+q[front].x;
                int b=dy[i]+q[front].y;
                if(a<0||a>=5||b<0||b>=5||map[a][b])//是否在迷宫内,是否可行
                    continue;
                else
                {
                    map[a][b]=1; //走过的路做标记
                    q[rear].x=a;
                    q[rear].y=b;
                    q[rear].pre=front;
                    rear++; //入队
                }
                if(a==4&&b==4) print(front);
            }
            front++;//出队
        }
    }
    int main()
    {
        int i,j;
        for(i=0; i<5; i++)
            for(j=0; j<5; j++)
                cin>>map[i][j];
        cout<<"(0, 0)"<<endl;
        bfs(0,0);
        cout<<"(4, 4)"<<endl;
        return 0;
    }
  • 相关阅读:
    想要学习设计模式,你得先会看类图,一张图读懂UML
    UML类图中箭头的含义
    DDD学习
    Customize your build
    WaitAll vs WhenAll
    When does a C# Task actually start?
    UE4中多种颜色轮廓线的后期处理
    [UE4]武器碰撞
    动态材质实例(Dynamic Material Instance)
    卷积运算
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264929.html
Copyright © 2011-2022 走看看