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 ...
随机推荐
- centos7 追加python3 + 使用pip + virtualenv
一.安装Python3的方法: 首先安装依赖包: yum -y groupinstall "Development tools" yum -y install zlib-devel ...
- 作业5:Java编译原理
零.编译 1.编译器 (1)前端编译器:.java文件转变为.class文件Sun的javacEclipse JDT中的增量编译器(ECJ) (2)后端编译器:.class文件转变为机器码HotSpo ...
- sql server存储过程回滚事务
SET NOCOUNT ON这个很常用 作用:阻止在结果集中返回显示受T-SQL语句或则usp影响的行计数信息. 当SET ONCOUNT ON时候,不返回计数,当SET NOCOUNT OFF时候, ...
- centos7上的firewalld 的使用
#centos7上的firewalld 的使用 一.firewalld的基本启动关闭命令 启动服务------systemctl start firewalld 关闭服务------systemctl ...
- php之Opcache
opcache的原理 1.Opcache是什么? Opcache是一种通过将解析的PHP脚本预编译的字节码(Operate Code)存放在共享内存中来避免每次加载和解析PHP脚本的开销,解析器可以直 ...
- chrome input 输入框去掉黄色
-webkit-box-shadow: 0 0 0px 1000px white inset input设置内置阴影,要比你的input本身大
- vue 节流
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bridge的数据在内核处理流程
转:http://blog.sina.com.cn/s/blog_67cc0c8f0101oh33.html 转载一篇Bridge的数据在内核处理流程,文章写的不错啊! (2013-07-05 16: ...
- 修改tomcat 端口
假设tomcat所在目录为/usr/local/apache-tomcat/ 1.打开tomcat配置文件#vi /usr/local/apache-tomcat/conf/server.xml 2. ...
- 异步通信&同步通信
首先是两者的不同: 同步通信要求接收端时钟频率和发送端时钟频率一致,发送端发送连续的比特流:异步通信时不要求接收端时钟和发送端时钟同步,发送端发送完一个字节后,可经过任意长的时间间隔再发送下一个字节. ...