HDU-5335
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1167 Accepted Submission(s): 216
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.
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.
2
2 2
11
11
3 3
111
111
111
/**
题意:给一个n*m的01矩阵 然后要求从(0,0) 走到(n-1,m-1)
问走到的最小的串
做法:bfs + 贪心先找离(n-1,m-1),最近的1的位置,就是找所有的
前缀0,然后从最近的1开始搜,只需要搜索当前位置的左和下
然后直至(n-1,m-1)
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define maxn 1100
int vis[maxn][maxn];
char ch[maxn][maxn];
int n, m;
int dx[][] = {, , , , -, , , -};
int sx, sy;
struct Node
{
int x;
int y;
Node() {}
};
int check(int x, int y)
{
if(x >= && x < n && y >= && y < m) {
return ;
}
return ;
}
void bfs(int x, int y)
{
Node tmp, now, temp;
queue<Node>que;
vis[x][y] = ;
temp.x = x;
temp.y = y;
que.push(temp);
while(!que.empty())
{
now = que.front();
que.pop();
for(int i = ; i < ; i++)
{
tmp.x = now.x + dx[i][];
tmp.y = now.y + dx[i][];
if(check(tmp.x, tmp.y) && vis[tmp.x][tmp.y] == )
{
vis[tmp.x][tmp.y] = ;
if(ch[tmp.x][tmp.y] == '') {
que.push(tmp);
}
if(tmp.x + tmp.y > sx + sy)
{
sx = tmp.x;
sy = tmp.y;
}
}
}
}
}
void bfs1()
{
printf("");
bool isok = false;
bool isok1 = false;
for(int i = sx + sy; i < n + m - ; i++)
{
isok = false;
for(int j = ; j <= i; j++)
{
int x = j;
int y = i - j;
if(check(x, y) == || vis[x][y] == ) {
continue;
}
if(isok1 && ch[x][y] == '') {
continue;
}
for(int p = ; p < ; p++)
{
int tx = x + dx[p][];
int ty = y + dx[p][];
if(check(tx, ty) == ) {
continue;
}
vis[tx][ty] = ;
if(ch[tx][ty] == '') {
isok = true;
}
}
}
isok1 = isok;
if(isok) {
printf("");
}
else {
printf("");
}
}
printf("\n");
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
memset(vis, , sizeof(vis));
for(int i = ; i < n; i++)
{
scanf("%s", ch[i]);
}
sx = sy = ;
vis[][] = ;
if(ch[][] == '') {
bfs(, );
}
//cout << sx << " " << sy << endl;
if(ch[sx][sy] == '') {
printf("0\n");
}
else {
bfs1();
}
}
return ;
}
HDU-5335的更多相关文章
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- HDU 5335 Walk Out BFS 比较坑
H - H Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 5335 Walk Out (BFS,技巧)
题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- HDU 5335 Walk Out
题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...
- hdu 5335 Walk Out(bfs+寻找路径)
Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...
- HDU 5335——Walk Out——————【贪心】
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5335 Walk Out(多校)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 2015 多校赛 第四场 1009 (hdu 5335)
Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit) ...
- hdu 5335 Walk Out (2015 Multi-University Training Contest 4)
Walk Out Time Limit: 2000/10 ...
随机推荐
- [bzoj] 1588 营业额统计 || Splay板子题
原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...
- application.properties 改成 application.yml
application.properties 改成 application.yml
- 关于string::size_type
size_type其实是string模板类定义的一种类型之一,它与size_of的用法相似,只是它根据存储的类型返回字符串的长度.对于 string具体化,将根据char返回字符串的长度,在这种情况下 ...
- hadoop设置公平队列
http://hadoop.apache.org/docs/r1.2.1/fair_scheduler.html fair-scheduler.xml文档 <?xml version=" ...
- 一个JAVA题引发的思考
转载自:http://www.cnblogs.com/heshan664754022/archive/2013/03/24/2979495.html 十年半山 今天在论坛闲逛的时候发现了一个很有趣的题 ...
- Codeforces Round #380 (Div. 2)/729E Subordinates 贪心
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...
- [USACO13FEB]出租车Taxi
洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...
- java 8新特性 instant
Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR310规范).在本系列文章中,将对这些新的API进行举例说明. ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水
[算法]最小生成树 [题解] 想到网络流,但是好像不能处理流量和费用的关系. 想到最短路,但好像不能处理重复选边的问题. 每条边只需要选一次,每个点就要遍历到,可以想到最小生成树. 建超级源向每个点连 ...
- 【洛谷 T47488】 D:希望 (点分治)
题目链接 看到这种找树链的题目肯定是想到点分治的. 我码了一下午,\(debug\)一晚上,终于做到只有两个点TLE了. 我的是不完美做法 加上特判\(A\)了这题qwq 记录每个字母在母串中出现的所 ...