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 ...
随机推荐
- UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库
在应用中使用 SQLite 数据库来存储数据是相当常见的.在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLit ...
- bzoj 3451: Tyvj1953 Normal [fft 点分治 期望]
3451: Tyvj1953 Normal 题意: N 个点的树,点分治时等概率地随机选点,代价为当前连通块的顶点数量,求代价的期望值 百年难遇的点分治一遍AC!!! 今天又去翻了一下<具体数学 ...
- MarkDown 编辑数学公式
1. 参考博客:http://blog.csdn.net/smstong/article/details/44340637 1 数学公式的web解决方案 在网页上显示漂亮的数学公式,是多年来数学工作者 ...
- LVS结合keepalived配置测试
LVS/DR + keepalived配置 注意:前面虽然我们已经配置过一些操作,但是下面我们使用keepaliave操作和之前的操作是有些冲突的,所以若是之前配置过DR,请首先做如下操作: 三 ...
- Halcon一日一练:图像拼接技术2:步骤与例程
上一篇主要介绍了图像拼接的一些原理和方法,这一篇将主要介绍步骤和例程: 接上一篇: 基于特征的接拼方法,分为四个步骤 1.特征检测:从图像中检测出显著且独特的图像特征,诸如:闭合区域,直线段,边缘,轮 ...
- String类为什么是final的
首先我们使用new创建一个String对象的时候比如: String str=new String("123"); 这句话里面创建了两个对象,第一个在系统中创建了一个"a ...
- 浅谈session,cookie,sessionStorage,localStorage的区别及应用场景
浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...
- 用yii2给app写接口(上)
Yii2如何实现RESTful风格的API 1.建立单独的应用程序 为了增加程序的可维护性,易操作性,我们选择新建一套应用程序,这也是为了和前台应用.后台应用区分开操作.有些人要嚷嚷了,为啥非得单独搞 ...
- Docker系统八:Docker的存储驱动
Docker存储驱动 1. Docker存储驱动历史 Docker目前支持的greph driver包括: AUFS device-mapper btrfs overlayfs(重点) 关于各存储驱的 ...
- Ansible自动化运维笔记1(安装配置)
1.Ansible的安装 pip install ansible==1.9.1 ansible1.9.1版本依赖的软件有 Python2.6以上版本 paramiko模块 PyYAML Jinja2 ...