2015 Multi-University Training Contest 4 Walk Out
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3210 Accepted Submission(s): 647
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.
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.
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 据 ...
- 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 ...
- 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)
官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...
- 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 ...
- hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)
OO's Sequence Time Limit: 4000/2000 MS (Jav ...
- 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 ...
随机推荐
- 700名黑客无法攻破Trillium安全防护软件 欲以网络安全险获利
盖世汽车讯 据外媒报道,在2017 DEF CON黑客大会上,黑客们受邀入侵Car Hacking Village会场内的各类自动驾驶车辆.物联网网络安全公司--Trillium Incorporat ...
- AGC017 F - Zigzag
传送门 Time limit : 4sec / Memory limit : 256MB Score : 1600 points Problem Statement There are N(N+1)⁄ ...
- 使用Python的requests库进行接口测试——session对象的妙用
from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
- qt中的多线程
1.dialog.h #define DIALOG_H #include <QDialog>#include"mythread.h"namespace Ui {clas ...
- 在Arrays.asList()引发的问题中进一步学习集合与泛型等内容
前言 最近在网上看到一个问题,情况类似如下(记为问题1): public class Demo { public static void main(String[] args) { System.ou ...
- 一个客户端一键安装环境和服务的shell脚本
#!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...
- Array 数组的排序 sort
JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序.sort() 方法用于对数组的元素进行排序.语法如下:arrayObject.sort(s ...
- JetBrains Rider 破解 (ideaIU等等开发工具都通用)2018-02-27
贴一下Rider下载地址:(下载不了可以用百度云离线下载) Win:https://download.jetbrains.com/resharper/JetBrains.Rider-2017.3.1. ...
- mac 上格式化磁盘出现MediaKit报告设备上的空间不足以执行请求的解决办法
1.问题描述: 我使用是一个2T移动硬盘,分了5个区 2.分析原因:因为mac OSX的日志式格式需要有EFI分区进行引导,而我的移动硬盘是没有EFI分区的,这样的话就会出现问题: 3.解决办法: 1 ...
- Java中空串和null串的区别
对于空串来说这是一个对象他被""这个对象给实例化了只是他的长度为0字符的内容为空. 而String变量中还可以存储一个特殊的值,这个是null,这个表示没有和其他的对象与这个变量相 ...