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  or a  written on it.

An explorer gets lost in this grid. His position now is (,), 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=), indicating the number of testcases. 

For each testcase, the first line contains two integers n and m (≤n,m≤). The i-th line of the next n lines contains one  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  unless the answer itself is  (in this case, print  instead).
 
Sample Input
 


Sample Output

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)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stdlib.h>
using namespace std;
#define N 1006
int n,m;
int mp[N][N];
int vis[N][N];
struct Node{
int x,y;
}st;
int dirx[]={,,-,};
int diry[]={,,,-};
int wx,wy;
void bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(vis[t2.x][t2.y]) continue;
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
vis[t2.x][t2.y]=;
if(mp[t2.x][t2.y]==){
q.push(t2);
}
if(wx+wy<t2.x+t2.y){
wx=t2.x;
wy=t2.y;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
char s[];
for(int i=;i<n;i++){
scanf("%s",s);
for(int j=;j<m;j++){
mp[i][j]=s[j]-'';
}
}
st.x=;
st.y=;
memset(vis,,sizeof(vis));
vis[][]=;
wx=;
wy=;
if(mp[][]==){
bfs();
}
if(mp[wx][wy]==){
printf("0\n");
continue;
}
printf("");
int nowflag=;
for(int i=wx+wy;i<n+m-;i++){
int flag=;
int step=i;
for(int j=;j<=step;j++){
int x=j;
int y=i-j;
if(x< || x>=n || y< || y>=m) continue;
if(nowflag && mp[x][y]) continue;
if(!vis[x][y]) continue; for(int k=;k<;k++){
int sx=x+dirx[k];
int sy=y+diry[k];
if(sx< || sx>=n || sy< || sy>=m) continue;
vis[sx][sy]=;
if(mp[sx][sy]==){
flag=;
}
} }
nowflag=flag;
if(flag){
printf("");
}
else{
printf("");
}
}
printf("\n");
}
return ;
}

hdu 5335 Walk Out(bfs+寻找路径)的更多相关文章

  1. HDU 5335 Walk Out BFS 比较坑

    H - H Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4

    题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...

  3. HDU 5335 Walk Out (BFS,技巧)

    题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...

  4. hdu 5335 Walk Out (搜索)

    题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...

  5. hdu 5335 Walk Out (2015 Multi-University Training Contest 4)

    Walk Out                                                                         Time Limit: 2000/10 ...

  6. hdu 5335 Walk Out 搜索+贪心

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  7. HDU 5335 Walk Out

    题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...

  8. HDU 5335——Walk Out——————【贪心】

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  9. HDU 5335 Walk Out(多校)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

随机推荐

  1. Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询

    HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多.条件查询.分页查询.连接查询.嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了 ...

  2. python3-day5(模块)

    1.获取路径import os,sys #获取全部路径 print(os.path.abspath(__file__)) #获取目录 print(os.path.dirname(os.path.abs ...

  3. HDU 4907 Task schedule

    对于询问q 假设q不存在直接输出q 否则输出后面第一个不存在的数 从2*10^5到1遍历一边ac #include<bits/stdc++.h> using namespace std; ...

  4. std::remove_if

    原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...

  5. mysql主从同步报错

    主从不同步,经查看发现如下报错 Last_Errno: 1666 ​Last_Error: Error executing row event: 'Cannot execute statement: ...

  6. image即时上传

    function preview_upload(input, img_div_id){ var img_id = img_div_id.replace("itempic_previewDiv ...

  7. C# 创建验证码图片

    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...

  8. Json部分知识(前台显示格式、Json-lib日期处理)

    1,Json格式用于datagrid数据显示 easyui前台显示数据可以使用JSONObject,也可以使用JSONArray.但是如果需要在datagrid表格中进行数据显示,只能使用JSONOb ...

  9. CCFileUtils::getFileData疑惑

    背景 这几天在使用cocos2d-x读取磁盘文件的时候,发现了CCFileUtils中一点不合理的地方,特此记录,以供研讨. 项目结构 ①我使用的是cocos2d-x 2.1.3版本,CCFileUt ...

  10. 一句SQL实现MYSQL的递归查询

    众所周知,目前的mysql版本中并不支持直接的递归查询,但是通过递归到迭代转化的思路,还是可以在一句SQL内实现树的递归查询的.这个得益于Mysql允许在SQL语句内使用@变量.以下是示例代码. 创建 ...