Walk Out

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

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).

首先很容易想到位数越少越小,所以说肯定选择向下或者向右的走向到终点(即最短路径为忧)

其次如果一开始(1,1)为0的话,如果有一段连续的0路径,可以选择先绕到离终点最近的0,这样前面全是前导0,对答案没有影响

所以说策略是先找到一段连续的0距终点最近,然后再在每层寻找最小的数字(这里说的层和距离都是斜过来的)

千万不能用dfs找每层的0....数据卡了这个,直接每次递推寻找最小值然后标记就好了(哭死了,当时因为这个超时没过)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 1005 int n,m;
int tx[4]={0,0,1,-1},ty[4]={1,-1,0,0};
char graph[N][N];
bool used[N][N];
int xx[1100000], yy[1100000]; void Bfsimilar(){
int i,j,k;
memset(used,false,sizeof(used));
used[1][1] = true;
int q=1,h=1;
xx[q]=yy[q]=1;
for (; q<=h ; q++) //递归形dfs拿时间换空间
if(graph[xx[q]][yy[q]]=='0'){
for(i=0;i<4;i++){
int X=xx[q]+tx[i], Y=yy[q]+ty[i];
if(X>0 && X<=n && Y>0 && Y<=m && !used[X][Y]){ //找到最近的1
h++;
xx[h]=X;
yy[h]=Y;
used[X][Y]=true;
}
}
}
if(used[n][m] && graph[n][m]=='0') { //处理一直是0的情况
printf("0\n");
return;
}
int ma=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(used[i][j])
ma=max(ma,i+j); //找到最近的0
printf("1");
//printf("%d\n",ma);
for(i=ma;i<n+m;i++){
char mi='1';
int temp1=max(1,i-m);
int temp2=min(n,i-1);
for(j=temp1;j<=temp2;j++)
if(used[j][i-j]){
mi=min(mi, graph[j+1][i-j]);
mi=min(mi, graph[j][i-j+1]);
}
printf("%c",mi);
for(j=temp1;j<=temp2;j++)
if(used[j][i-j]){
if(graph[j+1][i-j]==mi) used[j+1][i-j] =true;
if(graph[j][i-j+1]==mi) used[j][i-j+1] =true;
}
}
printf("\n");
} int main() {
//freopen("in.txt", "r", stdin);
int T;
int i,j,k;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
scanf("%s",graph[i]+1);
for(i=0;i<=n+1;i++)
graph[i][0]='2',graph[i][m+1]='2'; //将边界处理为2,方便之后的处理
for(i=0;i<= m+1;i++)
graph[0][i]='2',graph[n+1][i]='2';
Bfsimilar();
}
return 0;
}

2015 Multi-University Training Contest 4 Walk Out的更多相关文章

  1. 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 ...

  2. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  3. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...

  4. 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】

    2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...

  5. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  6. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  7. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  8. hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)

    OO's Sequence                                                          Time Limit: 4000/2000 MS (Jav ...

  9. HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

随机推荐

  1. bzoj 4871: [Shoi2017]摧毁“树状图” [树形DP]

    4871: [Shoi2017]摧毁"树状图" 题意:一颗无向树,选两条边不重复的路径,删去选择的点和路径剩下一些cc,求最多cc数. update 5.1 : 刚刚发现bzoj上 ...

  2. BZOJ 3270: 博物馆 [概率DP 高斯消元]

    http://www.lydsy.com/JudgeOnline/problem.php?id=3270 题意:一张无向图,一开始两人分别在$x$和$y$,每一分钟在点$i$不走的概率为$p[i]$, ...

  3. BZOJ 2754: [SCOI2012]喵星球上的点名 [后缀数组+暴力]

    2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1906  Solved: 839[Submit][St ...

  4. 2018/1/19 Netty学习笔记(一)

    这段时间学了好多好多东西,不过更多是细节和思想上的,比如分布式事物,二次提交,改善代码质量,还有一些看了一些源码什么的; 记录一下真正的技术学习,关于Netty的学习过程; 首先说Netty之前先说一 ...

  5. 如何解决JavaScript中0.1+0.2不等于0.3

    console.log(0.1+0.2===0.3)// true or false?? 在正常的数学逻辑思维中,0.1+0.2=0.3这个逻辑是正确的,但是在JavaScript中0.1+0.2!= ...

  6. react小结

    react基础小结 1. 例子 import React from 'react' import { render } from 'react-dom' // 定义组件 class Hello ext ...

  7. 5.C++里的4种新型类型转换

    1首先来回顾C的强制转换 大家都知道,在编译C语言中的强制转换时,编译器不会检查转换是否成功,都会编译正确. 比如: #include "stdio.h" struct Posit ...

  8. 代码从stepping stone搬移到内存

    为什么要搬移代码?如何搬移代码?arm启动流程回顾:2440:这里我们分析的是从nand flash 启动.2440的启动主要依赖于一个部件(SRAM),又名stepping stone.它的地址为0 ...

  9. 《Discuz安装时候出现乱码 -- 问题解决方法》

    自我安装discuz时出现安装界面乱码的情况,跟链接所说一样,经过原作的分享,加上我自己的实验,明白了,什么时候修改/usr/local/php/etc/php.ini里面的default_chars ...

  10. 树莓派系列教程:1.环境与系统,无显示器无键盘无网线联网并使用PuTTy与VNC图形界面远程登录

    本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...