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

  1. Input:
  2. 5 4
  3. 2120
  4. 1203
  5. 3113
  6. 1120
  7. 1110
  1. Output:
  2. 6

思路:恰好能够跳Map[i][j]步,dfs超时,改用bfs

代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<queue>
  7. #include<algorithm>
  8. using namespace std;
  9. const int INF=0X3F3F3F3F;
  10. const int MAXN=550;
  11. int Map[MAXN][MAXN];
  12. bool vis[MAXN][MAXN];
  13. int n,m;
  14. struct node
  15. {
  16. int x,y,step;
  17. node(){x=y=step=0;}
  18. };
  19. void init()
  20. {
  21. memset(Map,0,sizeof(Map));
  22. memset(vis,false,sizeof(vis));
  23. char s[m+2];
  24. for(int i=0;i<n;i++)
  25. {
  26. scanf("%s",s);
  27. for(int j=0;j<m;j++)Map[i][j]=s[j]-'0';
  28. }
  29. }
  30. int bfs()
  31. {
  32. node now,next;
  33. queue<node> q;
  34. now.x=now.y=now.step=0;
  35. vis[0][0]=true;
  36. q.push(now);
  37. while(!q.empty())
  38. {
  39. now=q.front();q.pop();
  40. if(now.x==n-1&&now.y==m-1)return now.step;
  41. 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}};
  42. for(int i=0;i<4;i++)
  43. {
  44. int xx=now.x+dir[i][0],yy=now.y+dir[i][1];
  45. if(xx<0||xx>=n||yy<0||yy>=m)continue;
  46. if(vis[xx][yy])continue;
  47. next.x=xx;next.y=yy;next.step=now.step+1;
  48. vis[next.x][next.y]=true;
  49. q.push(next);
  50. }
  51. }
  52. return -1;
  53. }
  54. int main()
  55. {
  56. while(~scanf("%d %d\n",&n,&m))
  57. {
  58. init();
  59. int result=bfs();
  60. printf("%d\n",result);
  61. }
  62. return 0;
  63. }

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. swift之函数式编程(二)

    本文的主要内容来自<Functional Programming in Swift>这本书,有点所谓的观后总结 在本书的Introduction章中: we will try to foc ...

  2. 小程序组件之picker和range-key的用法

        因为在微信小程序的官网上并没有range-key的例子以及实际用法,所以好多人不知道具体如何使用.然后我在这里对其进行一个简单的实现,并记录一些注意事项. 以下是官网给的说明:   具体的用法 ...

  3. 移动端车牌识别sdk开发包(可下载)

    移动端车牌识别是一项基于OCR识别的应用技术.移动端车牌识别过程主要包含五个步骤,其中包括图像采集.图像预处理.车牌定位.字符分割.字符识别.输出结果等一系列计算机算法运算, 第一步[图像采集]:此步 ...

  4. ligerUI---ligerGrid中treegrid(表格树)的使用

    写在前面: 表格树是在普通ligerGrid的基础上,做了一点改变,使数据以表格树的形式显示出来,适用于有级别的数据比如菜单(有父菜单,父菜单下面有子菜单).表格树的显示有两种方法,可以根据自己的项目 ...

  5. spa(单页应用)中,使用history模式时,微信长按识别二维码在ios下失效的问题

    spa(单页应用,vue)中,使用history模式时,微信长按识别二维码在ios下失效的问题. 触发条件: spa单页应用: 路由模式 history 从其他页面跳转到带有微信二维码识别的页面(不是 ...

  6. C#递归查询

    一.sql --构造测试数据: 只作演示用 CREATE TABLE [dbo].[Tim_LinqTable]( [Id] int PRIMARY KEY IDENTITY(1,1) NOT NUL ...

  7. JavaScript系列----作用域链和闭包

    1.作用域链 1.1.什么是作用域 谈起作用域链,我们就不得不从作用域开始谈起.因为所谓的作用域链就是由多个作用域组成的.那么, 什么是作用域呢? 1.1.1作用域是一个函数在执行时期的执行环境. 每 ...

  8. 零基础如何迅速学习HTML5?新手小白学习web前端H5自白!

    很多的人在毕业之后才发现原来学的专业不是自己想做的工作,或者专业对口的工作待遇让人觉得并不满意,于是很多人选择培训机构学新的一门技能转换行业.IT行业的web前端H5受到很多学员的青睐.那么学习web ...

  9. IntelliJ配置jenkins服务的Crumb Data

    近期在做jenkins测试,IntelliJ并没有自动安装jenkins服务器,因此需要自己添加,但是如果不配置Crumb Data,jenkins的服务就不能使用. 首先在服务器中开启CSRF服务, ...

  10. Python 之 hello world

    写好的内容不小心关机都没了...奈何..重写一遍吧... 本机环境 : windows7 sp1 64位 企业版,python3.6 一:安装与配置 1:首先大胆的下载python,新入门的建议下载3 ...