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. 3167: [Heoi2013]Sao [树形DP]

    3167: [Heoi2013]Sao 题意: n个点的"有向"树,求拓扑排序方案数 Welcome to Sword Art Online!!! 一开始想错了...没有考虑一个点 ...

  2. poj1265&&2954 [皮克定理 格点多边形]【学习笔记】

    Q:皮克定理这种一句话的东西为什么还要写学习笔记啊? A:多好玩啊... PS:除了蓝色字体之外都是废话啊...  Part I 1.顶点全在格点上的多边形叫做格点多边形(坐标全是整数) 2.维基百科 ...

  3. 2018/1/28 RocketMq学习笔记

    RocketMq是支持Topic模式的MQ中间件,它的传输格式为topic(主题,一个product对应一个主题,),Tag(标签,其实就是副标题,是为了更好的支持集群模式而出现的,这样客户端可以指定 ...

  4. 📉 Draggable Curve Control (English)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature called Curve A ...

  5. [Python Study Notes]电池信息

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  6. virsh 常用操作

    virsh list 显示在运行的 虚拟机    virsh list --all 显示在运行和停止的虚拟机    ssh 192.168.0.115 通过网络连接子机   如果没有网络 可以通过 v ...

  7. 读书简记-java与模式

  8. 【Unity3D技术文档翻译】第1.5篇 本地使用 AssetBundles

    上一章:[Unity3D技术文档翻译]第1.4篇 AssetBundle 依赖关系 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Devel ...

  9. RMI远程服务调用

    数据库:info.sql /* Navicat MySQL Data Transfer Source Server : yuanzhen Source Server Version : 50713 S ...

  10. c++ 如何把RGB图像转换成HSV图像?

    CV_<bit_depth>(S|U|F)C<number_of_channels> 1--bit_depth---比特数---代表8bite,16bites,32bites, ...