HDU 5335 Walk Out BFS 比较坑
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
For each testcase, the first line contains two integers n and m \ (1 \le n, m \le 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
Sample Input
2 2
11
11
3 3
001
111
101
Sample Output
101
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
#define MM(a,b); memset(a,b,sizeof(a));
int n,m,step,res,ans[1000000+10],vis[1003][1003],cnt=0,a[1010][1010];
int dx[5]={0,1,0,-1};
int dy[5]={1,0,-1,0};
int flag[1000+1000+100];
char s[1003][1003]; struct node{
int x,y;
};
queue<node> q; bool legal(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m;
} void bfs()
{
q.push((node){1,1});
vis[1][1]=1;
while(q.size())
{
int x=q.front().x,y=q.front().y;q.pop();
for(int i=0;i<4;i++)
{
int tx=x+dx[i],ty=y+dy[i];
if(vis[tx][ty]||!legal(tx,ty)||s[tx][ty]=='1') continue;
vis[tx][ty]=1;
q.push((node){tx,ty});
if(tx+ty-1>res) {res=tx+ty-1;flag[res]=1;}
}
}
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
MM(flag,0);MM(vis,0); if(s[1][1]=='1') {res=0;flag[1]=0;q.push((node){1,1});}
else
{
res=1;
flag[1]=1;
bfs();
if(res==n+m-1) {printf("0\n");continue;}
for(int x=1;x<=n;x++)
{
int y=res+1-x;
if(vis[x][y]) q.push((node){x,y});
}
} while(q.size())
{
int ux=q.front().x,uy=q.front().y;q.pop();
if(flag[ux+uy-1]&&s[ux][uy]=='1') continue;
for(int i=0;i<2;i++)
{
int vx=ux+dx[i],vy=uy+dy[i];
if(!legal(vx,vy)||vis[vx][vy]) continue;
vis[vx][vy]=1;
if(s[vx][vy]=='1'&&!flag[vx+vy-1]) q.push((node){vx,vy});
if(s[vx][vy]=='0') {flag[vx+vy-1]=1;q.push((node{vx,vy}));}
}
} int i=1;
for(;i<=n+m-1;i++)
if(!flag[i]) break;
if(i>n+m-1) printf("0");
else for(;i<=n+m-1;i++)
printf("%d",!flag[i]);
printf("\n");
}
return 0;
}
分析:比赛时看到这道题过的人比较少,以为很难,,其实后来看了下题解,再自己想了下,发现自己比赛时离做出这道题
已经很近了,自己想到了,应该做矩阵的斜对角线,主要目标是在这个上面搜,然后也想到只能往右下角走,但是忘了一种特殊
情况,就是如果s[1][1]=='0'的话,那么就不一定非得往右下角走,但是总之得先通过0达到离点(n,m)最靠近的对角线,然后再往右下角走
HDU 5335 Walk Out BFS 比较坑的更多相关文章
- 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(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 (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- hdu 5094 状压bfs+深坑
http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...
- 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 ...
- hdu 5335 Walk Out (2015 Multi-University Training Contest 4)
Walk Out Time Limit: 2000/10 ...
- hdu 5335 Walk Out 搜索+贪心
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
随机推荐
- LeetCode 142——环形链表II(JAVA)
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...
- 美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)
大意: 给定树, 初始每个点全为白色, 点$i$有权值$k_i$, 表示选择$i$后, 所有距离$i$小于$k_i$的祖先(包括i)会变为黑色, 求最少选多少个点能使所有点变为黑色. 链上情况的话, ...
- 怎样使 html 文本文字不能被选中?
-webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none;
- linux 下安装 jre
本文链接:https://blog.csdn.net/qq_34368587/article/details/79559102 Linux下安装Java运行环境 现需要项目部署到Linux中,需要配置 ...
- 【原创】大叔经验分享(72)mysql时区
查看当前时区 > show variables like '%time_zone%'; +------------------+--------+ | Variable_name | Value ...
- 适配方案(四)适配的基础知识之单位、分辨率、viewport
适配的基础知识 一.理解单位 px.pt.pc.sp.em.rem.dpr.dp.dip.ppi.dpi.ldpi.mdpi.hdpi.xhdpi.xxhdpi 如果你是ios开发,你需要了解的单位: ...
- shell中$(( ))、$( )、``与${ }的区别详解
命令替换在bash中,$( )与` `(反引号)都是用来作命令替换的.命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行 [python@m ...
- JavaWeb【过滤器】
定义: 服务器端组件,可以截取用户端的请求和响应,并对这些信息做过滤. 课程概要: 1.工作原理 2.生命周期 1.web.xml配置 注意:url-pattern配置路径前面需要加"/&q ...
- 第三章·MySQL版本区别及管理
一.MySQL5.6与MySQL5.7安装的区别 1.cmake的时候加入了bostorg 2.初始化时 使用mysqld --initialize 替代mysql_install_db,其它参数没有 ...
- busybox date 时间的加减
1.下载安装busybox: # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2 # tar -jxvf busybox-.tar.b ...