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 ...
随机推荐
- web.xml中contextConfigLocation的作用(转)
原文地址:http://blog.csdn.net/zhangliao613/article/details/6289114 原文格式较乱,此处略作整理.内容未变. 在web.xml中使用contex ...
- Web Api Session开启会话支持
1.WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 //代码如下 public override voi ...
- (转)web.config详解之在文件中配置网站默认页面
在<configuration></configuration>中添加下面的配置 <system.webServer> <defaultDocu ...
- ios文件读取
/* * @brief 沙盒下的各种文件 */ - (void)getPaths { /** * @brief 获取沙盒的路径 */ NSString * HomeDirectory = NSHo ...
- RadGrid SelectedIndexChanged 事件没反应的解决方法
Hello Hrushikesh, You can set ClientSettings.EnablePostBackOnRowClick to true along with ClientSetti ...
- FileUpload
一upload原理: 1.表单的method必须是post方法 2.enctype属性必须是“mutipatr/form-data”类型 enctype默认的属性是“application/x-www ...
- ZigZag-LeetCode
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- lnmp 60秒的服务器缓存时间
1.问题 php代码写好之后执行发现居然没有生效,打断点,改代码.刷新都没有达到预期的效果.但是间隔60秒之后刷新就看到效果了,或者删除文件就里面见效. 2.原因 从phpinfo()页面输出搜索&q ...
- a标签href不跳转 禁止跳转
a标签href不跳转 禁止跳转 当页面中a标签不需要任何跳转时,从原理上来讲,可分如下两种方法: 标签属性href,使其指向空或不返回任何内容.如: <a href="javascri ...
- TypeScript环境搭建
环境搭建 本篇将简单介绍一下TypeScript,并记录开发环境的搭建.使用Visual Studio Code进行一个简单的Demo开发过程. 第一部分.简介 TypeScript是一种由微软开发的 ...