SERGRID - Grid

no tags 

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的更多相关文章

  1. SPOJ SERGRID 【BFS】

    思路: 在一个方向上走K步,基础BFS. 注意标记: 注意路径: PS:显著注释是记录路径. #include<bits/stdc++.h> using namespace std; co ...

  2. SPOJ - AMR11J ——(BFS)

    The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson ...

  3. SPOJ LAS(BFS)题解

    题目:VJ 思路: BFS+回溯,但是要剪枝,看了dalao的题解,超时+WA无数发,终于过了 #include<cstdio> #include<cstring> #incl ...

  4. SPOJ-Grid ,水广搜easy bfs

    SERGRID - Grid 一个水广搜我竟然纠结了这么久,三天不练手生啊,况且我快三个月没练过搜索了... 题意:n*m的方格,每个格子有一个数,意味着在此方格上你可以上下左右移动s[x][y]个格 ...

  5. [LeetCode] Shortest Distance from All Buildings Solution

    之前听朋友说LeetCode出了一道新题,但是一直在TLE,我就找时间做了一下.这题是一个比较典型的BFS的题目,自己匆忙写了一个答案,没有考虑优化的问题,应该是有更好的解法的. 原题如下: You ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. mysql中多个left join子查询写法以及别名用法

    不多说 直接上语句   SELECT     a.id,     a.thumbNail,     a. NAME,     a.marketPrice,     a.memberPrice,     ...

  2. 重温C语言小感

    这周对我感触比较大的就是重温了下C语言,当然重点还是放到了指针那块,一看到指针就想到了链表,还有那个 指针申明,“函数指针”,“指针函数”, “使用指针实现数组降维数”,还有就是大学初学编程的点滴. ...

  3. .13-Vue源码之patch(3)(终于完事)

    怎么感觉遥遥无期了呀~这个源码,跑不完了. 这个系列写的不好,仅作为一个记录,善始善终,反正也没人看,写着玩吧! 接着上一节的cbs,这个对象在初始化应该只会调用create模块数组方法,简单回顾一下 ...

  4. Linux学习(十四)磁盘格式化、磁盘挂载、手动增加swap空间

    一.磁盘格式化 分好去的磁盘需要格式化之后才可以使用.磁盘分区一般用mke2fs命令或者mkfs.filesystemtype.这个filesystemtype分为ext4,ext3,xfs等等.xf ...

  5. 一个强大的封装好的pdo处理类

    php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了.不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类. ...

  6. Restaurant

    Restaurant Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. 哈尔滨理工大学第六届程序设计团队 I-Team

    /* 以前做过一个插队的题,这个类似从后往前操作 */ #include <iostream> #include <stdio.h> #include <algorith ...

  8. 对比jquery获取属性的方法props、attr、data

    1.attr,prop 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法.对于自定义的属性是取不到的: 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. 2.a ...

  9. css之定位(position)

    1.什么是定位: css中的position属性,position有四个值:absolute/relative/fixed/static(绝对/相对/固定/静态(默认))通过定位属性可以设置一些不规则 ...

  10. 开发一个基于 Android系统车载智能APP

    很久之前就想做一个车载相关的app.需要实现如下功能: (1)每0.2秒更新一次当前车辆的最新速度值. (2)可控制性记录行驶里程. (3)不连接网络情况下获取当前车辆位置.如(北京市X区X路X号) ...