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 ...
随机推荐
- yii2之数据验证
一.场景 什么情况下需要使用场景呢?当一个模型需要在不同情境中使用时,若不同情境下需要的数据表字段和数据验证规则有所 不同,则需要定义多个场景来区分不同使用情境.例如,用户注册的时候需要填写email ...
- 微信小程序获取微信的openid
通过登录接口获取登录凭证,然后通过request请求后台获取openid,需要把后台域名放到小程序后台的request 合法域名内: 1.wx.login 获取登录凭证 2.wx.request 发 ...
- angular 1.5.3各种模块使用(一)
1.angular cookie的用法:(1)引入angular-cookies(2)注入ngCookies这模块(3)想要更改cookies存储位置的话要添加内置服务$cookiesProvider ...
- 使用TP5创建一个REST API
原文在这里 : http://hmw.iteye.com/blog/1190827 tp自带的api,get请求接口 /** * 显示资源列表 * * @return \think\Response ...
- 谷歌浏览器Chrome播放rtsp视频流解决方案
找半天,HTML5的可以支持RTMP 但是无法播放RTSP,flash也止步于RTMP,最后同事推荐了个开源的好东东 VLC ,请教谷歌大神之后,这货果然可以用来让各浏览器(IE activex方式, ...
- ldap数据库--ODSEE--复制协议
简单介绍一下ODSEE的复制拓扑的建立,复制协议可以通过管理界面进行创建,也可以通过命令行创建.在此之前需要了解一些复制协议的相关概念,这里针对OESEE. 1,复制角色 master(提供者,也可以 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- 发放福利:原价135元/年的阿里云CDN流量包(500GB)免费送
不少朋友看过本站推荐的两篇文章:1. <阿里云全民云计算活动:云服务器ECS二折起>2. <阿里云双11优惠活动-爆款云服务器> 大部分人都说不错,很快下单购买了服务器,后续使 ...
- Lucene全文检索学习笔记
全文索引 介绍Lucene的作者:Lucene的贡献者Doug Cutting是 一位资深全文索引/检索专家,曾经是V-Twin搜索引擎(Apple的Copland操作系统的成就之一)的主要开发者,后 ...
- bzoj1001(对偶图最短路)
显然是个最大流问题. 边数达到了10^6级别,显然用dinic算法会TLE 对于一个平面图来说,当然用对偶图的最短路来求最小割(最大流) SPFA转移的时候注意判断边界情况 应该要开longlong才 ...