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 ...
随机推荐
- 文件操作:w,w+,r,r+,a,wb,rb
1.文件操作是什么? 操作文件: f = open("文件路径",mode="模式",encoding="编码") open() # 调用操 ...
- python *****【异常处理】*****
try: val = input('请输入数字:') num = int(val) except Exception as e: print('操作异常') # import requests # # ...
- pycharm2017.3.3永久激活(转载)
pycharm是很强大的开发工具,但是每次注册着实让人头疼.网络上很多注册码.注册服务器等等.但都只是一年或者不能用:为次有如下解决方案.亲测有效!!! 如果想让pycharm永久被激活,比如截止日到 ...
- SSD训练网络参数计算
一个预测层的网络结构如下所示: 可以看到,是由三个分支组成的,分别是"PriorBox"层,以及conf.loc的预测层,其中,conf与loc的预测层的参数是由PriorBox的 ...
- c++ 使用 gsoap 调用 WebService 中文乱码
c++ 使用 gsoap 调用 WebService 中文乱码 问题产生: 使用gsoap时,如果WebService服务端及客户调用端都使用 C++ , 再传递中文时不会存在乱码问题, 当客户 ...
- Python——用turtle画一个月饼
今天是中秋节,首先在这里祝大家中秋快乐!那么提到中秋,我们首先想到的当然是香甜的月饼,所以我今天就在这里画一个月饼送给大家. 那么 要用Python画图,我们必须掌握并运用Turtle库,这个可以自己 ...
- cube打包后css顺序错误
先说下解决办法: 把import {...} from cube-ui放在 import App from './App.vue'的前面 不然会产生如下错误 正确的顺序
- css基础(代码)
display: block; /*元素分为三大类,设置元素的显示方式} 行内 inlineli{ ...
- 快速写个node命令行工具
1.package.json-bin配置 [创建bat文件,把bat路径添加到PATH中]这些固定的工作可以由npm帮我们完成.package.json中有个bin字段配置: bin: { " ...
- 使用docker搭建OpenResty开发环境
Dockerfile文件: FROM centos:latest RUN yum install -y pcre-devel openssl-devel gcc curl wget perl make ...