Walk Out

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

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).
 
Sample Input
2
2 2
11
11
3 3
001
111
101
 
Sample Output
111
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)的更多相关文章

  1. 2015 多校联赛 ——HDU5402(模拟)

    For each test case, in the first line, you should print the maximum sum. In the next line you should ...

  2. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. 2015 多校联赛 ——HDU5294(最短路,最小切割)

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

  5. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  6. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  7. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  9. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

随机推荐

  1. 2018上C语言程序设计(高级)作业- 第2次作业

    作业要求一 提交截图: 6-7: 6-8: 6-9: 7-1: 作业要求二 题目6-7删除字符中数字字符 1.设计思路: (1)第一步:本题要求是删除字符中的数字字符,我的主要思路是通过数组遍历若遇到 ...

  2. scrapy 避免被ban

    1.settings.pyCOOKIES_ENABLED = False DOWNLOAD_DELAY = 3 ROBOTSTXT_OBEY = Falseip代理池设置 IPPOOL = [{'ip ...

  3. 使用Github pages+jekyll搭建自己的博客(windows版)

    最近突发奇想,想试试GitHub pages来搭建博客.网上一搜一大堆,嗯...看来还是挺简单的...于是自己撸起袖子干...... 结果对于我这种GitHub注册过,git 没用过,ruby.jek ...

  4. 13-TypeScript单例模式

    在JavaScript中,要实现设计模式比较复杂.而在TypeScript中因为使用面向对象的思想编程,要实现设计模式的方式与后端语言C#.Java等非常类似. 单例模式是一种常用的设计模式,通常用于 ...

  5. Linux "零拷贝" sendfile函数中文说明及实际操作

    Sendfile函数说明 #include ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); sendfile ...

  6. js 过多 导致页面加载过慢

    自己的代码检查了很久,才检查 出来 通常我们的网站里面会加载一些js代码,统计啊,google广告啊,百度同盟啊,阿里妈妈广告代码啊, 一堆,最后弄得页面加载速度很慢,很慢. 解决办法:换一个js包含 ...

  7. python中的赋值与深浅拷贝

    Python当中对于拷贝,分为两种类型.一种是数字和字符串,另一种就是列表.元组.字典等其他类型了. 一.数字和字符串的拷贝 1.赋值 举个栗子: a1 = 123123 a2 = 123123 # ...

  8. 新概念英语(1-39)Don't drop it!

    新概念英语(1-39)Don't drop it! Where does Sam put the vase in the end ? A:What are you going to do with t ...

  9. Cookie、Session登陆验证相关介绍和用法

    一.Cookie和Session 首先.HTTP协议是无状态的:所谓的无状态是指每次的请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应直接影响,也不会直接 ...

  10. powerdesigner将name的名字赋给comment

    1 PowerDesigner中批量根据对象的name生成comment的脚本 执行方法:Open PDM -- Tools -- Execute Commands -- Run Script Vb ...