Walk Out

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

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5342 5341 5340 5339 5338

本题思路:

1.先判断第一个点是不是0,如果是0先把所有的0都走一遍,找到哈曼顿距离最小的点(可能会有几个)。

这个过程可以用DFS也可以BFS(建议BFS,因为DFS会爆栈,必须自己把栈开导最大,后面会说明)

2.如果第一点不是0,直接从第一个点开始搜,只搜下和右两个方向,如果这两个方向有两个0,输出0,把两个0都加入队列;如果只有一个0,输出0,只把0那个点加入队列;如果是两个1,也把两个点都加入队列。

3.如果第一个点是0,再把这个0的右边的点和下边的点(超边界的不算)都加入队列开始用2的方法搜。

开始用DFS搜

 #pragma comment(linker, "/STACK:10240000000000,10240000000000")//这行代码不加就会STACK_OVERFLOW
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234
struct point
{
int x,y,d;
}st[N*]; int dx[]={,,-,};
int dy[]={,,,-};
int n,m,k,dis,flag;
char mat[N][N];
bool vis[N][N]; void dfs(int x,int y)
{
if(x<||x>n||y<||y>m)return;
if(mat[x][y]=='')return;
if(vis[x][y]==)return;
vis[x][y]=;
if(dis<x+y)
{
k=;
dis=x+y;
st[k].x=x;
st[k++].y=y;
}
else if(dis==x+y)
{
st[k].x=x;
st[k++].y=y;
}
for(int i=;i<;i++)
dfs(x+dx[i],y+dy[i]);
}
void bfs()
{
memset(vis,,sizeof(vis));
queue<point>q1;
queue<point>q2;
for(int i=;i<k;i++)
{
if(mat[st[i].x][st[i].y]=='')
{
if(st[i].x==n&&st[i].y==m){printf("");return;}
point a1=st[i],a2=st[i];
a1.x++;a2.y++;
if(a1.x<=n)
q1.push(a1),vis[a1.x][a1.y]=;
if(a2.y<=m)
q1.push(a2),vis[a2.x][a2.y]=;
}
else
q1.push(st[i]),vis[st[i].x][st[i].y]=;
}
printf("");
if(vis[n][m])return;
while()
{
flag=;
while(!q1.empty())
{
point cur=q1.front();
q1.pop();
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i],next.y+=dy[i];
if(vis[next.x][next.y] || next.x< || next.x>n || next.y< || next.y>m)continue;
if(mat[next.x][next.y] == '')
flag = ;
q2.push(next);
vis[next.x][next.y]=;
}
}
printf("%d",flag);
if(vis[n][m])return; while(!q2.empty())
{
point cur=q2.front();
q2.pop();
if(flag==)
q1.push(cur);
else if(flag== && mat[cur.x][cur.y]=='')
q1.push(cur);
}
}
} int main()
{
int t;cin>>t;
while(t--)
{
memset(vis,,sizeof(vis));
dis=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",mat[i]+); if(mat[][]=='')
st[].x=,st[].y=,k=;
else
dfs(,);
bfs();
cout<<endl;
}
return ;
} //几组比较好的数据 /*
5
2 2
01
11
2 2
00
11
2 2
00
00
3 3
000
110
110
3 3
000
110
111 */

开始用BFS搜(推荐)

 #include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234
struct point
{
int x,y;
}st[N*];
int dx[]={,,-,};
int dy[]={,,,-};
int n,m,k,dis;
char mat[N][N];
bool vis[N][N]; void bfs1()
{
memset(vis,,sizeof(vis));
queue<point>q;
point first;
first.x=first.y=;
q.push(first);vis[][]=;
st[].x=st[].y=;
k=;
while(!q.empty())
{
point cur=q.front();
q.pop();
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i],next.y+=dy[i];
if(next.x<||next.x>n||next.y<||next.y>m)continue;
if(vis[next.x][next.y] || mat[next.x][next.y]=='')continue;
q.push(next);vis[next.x][next.y]=;
if(dis<next.x+next.y)
{
k=;
dis=next.x+next.y;
st[k].x=next.x;
st[k++].y=next.y;
}
else if(dis==next.x+next.y)
{
st[k].x=next.x;
st[k++].y=next.y;
}
}
}
}
void bfs()
{
memset(vis,,sizeof(vis));
queue<point>q1;
queue<point>q2;
for(int i=;i<k;i++)
{
if(mat[st[i].x][st[i].y]=='')
{
if(st[i].x==n&&st[i].y==m){printf("");return;}
point a1=st[i],a2=st[i];
a1.x++;a2.y++;
if(a1.x<=n)
q1.push(a1),vis[a1.x][a1.y]=;
if(a2.y<=m)
q1.push(a2),vis[a2.x][a2.y]=;
}
else
q1.push(st[i]),vis[st[i].x][st[i].y]=;
}
printf("");
if(vis[n][m])return;
while()
{
int flag=;
while(!q1.empty())
{
point cur=q1.front();
q1.pop();
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i],next.y+=dy[i];
if(vis[next.x][next.y] || next.x< || next.x>n || next.y< || next.y>m)continue;
if(mat[next.x][next.y] == '')
flag = ;
q2.push(next);
vis[next.x][next.y]=;
}
}
printf("%d",flag);
if(vis[n][m])return; while(!q2.empty())
{
point cur=q2.front();
q2.pop();
if(flag==)
q1.push(cur);
else if(flag== && mat[cur.x][cur.y]=='')
q1.push(cur);
}
}
} int main()
{
int t;cin>>t;
while(t--)
{
dis=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",mat[i]+); if(mat[][]=='')
st[].x=,st[].y=,k=;
else
bfs1();
bfs();
cout<<endl;
}
return ;
}

