SPOJ SERGRID - Grid BFS
SERGRID - Grid
You are on an nxm grid where each square on the grid has a digit on it. From a given square that has digit k on it, a Move consists of jumping exactly k squares in one of the four cardinal directions. A move cannot go beyond the edges of the grid; it does
not wrap. What is the minimum number of moves required to get from the top-left corner to the bottom-right corner?
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input contains two space-separated integers n and m (1≤n,m≤500), indicating the size of the grid. It is guaranteed that
at least one of n and m is greater than 1. The next n lines will each consist of m digits, with no spaces, indicating the nxm grid. Each digit is between 0 and 9, inclusive. The top-left corner of the grid will be the square corresponding to the first character
in the first line of the test case. The bottom-right corner of the grid will be the square corresponding to the last character in the last line of the test case.
Output
Output a single integer on a line by itself representing the minimum number of moves required to get from the top-left corner of the grid to the bottom-right. If it isn’t possible, output -1.
Example
Input:
5 4
2120
1203
3113
1120
1110
Output:
6
思路:恰好能够跳Map[i][j]步,dfs超时,改用bfs
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
const int INF=0X3F3F3F3F;
const int MAXN=550;
int Map[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n,m;
struct node
{
int x,y,step;
node(){x=y=step=0;}
};
void init()
{
memset(Map,0,sizeof(Map));
memset(vis,false,sizeof(vis));
char s[m+2];
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=0;j<m;j++)Map[i][j]=s[j]-'0';
}
}
int bfs()
{
node now,next;
queue<node> q;
now.x=now.y=now.step=0;
vis[0][0]=true;
q.push(now);
while(!q.empty())
{
now=q.front();q.pop();
if(now.x==n-1&&now.y==m-1)return now.step;
int dir[4][2]={{0,Map[now.x][now.y]},{0,-Map[now.x][now.y]},{Map[now.x][now.y],0},{-Map[now.x][now.y],0}};
for(int i=0;i<4;i++)
{
int xx=now.x+dir[i][0],yy=now.y+dir[i][1];
if(xx<0||xx>=n||yy<0||yy>=m)continue;
if(vis[xx][yy])continue;
next.x=xx;next.y=yy;next.step=now.step+1;
vis[next.x][next.y]=true;
q.push(next);
}
}
return -1;
}
int main()
{
while(~scanf("%d %d\n",&n,&m))
{
init();
int result=bfs();
printf("%d\n",result);
}
return 0;
}
SPOJ SERGRID - Grid BFS的更多相关文章
- SPOJ SERGRID 【BFS】
思路: 在一个方向上走K步,基础BFS. 注意标记: 注意路径: PS:显著注释是记录路径. #include<bits/stdc++.h> using namespace std; co ...
- SPOJ - AMR11J ——(BFS)
The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson ...
- SPOJ LAS(BFS)题解
题目:VJ 思路: BFS+回溯,但是要剪枝,看了dalao的题解,超时+WA无数发,终于过了 #include<cstdio> #include<cstring> #incl ...
- SPOJ-Grid ,水广搜easy bfs
SERGRID - Grid 一个水广搜我竟然纠结了这么久,三天不练手生啊,况且我快三个月没练过搜索了... 题意:n*m的方格,每个格子有一个数,意味着在此方格上你可以上下左右移动s[x][y]个格 ...
- [LeetCode] Shortest Distance from All Buildings Solution
之前听朋友说LeetCode出了一道新题,但是一直在TLE,我就找时间做了一下.这题是一个比较典型的BFS的题目,自己匆忙写了一个答案,没有考虑优化的问题,应该是有更好的解法的. 原题如下: You ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- Robots on a grid(DP+bfs())
链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
- spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the ...
随机推荐
- 相对定位的div没有出现纵向滚动条
在一个相对定位的div中绝对定位很多html元素,纵向没有滚动条出现.原因:我们期望作为原点的点在窗口原点的下方,但是滚动条以窗口原点作为原点,所以没有出现滚动条.解决:在div外面再套一个div,o ...
- 对foreach循环的思考
阿里java开发手册已经发表,很多都值得认真研究思考,看到零度的思考题,没忍住研究了一下. 零度的思考:https://mp.weixin.qq.com/s/dDR21k30s6ZVfDvl8BVQm ...
- IIS下自定义错误页面配置的两种方式(亲测可行)--IIS服务器
网站自定义错误页面的设置,大家应该都知道它的重要性……不多说,下面带大家一步步在IIS下设置网站自定义错误页面…… 1.首先进入你的网站主页,找到[错误页](注意是IIS下的错误页不是.NET错误页) ...
- Python Web框架篇:Django cookie和session
part 1 概念 在Django里面,cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话. 两者最大的区别是cookie的信息是存放在浏览器客户端的,而sessio ...
- 【ThinkPHP框架学习 】(1) --- thinkphp 3.2.3 验证码验证使用教程分享
框架版本:ThinkPHP框架 thinkphp 3.2.3 生成验证码 下面是最简单的方式生成验证码: $Verify = new \Think\Verify(); $Verify-> ...
- jQuery基础应用
什么是 jQuery ? jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多"的JavaScript库. jQuery库包含以下功能: HT ...
- grunt concat针对有依赖文件的js脚本的合并
grunt concat针对有依赖文件的js脚本的合并: 在一个入口文件index.js里,有很多依赖文件,主要分两类,一类是和主文件同目录,另一类是其他目录下的js(cmd.非cmd的js文件,一般 ...
- geolocation h5
navigator. geolocation.getCurrentPosition() 触发浏览器弹窗询问用户同意访问地址.接收三个参数:成功回调函数,可选的失败回调,可选选项对象 成功回调函数接 ...
- font-size:100% 原因
The browser default which is something like 16pt for Firefox, You can check by going into Firefox op ...
- w3wp.exe已附加有调试器,但没有该调试器配置为调试此未经处理的异常,若要调试此异常,必须分离当前的调试器。
之前通过使用VS2010附加进程调试项目后,今天开机发现调试本机的项目报错如下图: 到网上到处查看无果,经过反复实验找到解决方法,我的项目是发布到IIS的 1.首先删除IIS上面的项目 2.在VS右击 ...