• hdu5335(搜索)


    Walk Out

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1977    Accepted Submission(s): 373


    Problem Description
    In an nm maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it.

    An explorer gets lost in this grid. His position now is (1,1) , and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position (1,1) . Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.
     
    Input
    The first line of the input is a single integer T (T=10) , indicating the number of testcases.

    For each testcase, the first line contains two integers n and m (1n,m1000) . The i -th line of the next n lines contains one 01 string of length m , which represents i -th row of the maze.
     
    Output
    For each testcase, print the answer in binary system. Please eliminate all the preceding 0 unless the answer itself is 0 (in this case, print 0 instead).
     
    Sample Input
    2 2 2 11 11 3 3 001 111 101
     
    Sample Output
    111 101
     
    Author
    XJZX
     
    Source
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  5338 5337 5336 5334 5333 
    题目意思:
    给定一个n*m的图,图上的每个点上的值为0或1,让你找到一条路,所经过的01序列最小。
    从当前点来看,如果能走0肯定要走0,如果没有0才会走1,我们可以先找到能到的最远的0,然后从最远的0处,向下和向右两个方向搜索了,
    (斜行递推,从第一个行开始标记下一行要走的点,然后在下一行的时候检查那些点被标记了,说明这些点可以走,然后再标记下一行的,
    直到倒数第二行,由于下一行的点要么是0,要么是1,所以可以直接输出,即使下一行有多个点可以走,但是它都是0或者1)
    这样处理可以排除特殊情况
    ,(直接从两个方向搜,可能会漏掉往左走最终到达的这个路径上全是0的这种情况)。
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <queue>
    #define SIZE 1005
    #define maxn 2010
    using namespace std;
    int n,m;
    char Map[SIZE][SIZE];
    int  visit[SIZE][SIZE];
    int  d[SIZE][SIZE]; 
    int  VIS[maxn][maxn];
    int tot;
    int dir[4][2]={0,-1,0,1,-1,0,1,0};
    struct node
    {
        int x,y,cnt;
    };
    queue <node>  que;
    void init()
    {
        tot=1;
       memset(visit,0,sizeof(visit));
       memset(d,0,sizeof(d));
       memset(VIS,0,sizeof(VIS));
    }
    void dfs(int x,int y)
    {
        if(visit[x][y]==1)
            return ;
        visit[x][y]=1;
        if(Map[x][y]=='1')
            return ;
        d[x][y]=1;
        if(x+y>tot)
            tot=x+y;
        if(x>1)
            dfs(x-1,y);
        if(x<n)
            dfs(x+1,y);
        if(y>1)
            dfs(x,y-1);
        if(y<m)
            dfs(x,y+1);
    }
    void bfs()
    {
        if(Map[1][1]=='0')
        {
            node a,b,c;
            a.x=1; a.y=1; a.cnt=2;
            que.push(a);
            VIS[1][1]=1;
            d[1][1]=1;
            while(!que.empty())
            {
                 b=que.front();
                 que.pop();
                 if(b.cnt > tot)
                    tot=b.cnt;
                 for(int i=0;i<4;i++)
                 {
                     int next_x=b.x+dir[i][0];
                     int next_y=b.y+dir[i][1];
                     if( (1<=next_x) &&(next_x<=n) && (1<=next_y) && (next_y<=m) && VIS[next_x][next_y]==0 && Map[next_x][next_y]=='0')
                     {
                         c.x=next_x; c.y=next_y;
                         c.cnt=next_x+next_y;
                         que.push(c);
                         VIS[next_x][next_y]=1;
                         d[next_x][next_y]=1;
                     }
                 }
    
            }
        }
        else
            return ;
    
    }
    void solve()
    {
        if(tot==m+n)
        {
            printf("0
    ");
            return ;
        }
        if(tot==1)
        {
           tot=2; //代表起始点为(1,1)
           d[1][1]=1;
           printf("1");
        }
        for(int p=tot;p<n+m;p++)
        {
           int flag=1;
           for(int q=max(p-m,1);q<=min(p-1,n);q++)  //q代表行号
           {
               if(d[q][p-q])
               {
                   int x=(Map[q][p-q+1]-'0')?1:0;
                   int y=(Map[q+1][p-q]-'0')?1:0;
                   flag=min(flag,x);
                   flag=min(flag,y);
               }
           }
           for(int q=max(p-m,1);q<=min(p-1,n);q++)
           {
               if(d[q][p-q])
               {
                   int x=(Map[q][p-q+1]-'0')?1:0;
                   int y=(Map[q+1][p-q]-'0')?1:0;
                   if(x==flag)
                      d[q][p-q+1]=1;
                   if(y==flag)
                      d[q+1][p-q]=1;
               }
           }
           printf("%d",flag);
        }
         printf("
    ");
    }
    int main()
    {
        //freopen("test.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t --)
        {
            init();
            scanf("%d%d%*c",&n,&m);
           // printf("%d %d %d
    ",n,m,tot);
            for(int i = 1 ; i <= n ; i ++)
            {
                scanf("%s",&Map[i][1]);
            }
           // getchar();
            //dfs(1,1);
            bfs();
          //  printf("tot: %d
    ",tot);
            solve();
        }
        return 0;
    }
     
  • 相关阅读:
    recon-ng打开后显示No modules enabled/installed
    mysql的floor()报错注入方法详细分析
    视频合并时使用python批量修改文件名
    ThinkPHP5框架引入的css等外部资源文件没有生效
    用session实现的用户登陆,客户端是怎样获取到cookie信息的
    关于Linux系统下基于Tomcat部署和升级war包的详细过程
    nginx代理的页面性能优化大全
    linux下MySQL数据库的定时备份与定时删除
    关于linux查询内存,CPU,存储空间和日志查询的的常用命令及参数讲解
    linux 查看cpu核心数
  • 原文地址:https://www.cnblogs.com/xianbin7/p/4692701.html
Copyright © 2020-2023  润新知