题目:http://poj.org/problem?id=3009

参考博客:http://www.cnblogs.com/LK1994/

  1. #include <iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<stack>
  6. #include<queue>
  7. #include<iomanip>
  8. #include<cmath>
  9. #include<map>
  10. #include<vector>
  11. #include<algorithm>
  12. using namespace std;
  13.  
  14. int w,h,G[][];
  15. int d[][]={{,},{,-},{-,},{,}};
  16. int minstep;
  17.  
  18. int dfs(int sx,int sy,int step)
  19. {
  20. if(step>)
  21. return step;
  22. for(int i=; i<; i++)
  23. for(int k=; k<; k++)
  24. {
  25. int tx=sx+d[i][]*k;
  26. int ty=sy+d[i][]*k;
  27. if(tx>=&&tx<=h&&ty>=&&ty<=w)
  28. {
  29. if(G[tx][ty]==) return step+;
  30. else if(G[tx][ty]==)
  31. {
  32. if(k==) break;
  33. int x=sx+d[i][]*(k-);
  34. int y=sy+d[i][]*(k-);
  35. G[tx][ty]=;
  36. minstep=min(minstep,dfs(x,y,step+));
  37. G[tx][ty]=;
  38. break;
  39. }
  40. }
  41. else break;
  42. }
  43. return minstep;
  44. }
  45. int main()
  46. {
  47. int sx,sy,ans;
  48. while(cin>>w>>h&&(w!=||h!=))
  49. {
  50. for(int i=; i<=h; i++)
  51. for(int j=; j<=w; j++)
  52. {
  53. cin>>G[i][j];
  54. if(G[i][j]==)
  55. {
  56. sx=i; sy=j;
  57. G[i][j]=;
  58. }
  59. }
  60. minstep=;
  61. ans=dfs(sx,sy,);
  62. if(ans>) ans=-;
  63. cout<<ans<<endl;
  64. }
  65. return ;
  66. }

poj 3009 Curling 2.0( dfs )的更多相关文章

  1. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  2. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  3. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  4. 【POJ】3009 Curling 2.0 ——DFS

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Desc ...

  5. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  6. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  7. poj 3009 Curling 2.0

    题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...

  8. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  9. POJ3009——Curling 2.0(DFS)

    Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...

随机推荐

  1. WeX5是主要进行app开发吗?能开发微信App吗?

    WeX5是一款html5开发工具,可以进行app开发,做出各种H5 App,同样也可以进行主要运行在PC的html5产品,. WeX5开发的应用,不仅可以在微信上运行,也可以直接手机浏览器运行,或者打 ...

  2. Keep two divs sync scroll and example

    srcDiv has visible horizontal scrollbar.(style="overflow:auto;") targetDiv has no scrollba ...

  3. webpack+react+jquery和jquery插件

    要引入jquery插件 全局引入jquery plugins : [new webpack.ProvidePlugin({ $: 'jquery', jQuery:'jquery' "win ...

  4. Linux学习1

    Linux中一切皆文件,且不依靠扩展名区分文件,学习Linux必须要熟悉在字符界面进行文件的管理. 首先是Linux的查询命令. (1)ls -a是显示当前目录所有文件,包含隐藏文件,如图中文件名前加 ...

  5. Sugarcrm Email Integration

    Introdurce http://www.sugarcrm.com/feature/email-integration Tutor http://www.youtube.com/watch?v=BQ ...

  6. linux whereis which

    whereis 命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s). [root@localhost ~]# whereis svn svn ...

  7. Qt单元测试

    单元测试之作用要完成测试用例,保证设计上的耦合依赖通过测试用例,保证覆盖率,提高程序质量 QTest一些有用的静态函数QTest::qExecQTest::qSleepQTest::qWait   例 ...

  8. ios app - 打开系统设置URL

    ios --- 调用系统"设置"里的功能(转) 安装后第一次运行软件时,系统会弹出提示用户是否允许软件获取当前位置,如果用户不允许的话,之后运行时系统不会在弹出提示设置,这点很不方 ...

  9. 简单3d RPG游戏 之 004 攻击(二)

    人物和怪物的攻击都有CD冷却,在PlayerAttack脚本中添加成员 //冷却倒计时 public float attackTimer; //CD冷却时间 public float coolDown ...

  10. JS中遍历普通数组和字典数组的区别

    // 普通数组 var intArray = new Array(); intArray[0] = "第一个"; intArray[1] = "第二个"; fo ...