zoukankan      html  css  js  c++  java
  • E深搜

    <span style="color:#330099;">/*
    E - 广搜记录路径 基础
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit
     
    Status
    Description
    定义一个二维数组: 
    
    int maze[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,
    
    };
    
    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
    Input
    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
    Output
    左上角到右下角的最短路径,格式如样例所示。
    Sample Input
    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
    Sample Output
    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)
    By Grant Yuan
    2014.712
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    int a[6][6];
    int c[30];
    typedef struct{
     int x;
     int y;
     int f;
    }node;
    node s[30];
    bool flag[6][6];
    int top,base;
    int best;
    int next[4][2]={1,0,0,1,-1,0,0,-1};
    int res;
    bool can(int m,int n)
    {
        if(m>=0&&m<=4&&n>=0&&n<=4&&flag[m][n]==0&&a[m][n]==0)
           return 1;
        return 0;
    }
    
    void slove()
    {int m,n;int xx,yy;
        while(top>=base){
          if(s[base].x==4&&s[base].y==4)
            {   res=base;
               break;
            }
          xx=s[base].x;
          yy=s[base].y;
          for(int i=0;i<4;i++)
            {  m=xx+next[i][0];
               n=yy+next[i][1];
                if(can(m,n)){
                    s[++top].x=m;
                    s[top].y=n;
                    s[top].f=base;
                    flag[m][n]=1;
                }
            }
            base++;
        }
    
    }
    int main()
    {
        int i,j;
        memset(flag,0,sizeof(flag));
        for(i=0;i<5;i++)
          for(j=0;j<5;j++)
            cin>>a[i][j];
        top=-1;base=0;
        s[++top].x=0;
        s[top].y=0;
        s[top].f=0;
        flag[0][0]=1;
        slove();
        int t=1,v=-1;
        c[++v]=res;
        while(1){
            t=c[v];
            if(t)
               c[++v]=s[t].f;
            else break;
          }
        for(i=v;i>=0;i--)
           printf("(%d, %d)
    ",s[c[i]].x,s[c[i]].y);
           return 0;
    }
    </span>

  • 相关阅读:
    MATLAB2019a安装
    每日日报6
    HTML表格
    HTML常见表单元素
    HTML特殊符号
    CTF-WEB:攻防世界-bug(综合应用)
    CTF-WEB:文件上传和 webshell
    CTF-WEB:BurpSuite 工具应用
    CTF-WEB:后台扫描与备份泄露
    CTF-WEB:PHP 伪协议
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254527.html
Copyright © 2011-2022 走看看