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 ...
随机推荐
- BZOJ4815 [CQOI2017]小Q的表格 【数论 + 分块】
题目链接 BZOJ4815 题解 根据题中的式子,手玩一下发现和\(gcd\)很像 化一下式子: \[ \begin{aligned} bf(a,a + b) &= (a + b)f(a,b) ...
- linux查找文件目录及mysql卸载
我们要卸载 mysql但是不知道其安装在哪里了,可以用 where +关键词 的方式查找,如上图 输入 whereis mysql 后,下面显示出了4个包含mysql的位置. ..... 查看安装m ...
- nginx 报invalid pid number
/opt/ibis/sbin/nginx -c /opt/ibis/conf/nginx.conf 这是make make install之后生成的文件夹和文件. -c c是configure的缩写 ...
- 【题解】我也不是B ifrog 1112 二分 倍增
题目传送门:http://ifrog.cc/acm/problem/1112 神奇的倍增二分,长见识了,在此做个记录,分享给大家. 懒得写题解了,直接转YJQ的:http://ifrog.cc/acm ...
- 在IAR使用FreeRTOS出现Error[Pa045]: function "XXX" has no prototype
FreeRTOS官方例程中设置了需要“Require prototype”,所以每个函数(除了main函数)都需要函数声明,其中对于无形参的函数声明要加void,比如void led_init(voi ...
- android中dip、dp、px、sp和屏幕密度
1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这 这个 ...
- linux用户管理和文件权限
linux用户管理和文件权限 新建用户:useradd ftpuser useradd -g gxx userxx修改密码:passwd ftpuser新增用户组:# groupadd gr ...
- maven中package和install命令的区别
mvn package 编译并打包工程,根据pom文件中packaging是jar还是war进行打包,会在项目的target目录下生成一个jar/war包. mvn install 打包并安装到mav ...
- gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...
- 求逆元的两种方法+求逆元的O(n)递推算法
到国庆假期都是复习阶段..所以把一些东西整理重温一下. gcd(a,p)=1,ax≡1(%p),则x为a的逆元.注意前提:gcd(a,p)=1; 方法一:拓展欧几里得 gcd(a,p)=1,ax≡1( ...