H - H

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 \le n, m \le 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 0instead).
 

Sample Input

2
2 2
11
11
3 3
001
111
101
 

Sample Output

111
101
给定一个由0和1组成的棋盘,从左上角走到右下角路径(上下左右四种行走方式)组成二进制数问最小的是什么 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
#define MM(a,b); memset(a,b,sizeof(a));
int n,m,step,res,ans[1000000+10],vis[1003][1003],cnt=0,a[1010][1010];
int dx[5]={0,1,0,-1};
int dy[5]={1,0,-1,0};
int flag[1000+1000+100];
char s[1003][1003]; struct node{
int x,y;
};
queue<node> q; bool legal(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m;
} void bfs()
{
q.push((node){1,1});
vis[1][1]=1;
while(q.size())
{
int x=q.front().x,y=q.front().y;q.pop();
for(int i=0;i<4;i++)
{
int tx=x+dx[i],ty=y+dy[i];
if(vis[tx][ty]||!legal(tx,ty)||s[tx][ty]=='1') continue;
vis[tx][ty]=1;
q.push((node){tx,ty});
if(tx+ty-1>res) {res=tx+ty-1;flag[res]=1;}
}
}
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
MM(flag,0);MM(vis,0); if(s[1][1]=='1') {res=0;flag[1]=0;q.push((node){1,1});}
else
{
res=1;
flag[1]=1;
bfs();
if(res==n+m-1) {printf("0\n");continue;}
for(int x=1;x<=n;x++)
{
int y=res+1-x;
if(vis[x][y]) q.push((node){x,y});
}
} while(q.size())
{
int ux=q.front().x,uy=q.front().y;q.pop();
if(flag[ux+uy-1]&&s[ux][uy]=='1') continue;
for(int i=0;i<2;i++)
{
int vx=ux+dx[i],vy=uy+dy[i];
if(!legal(vx,vy)||vis[vx][vy]) continue;
vis[vx][vy]=1;
if(s[vx][vy]=='1'&&!flag[vx+vy-1]) q.push((node){vx,vy});
if(s[vx][vy]=='0') {flag[vx+vy-1]=1;q.push((node{vx,vy}));}
}
} int i=1;
for(;i<=n+m-1;i++)
if(!flag[i]) break;
if(i>n+m-1) printf("0");
else for(;i<=n+m-1;i++)
printf("%d",!flag[i]);
printf("\n");
}
return 0;
}

  分析:比赛时看到这道题过的人比较少,以为很难,,其实后来看了下题解,再自己想了下,发现自己比赛时离做出这道题

已经很近了,自己想到了,应该做矩阵的斜对角线,主要目标是在这个上面搜,然后也想到只能往右下角走,但是忘了一种特殊

情况,就是如果s[1][1]=='0'的话,那么就不一定非得往右下角走,但是总之得先通过0达到离点(n,m)最靠近的对角线,然后再往右下角走

 
 

HDU 5335 Walk Out BFS 比较坑的更多相关文章

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

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

  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+寻找路径)

    Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...

  4. hdu 5335 Walk Out (搜索)

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

  5. hdu 5094 状压bfs+深坑

    http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...

  6. 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(多校)

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

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

    Walk Out                                                                         Time Limit: 2000/10 ...

  9. hdu 5335 Walk Out 搜索+贪心

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

随机推荐

  1. pip安装源

    目录 介绍 永久配置安装源 Windows MacOS.Linux 配置文件内容 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:htt ...

  2. GTA4 EFLC cheat code

    GTA4 EFLC cheat code 提示警告:您的图像设置接近或超出您的系统推荐资源限制,为了使游戏运行更加流畅推荐你降低你的图像设置. 在游戏目录新建名为 commandline的txt文本文 ...

  3. golang(10):web开发 & 连接数据库

    http编程 ) Go原生支持 http : import ("net/http") ) Go 的 http 服务性能和 nginx 比较接近 ) 几行代码就可以实现一个 web ...

  4. JavaScript内置排序方法sort实现排序操作

    var arr = [10,8,6,9,1,7,2,13,5,1,9]; //sort排序 arr.sort(function(a,b){ //可以改变数组本身的排序方法 return a-b; }) ...

  5. Express bodyParser中间件使用方式

    bodyParser中间件用来解析http请求体,是express默认使用的中间件之一. 1.这个模块提供以下解析器 (1) JSON body parser (2) Raw body parser ...

  6. 【Git的基本操作三】基本操作命令

    基本操作 (1) 状态查看操作 git status 作用:查看工作区.暂存区状态 (2) 添加操作 git add [filename] 作用:将工作区文件的 添加/修改,添加到暂存区 (3) 提交 ...

  7. OSCP-Kioptrix2014-3 后渗透测试

    拿到root权限 之前的努力,最终获得了两个session 尝试看看该操作系统的漏洞 kali: searchsploit freebsd 9.0 cp /usr/share/exploitdb/ex ...

  8. OGG学习笔记02

    实验环境:源端:192.168.1.30,Oracle 10.2.0.5 单实例目标端:192.168.1.31,Oracle 10.2.0.5 单实例 1.模拟源数据库业务持续运行 2.配置OGG前 ...

  9. greenlet、gevent:历史悠久的用于处理并发的模块

    greenlet介绍 greenlet是用C语言编写的一个模块,然后让python调用,目的就是为了让python支持协程. A "greenlet" is a small ind ...

  10. Winfrom UI 美化 MetroModernUI库应用实例

    使用方式: 选择项目==>右键==>管理NuGet安装包==>输入Metro==> ==>添加选项卡(自定义命名,例如Metrol UI)==>浏览 ==>加 ...