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 ...
随机推荐
- 团队开发---”我爱淘“校园二手书店 NABC分析
本项目特点之一:可预订 N:对于一些抢手的书可以提前预定,避免学生买不到书 A:网上下单,通过手机便捷购物 B:使得订书更加方便快捷 C:二手书摊.网上书店 团队成员:杨广鑫.郭健豪.李明.郑涛
- 201621123031 《Java程序设计》第7周学习总结
作业07-Java GUI编程 1.本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 事件监听器: Java事件监听器是由事件类和监听接口组成,自定义 ...
- 20145237 《Java程序设计》第九周学习总结
20145237 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令 ...
- poj 2142 The Balance
The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS Memory Limit: 65536K Descripti ...
- 11-移动端开发教程-zepto.js入门教程
Zepto.js是一个轻量级的针对现代浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. 1. Why Zepto.js? API类 ...
- zookeeper入门系列:paxos协议
上一章讨论了一种强一致性的情况,即需要分布式事务来解决,本章我们来讨论一种最终一致的算法,paxos算法. paxos算法是由大牛lamport发明的,关于paxos算法有很多趣事.比如lamport ...
- JQuery 动态加载iframe.
html: <iframe id="ifm" style="width:inherit;height:inherit" runat="serve ...
- python全栈开发-json和pickle模块(数据的序列化)
一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...
- Python内置函数(41)——id
英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...
- Linux实战案例(4)CentOS清除用户登录记录和命令历史方法
CentOS清除用户登录记录和命令历史方法 清除登陆系统成功的记录[root@localhost root]# echo > /var/log/wtmp //此文件默认打开时乱码,可查到ip等信 ...