zoukankan      html  css  js  c++  java
  • POJ 3984

    bfs,用数组来构建队列,用front指针来连接每一步

    输出的时候注意(0, 0)逗号后又一个空格。。。。被坑了一次

    #include <iostream>
    using namespace std;
    struct step{
        int x,y;
        step *front;
        void init(int xx,int yy){
            x=xx;
            y=yy;
        }
    };
    int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
    int sq[5][5];
    step q[30];                            //相当于数组队列,用于记录每一步
    step bfs(){
        int i,nx,ny,x,y;
        step start;
        start.init(4,4);                        //从右下角开始走,终点为左上角
        start.front=NULL;
        q[0]=start;
        int j=0,l=1;
        while(j<=l){
            x=q[j].x;
            y=q[j].y;
            for(i=0;i<4;i++){
                nx=x+dir[i][0];
                ny=y+dir[i][1];
                if(nx<0||nx>4||ny<0||ny>4)continue;
                if(sq[ny][nx]==1)continue;
                sq[ny][nx]=1;
                step s;
                s.init(nx,ny);
                s.front=&q[j];                                    //用于记录前一步的指针
                if(s.x==0&&s.y==0)return s;
                q[l]=s;
                l++;
            }
            j++;
        }
        return start;
    }
    int main(){
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                cin>>sq[i][j];
            }
        }
        step *s;
        step b;
        b=bfs();
        s=&b;
        while(s->front!=NULL){                            //用指针来记录路径,
            cout<<"("<<s->y<<", "<<s->x<<")"<<endl;
            s=s->front;
        }
        cout<<"(4, 4)"<<endl;
        return 0;
    }

  • 相关阅读:
    html,web页面组成 --L简
    python,appium,初步 --L简
    UIautomatorviewer,Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!
    python,selenium,初步--L简
    python,入门 --L简
    python,os模块 --L简
    RobotFramework(4)接口自动化 --L简
    RobotFramework(3)web自动化 --L简
    RobotFramework(2)自定义关键字,定义,使用 --L简
    RobotFramework(1),初探 --L简
  • 原文地址:https://www.cnblogs.com/Mr-Xu-JH/p/3855016.html
Copyright © 2011-2022 走看看