zoukankan      html  css  js  c++  java
  • hdu 5335 Walk Out(bfs+寻找路径)

    Problem Description
    In an n∗m 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 (1≤n,m≤1000). 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
     

     1、如果mp[0][0]==0的话,先bfs一次,将前导0去掉。bfs得到了wx,wy

    2、然后就是找最小路径了。枚举步数for(int i=wx+wy;i<n+m-2;i++),在这个步数的基础上枚举所有可能的(x,y)x+y==step,如果遇到0则取0,一直找到(n-1,m-1)

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 #include<stdlib.h>
      6 using namespace std;
      7 #define N 1006
      8 int n,m;
      9 int mp[N][N];
     10 int vis[N][N];
     11 struct Node{
     12     int x,y;
     13 }st;
     14 int dirx[]={1,0,-1,0};
     15 int diry[]={0,1,0,-1};
     16 int wx,wy;
     17 void bfs(){
     18     queue<Node>q;
     19     q.push(st);
     20     Node t1,t2;
     21     while(!q.empty()){
     22         t1=q.front();
     23         q.pop();
     24         for(int i=0;i<4;i++){
     25             t2.x=t1.x+dirx[i];
     26             t2.y=t1.y+diry[i];
     27             if(vis[t2.x][t2.y]) continue;
     28             if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
     29             vis[t2.x][t2.y]=1;
     30             if(mp[t2.x][t2.y]==0){
     31                 q.push(t2);
     32             }
     33             if(wx+wy<t2.x+t2.y){
     34                 wx=t2.x;
     35                 wy=t2.y;
     36             }
     37         }
     38     }
     39 }
     40 int main()
     41 {
     42     int t;
     43     scanf("%d",&t);
     44     while(t--){
     45         scanf("%d%d",&n,&m);
     46         char s[1006];
     47         for(int i=0;i<n;i++){
     48             scanf("%s",s);
     49             for(int j=0;j<m;j++){
     50                 mp[i][j]=s[j]-'0';
     51             }
     52         }
     53         st.x=0;
     54         st.y=0;
     55         memset(vis,0,sizeof(vis));
     56         vis[0][0]=1;
     57         wx=0;
     58         wy=0;
     59         if(mp[0][0]==0){
     60             bfs();
     61         }
     62         if(mp[wx][wy]==0){
     63             printf("0
    ");
     64             continue;
     65         }
     66         printf("1");
     67         int nowflag=0;
     68         for(int i=wx+wy;i<n+m-2;i++){
     69             int flag=0;
     70             int step=i;
     71             for(int j=0;j<=step;j++){
     72                 int x=j;
     73                 int y=i-j;
     74                 if(x<0 || x>=n || y<0 || y>=m) continue;
     75                 if(nowflag && mp[x][y]) continue;
     76                 if(!vis[x][y]) continue;
     77                 
     78                 for(int k=0;k<2;k++){
     79                     int sx=x+dirx[k];
     80                     int sy=y+diry[k];
     81                     if(sx<0 || sx>=n || sy<0 || sy>=m) continue;
     82                     vis[sx][sy]=1;
     83                     if(mp[sx][sy]==0){
     84                         flag=1;
     85                     }
     86                 }
     87                 
     88             }
     89             nowflag=flag;
     90             if(flag){
     91                 printf("0");
     92             }
     93             else{
     94                 printf("1");
     95             }
     96         }
     97         printf("
    ");
     98     }
     99     return 0;
    100 }
    View Code
  • 相关阅读:
    嵌入优酷视频
    简单字符串处理
    随机生成一个大写字母
    生成二维码
    【计算机网络(谢希仁)-读书笔记】5.2 用户数据报协议UDP
    【计算机网络(谢希仁)-读书笔记】5.1 运输层协议概述
    【计算机网络(谢希仁)-读书笔记】4.6 IP多播
    【计算机网络(谢希仁)-读书笔记】4.5 因特网的路由选择协议
    【计算机网络(谢希仁)-读书笔记】4.4 网际控制报文协议ICMP
    【计算机网络(谢希仁)-读书笔记】4.3 划分子网和构造超网
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4800419.html
Copyright © 2011-2022 走看看