2015 多校联赛 ——HDU5335(Walk out)
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2001 Accepted Submission(s): 376
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.
the answer itself is 0 (in
this case, print 0 instead).
2 2
11
11
3 3
001
111
101
101
从矩阵左上角走到右下角,而且经过的数字会被以二进制的方式记录下来,求最小
思路:如果第一个位置是1,只能一直走下 或者 右,找出最小值
如果第一个位置是0,找到离右下角最近的1,然后进行上面步骤
但是当时并没有实现,超时了,感觉有点麻烦 - -,用搜索不知道怎么记录值,而且容易超
标码中的用for循环求解,感觉好机智(⊙o⊙)…,找到最近的位置 x + y,找出这些中最小的值输出,再讲它们进行标记。
P:每次离右下角i + j的点是那些固定的,在这些固定的周围继续找。
必须扩充栈用C++,否则爆掉。 自己测试时就说怎么一直有问题 TAT
#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std; int T, n, m,tot;
char S[1100][1100];
bool used[1100][1100];
char ch = '0';
char ch1 = '1'; void bfs(int x,int y)
{
if(used[x][y])
return ;
used[x][y] = true; if(S[x][y] == ch1)
return; if (x>1) bfs(x-1,y);
if (x<n) bfs(x+1,y);
if (y>1) bfs(x,y-1);
if (y<m) bfs(x,y+1);
} int main()
{
//freopen("9.txt", "r", stdin); scanf("%d",&T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%s", S[i] + 1);
for (int i = 0; i <= n + 1; i++) //预处理
S[i][0] = '2', S[i][m + 1] = '2';
for (int i = 0; i <= m + 1; i++)
S[0][i] = '2', S[n + 1][i] = '2';
memset(used,false,sizeof(used));
tot = 0;
bfs(1,1); if(used[n][m] && S[n][m] == ch)
{
printf("0\n");//全是0
continue;
} for(int i= 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(used[i][j])
tot = max(tot,i+j); printf("1");
for (int i = tot; i < n + m; i++)
{
char mi = '1';
for (int j = 1; j <= n; j++)
if (1 <= i - j && i - j <= m && used[j][i - j])
{
mi = min(mi, S[j + 1][i - j]);
mi = min(mi, S[j][i - j + 1]);
}
printf("%c", mi);
for (int j = 1; j <= n; j++)
if (1 <= i - j && i - j <= m && used[j][i - j])
{
if (S[j + 1][i - j] == mi) used[j + 1][i - j] = true;
if (S[j][i - j + 1] == mi) used[j][i - j + 1] = true;
}
}
printf("\n");
} return 0;
}
2015 多校联赛 ——HDU5335(Walk out)的更多相关文章
- 2015 多校联赛 ——HDU5402(模拟)
For each test case, in the first line, you should print the maximum sum. In the next line you should ...
- 2015 多校联赛 ——HDU5334(构造)
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2015 多校联赛 ——HDU5302(构造)
Connect the Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 多校联赛 ——HDU5294(最短路,最小切割)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2015 多校联赛 ——HDU5325(DFS)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 多校联赛 ——HDU5316(线段树)
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...
- 2015 多校联赛 ——HDU5323(搜索)
Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015 多校联赛 ——HDU5319(模拟)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- 2015 多校联赛 ——HDU5301(技巧)
Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...
随机推荐
- Python实现网站模拟登陆
一.实验简介 1.1 基本介绍 本实验中我们将通过分析登陆流程并使用 Python 实现模拟登陆到一个实验提供的网站,在实验过程中将学习并实践 Python 的网络编程,Python 实现模拟登陆的方 ...
- java实现同步的两种方式
同步是多线程中的重要概念.同步的使用可以保证在多线程运行的环境中,程序不会产生设计之外的错误结果.同步的实现方式有两种,同步方法和同步块,这两种方式都要用到synchronized关键字. 给一个方法 ...
- logging日志
import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...
- [Android]上传到多个Maven仓库的Gradle插件RapidMavenPushPlugin
博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml RapidMavenPushPlugin 用 ...
- typescript简介
微软作为编译器狂魔一直有一个心病,就是改良JavaScript这种语法超级烂又很多人用的编程语言,于是TypeScript诞生了 先做个对比吧: TS JS 语法严谨性 严谨 宽松 静态性 静态 ...
- JS实现页面内跳转
使用js($.ajax中)实现页面内跳转(即:描点平滑跳转)的方法(aa为跳转目的标签的id): 在网络上有很多资料所说的:animate方法我试了并不好使,不知道是啥原因,欢迎大家指正,附上网络方法 ...
- 微信小程序组件学习中
一.轮播图 wxml代码: <swiper indicator-dots="true" autoplay="true" duration="10 ...
- Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面
参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...
- bootstrap 菜单之手风琴效果
自己用bootstrap搭了个项目,纯属娱乐....为了检验学习bootstrap之成果. 效果如图: 一.搭建中发现一问题,因为以前测试都是写的html页面,这次用了母版页,点击页面的之后,页面会刷 ...
- 页面加载loading动画
关于页面加载的loading动画,能度娘到的大部分都是通过定时器+蒙层实现的,虽然表面上实现了动画效果,实际上动化进程和页面加载进程是没有什么关系的,只是设置几秒钟之后关闭蒙层,但假如页面须要加载的元 ...