其他:

1输图的时候不要%c输入,用%s输入,速度会快很多,这题如果用%c输入会TLE,(花了一下午时间找为什么TLE,最后发现居然是因为输图方式。) 所以以后都要用:

for(int i=;i<n;i++)
scanf("%s",mat[i];
or
for(int i=;i<=n;i++)
scanf("%s",mat[i]+);

2 dfs是很容易爆栈的,这题就是我开始写的用dfs的就爆栈了,这时候有一个处理办法:在代码最前面加:#pragma comment(linker, "/STACK:10240000000000,10240000000000") 这句意思是自己开一个非常大的栈,STACK:后面那数字好像已经是能开的最大的了。
此题中加入这一行本来的Runtime Error(STACK_OVERFLOW)就会变成 Accepted!

但是好像正规比赛不允许使用这种方式。

      

HDU 5335 Walk Out(多校)的更多相关文章

  1. hdu 5335 Walk Out (搜索)

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

  2. HDU 5335 Walk Out BFS 比较坑

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

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

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

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

    Walk Out                                                                         Time Limit: 2000/10 ...

  5. hdu 5335 Walk Out 搜索+贪心

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

  6. 2015 Multi-University Training Contest 4 hdu 5335 Walk Out

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

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

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

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

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

  9. HDU 5335 Walk Out

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

随机推荐

  1. seajs模块化加载框架使用

    seajs是模块化加载框架.seajs.org已经打不开了,seajs的github.seajs速查文档 <!--如果完成下面4步,则seajs掌握了80%js模块化1.引入seajs的库 :& ...

  2. Python第三方库之openpyxl(6)

    Python第三方库之openpyxl(6) 折线图 折线图允许在固定轴上绘制数据,它们类似于散列图,主要的区别在于,在折线图中,每个数据序列都是根据相同的值绘制的,不同的轴可以用于辅助轴,与条形图类 ...

  3. Flutter 发布APK时,release版本和debug版本的默认权限不同

    Flutter 发布APK时,release版本和debug版本的默认权限不同 @author ixenos 在调试模式下,默认情况下启用服务扩展和多个权限(在flutter中) 当您处于发布模式时, ...

  4. hdu2087

    #include <stdio.h> #include <string.h> int main(){ int cnt,i,j,k; +],tmp[+]; int strl,tm ...

  5. 【转】Python + Android + Uiautomator自动化测试

    1.首先来介绍下UIAutomator工具 UIAutomator是Android官方推出的安卓应用界面自动化测试工具,是最理想的针对APK进行自动化功能回归测试的利器. 2.UIAutomator测 ...

  6. hibernate的cascade问题

    cascade属性的可能值有 all: 所有情况下均进行关联操作,即save-update和delete. none: 所有情况下均不进行关联操作.这是默认值. save-update: 在执行sav ...

  7. mybatis配置报错(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)

    如下报错:解决方案:要按照提示的顺序添加属性,(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrappe ...

  8. vi 和vim 的区别以及用法

    具体用法参考:http://blog.csdn.net/xuesnowce/article/details/53117352 它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...

  9. Day 3 网络基础

    网络基础 一.什么是互联网协议及为何要有互联网协议 ? 互联网协议:指的就是一系列统一的标准,这些标准称之为互联网协议.互联网的本质就是一系列的协议,总称为‘互联网协议’(Internet Proto ...

  10. vba功能语句

    VBA语句集(第1辑) 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏 ...