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 ...
随机推荐
- spark教程(19)-sparkSQL 性能优化之谓词下推
在 sql 语言中,where 表示的是过滤,这部分语句被 sql 层解析后,在数据库内部以谓词的形式出现: 在 sparkSQL 中,如果出现 where,它会现在数据库层面进行过滤,一般数据库会有 ...
- Pytest+allure安装和框架搭建
接口自动化框架搭建 -- 公司系统自测使用,只跑核心业务流程 编辑中...... 1.安装Pytest pip install -U pytest 1.1Pycharm测试脚本运行 创建project ...
- Make It One CodeForces - 1043F (数论,最短路,好题)
大意: 给定序列$a$, 求最小子集, 使得gcd为1. 对于数$x$, 素因子多少次幂是无关紧要的, 这样就可以用一个二进制数来表示. $x$取$gcd$后的二进制状态最多$2^7$, 可以暴力枚举 ...
- 论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks
论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks 2018年07月11日 14 ...
- 【原创】大叔经验分享(68)maven工程查看jar包依赖
1 idea 结果 2 maven命令 $ mvn dependency:tree 结果 [INFO] +- org.springframework.boot:spring-boot-starter- ...
- 配置Notepad++万能调试
需求: 正常情况下 使用notepad++编辑修改一些网页或脚本文件,修改之后想要查看效果需要Ctrl+S保存,然后从文件目录中打开查看. 现在我想做的就是简化查看效果的流程,让notepad++实现 ...
- Java学习笔记【六、正则表达式】
参考:http://www.runoob.com/java/java-regular-expressions.html 概述 java.util.regex包,主要包含三个类: Pattern:正则表 ...
- fastadmin 随笔 刷新表格数据 获取当前登录人信息 服务端导出Excel
table.bootstrapTable('refresh',{url:'你的url'}); 获取当前登录人信息 $this->auth就能获取当前用户信息,比如$this->auth-& ...
- PXE help Tips
http://www.kano.org.uk/projects/pxe/ http://howto.basjes.nl/linux/installing-fedora-linux-via-pxe-x8 ...
- scroll js 原生
1.当前位置滚动: document.documentElement.scrollTop 当前位置: 有可能是0 window.scrollTo(,document.documentElement.s ...