hdu 5335 Walk Out(bfs+寻找路径)
In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a or a written on it. An explorer gets lost in this grid. His position now is (,), 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.
The first line of the input is a single integer T (T=), indicating the number of testcases. For each testcase, the first line contains two integers n and m (≤n,m≤). The i-th line of the next n lines contains one string of length m, which represents i-th row of the maze.
For each testcase, print the answer in binary system. Please eliminate all the preceding unless the answer itself is (in this case, print instead).
1、如果mp[0][0]==0的话,先bfs一次,将前导0去掉。bfs得到了wx,wy
2、然后就是找最小路径了。枚举步数for(int i=wx+wy;i<n+m-2;i++),在这个步数的基础上枚举所有可能的(x,y)x+y==step,如果遇到0则取0,一直找到(n-1,m-1)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stdlib.h>
using namespace std;
#define N 1006
int n,m;
int mp[N][N];
int vis[N][N];
struct Node{
int x,y;
}st;
int dirx[]={,,-,};
int diry[]={,,,-};
int wx,wy;
void bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(vis[t2.x][t2.y]) continue;
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
vis[t2.x][t2.y]=;
if(mp[t2.x][t2.y]==){
q.push(t2);
}
if(wx+wy<t2.x+t2.y){
wx=t2.x;
wy=t2.y;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
char s[];
for(int i=;i<n;i++){
scanf("%s",s);
for(int j=;j<m;j++){
mp[i][j]=s[j]-'';
}
}
st.x=;
st.y=;
memset(vis,,sizeof(vis));
vis[][]=;
wx=;
wy=;
if(mp[][]==){
bfs();
}
if(mp[wx][wy]==){
printf("0\n");
continue;
}
printf("");
int nowflag=;
for(int i=wx+wy;i<n+m-;i++){
int flag=;
int step=i;
for(int j=;j<=step;j++){
int x=j;
int y=i-j;
if(x< || x>=n || y< || y>=m) continue;
if(nowflag && mp[x][y]) continue;
if(!vis[x][y]) continue; for(int k=;k<;k++){
int sx=x+dirx[k];
int sy=y+diry[k];
if(sx< || sx>=n || sy< || sy>=m) continue;
vis[sx][sy]=;
if(mp[sx][sy]==){
flag=;
}
} }
nowflag=flag;
if(flag){
printf("");
}
else{
printf("");
}
}
printf("\n");
}
return ;
}
hdu 5335 Walk Out(bfs+寻找路径)的更多相关文章
- 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+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- HDU 5335 Walk Out (BFS,技巧)
题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- 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 ...
- HDU 5335 Walk Out
题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...
- 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 ...
随机推荐
- text-overflow简单使用
text-overflow属性配合overflow才有效果,还记得把文字强制一行显示,如下代码: <!DOCTYPE html> <html lang="zh-cn&quo ...
- JavaScript 中的正常任务与微任务
正常情况下,JavaScript的任务是同步执行的,即执行完前一个任务,然后执行后一个任务.只有遇到异步任务的情况下,执行顺序才会改变. 这时,需要区分两种任务:正常任务(task)与微任务(micr ...
- Oracle CheckPoint进程
在实例经过分配内存结构,加载控制文件后,然后要打开数据库的时候,需要做到控制文件,数据文件,联机重做日志保持相互状态一致性,数据库才可以打开.当数据库发生实例不正常关闭时(比如系统掉电或者Shutdo ...
- 注册DLL,Unregister DLL
前一篇文章,反复注册,反注册.... 写了一个小工具 怎么传图片感觉不对劲,StatusBar应改成 拖动DLL至上方 文件 http://files.cnblogs.com/magicdawn/Dl ...
- C#读取XML配置文件
DataSource.xml文件,要放在bin/debug/目录下: <?xml version="1.0" encoding="utf-8" ?> ...
- UVa10651(记忆化搜索)
题意: 给一串-和o组成的字符串,你可以把“-oo"变成”o--",可以把“oo-”变成“--o",问最后最少有多少个o. 状态压缩 ,记忆化搜索 code: #incl ...
- D3.js data() 方法详解
Binding data(数据绑定) D3各种图表的作用体现在将数据(Data)转换成可视化的过程. 比如将一个月的气温数据,通过树形图来展现,能够直观的看到气温走势,下个月还需不需要穿秋裤 :) 我 ...
- jquery国内cdn
推荐几个国内的jquery CDN服务地址吧: 新浪CDN,感觉很快,用的人很多,推荐使用! http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.m ...
- POJ2446 二分图最大匹配
问题:POJ2446 分析: 采用黑白相间的方法把棋盘分成两个点集,是否可以用1*2的卡片实现全覆盖等价于二分图是否有完全匹配. AC代码 //Memory: 172K Time: 32MS #inc ...
- PHP原始的数据库操作
<?php //这是一个工具类;作用是完成对数据库的操作; class SqlHelper{ public $conn; public $dbname=& ...