题目链接:http://codeforces.com/problemset/problem/793/B

题目大意:告诉你起点和终点,要求你在只能转弯两次的情况下能不能到达终点。能就输出“YES”,不能就输出“NO”。

解题思路:这算是经典的转弯题了,接近半年没写搜索题了,,所以今天先拿这道题开刀吧。这个题关键就是“判重”,如何记录走过的点。我们可以开个三维数组,记录各个坐标各个方向上的转弯数,如果下次在到这个点这个方向,那就比较转弯数,如果这次转弯数大于等于已经记录的转弯数那就不用再找下去了,因为这次能走到的地方,上次肯定也能走到。估计一下时间复杂度大概为O(4*3n)。

dfs:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. const int N=1e3+;
  5. int m,n;
  6. bool flag=false;
  7. char map[N][N];
  8. int vis[N][N][];//*关键*用来标记走过的点,记录该点朝着各方向转弯数
  9. int d[][]={{,},{-,},{,},{,-}};
  10.  
  11. void dfs(int x,int y,int dir,int cnt){
  12. if(x<=||x>m||y<=||y>n||cnt>)
  13. return;
  14. if(vis[x][y][dir]<=cnt)//如果这个位置这个方向已经走过,且用了更小的转弯数,那就不用再走这个点了
  15. return;
  16. if(map[x][y]=='T'){
  17. flag=true;
  18. return;
  19. }
  20. if(map[x][y]!='.'&&map[x][y]!='S')
  21. return;
  22. vis[x][y][dir]=cnt;
  23. for(int i=;i<;i++) {
  24. int x1=x+d[i][];
  25. int y1=y+d[i][];
  26. if(dir==-)
  27. dfs(x1,y1,i,cnt);
  28. else if(dir!=i)
  29. dfs(x1,y1,i,cnt+);
  30. else
  31. dfs(x1,y1,i,cnt);
  32. }
  33. }
  34.  
  35. int main(){
  36. int index,indey;
  37. scanf("%d %d",&m,&n);
  38. getchar();
  39. for(int i=;i<=m;i++){
  40. for(int j=;j<=n;j++){
  41. scanf("%c",&map[i][j]);
  42. if(map[i][j]=='S'){
  43. index=i;
  44. indey=j;
  45. }
  46. }
  47. getchar();
  48. }
  49. memset(vis,0x3f,sizeof(vis));//转弯数初始化为无限大
  50. dfs(index,indey,-,);//-1表示开始位置没有方向
  51. if(flag)
  52. printf("YES\n");
  53. else
  54. printf("NO\n");
  55. }

bfs,跟上面差不多的:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int N=1e3+;
  6. int vis[N][N][];
  7. int d[][]={{,},{-,},{,},{,-}};
  8. char map[N][N];
  9. int m,n;
  10. bool flag=false;
  11.  
  12. struct node{
  13. int x,y,dir,cnt;
  14. }now,t,pre;
  15. //int num=0;
  16. void bfs(int stax,int stay){
  17. queue<node>q;
  18. t.x=stax;
  19. t.y=stay;
  20. t.dir=-;
  21. t.cnt=;
  22. q.push(t);
  23. while(!q.empty()){
  24. pre=q.front();
  25. q.pop();
  26. for(int i=;i<;i++){
  27. int x=pre.x+d[i][];
  28. int y=pre.y+d[i][];
  29. int cnt;
  30. if(pre.dir==-)//判断一下上次方向和当前要走的方向的关系
  31. cnt=;
  32. else if(pre.dir==i)
  33. cnt=pre.cnt;
  34. else if(pre.dir!=i)
  35. cnt=pre.cnt+;
  36. if(x<=||x>m||y<=||y>n||cnt>)
  37. continue;
  38. if(map[x][y]=='T'){//到达终点
  39. flag=true;
  40. return;
  41. }
  42. if(map[x][y]!='S'&&map[x][y]!='.')
  43. continue;
  44. if(vis[x][y][i]<=cnt)//这个点这个方向已经有更优方案了
  45. continue;
  46. vis[x][y][i]=cnt;
  47. now.x=x;
  48. now.y=y;
  49. now.dir=i;
  50. now.cnt=cnt;
  51. // num++;
  52. // printf("%d\n",num);
  53. q.push(now);
  54. }
  55. }
  56. }
  57. int main(){
  58. int index,indey;
  59. scanf("%d %d",&m,&n);
  60. getchar();
  61. for(int i=;i<=m;i++){
  62. for(int j=;j<=n;j++){
  63. scanf("%c",&map[i][j]);
  64. if(map[i][j]=='S'){
  65. index=i;
  66. indey=j;
  67. }
  68. }
  69. getchar();
  70. }
  71. memset(vis,0x3f,sizeof(vis));
  72. bfs(index,indey);
  73. if(flag)
  74. printf("YES\n");
  75. else
  76. printf("NO\n");
  77. }

codeforces 793B - Igor and his way to work(dfs、bfs)的更多相关文章

  1. codeforces 793B. Igor and his way to work

    B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...

  2. Codeforces Beta Round #94 div 2 C Statues dfs或者bfs

    C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)

    题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...

  4. codeforces793 B. Igor and his way to work (dfs)

    题目链接:codeforces793 B. Igor and his way to work (dfs) 求从起点到终点转方向不超过两次是否有解,,好水啊,感觉自己代码好搓.. #include< ...

  5. 【codeforces 793B】Igor and his way to work

    [题目链接]:http://codeforces.com/contest/793/problem/B [题意] 给一个n*m大小的方格; 有一些方格可以走,一些不能走; 然后问你从起点到终点,能不能在 ...

  6. codeforces 598D Igor In the Museum

    题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...

  7. Codeforces 747F Igor and Interesting Numbers DP 组合数

    题意:给你一个数n和t,问字母出现次数不超过t,第n小的16进制数是多少. 思路:容易联想到数位DP, 然而并不是...我们需要知道有多少位,在知道有多少位之后,用试填法找出答案.我们设dp[i][j ...

  8. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  9. Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs

    B. Preparing Olympiad Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550 ...

随机推荐

  1. 【JVM.2】垃圾收集器与内存分配策略

    垃圾收集器需要完成的3件事情: 哪些内存需要回收? 什么时候回收? 如何回收? 在前一节中介绍了java内存运行时区域的各个部分,其中程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭:栈 ...

  2. item 2: 理解auto类型的推导

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果你已经读过item 1的模板类型推导,你已经知道大部分关于au ...

  3. 【下一代核心技术DevOps】:(三)私有代码库阿里云Git使用

    1. 引言 使用DevOps肯定离不开和代码的集成.所以要想跑通整套流程,代码库的选型也是非常重要的.否则无法实现持续集成.目前比较常用的代码管理有SVN和GIt 如果还使用SVN的,建议尽早迁移到G ...

  4. C语言----数据类型(基础篇一)

    C语言的入门程序模板 #include <stdio.h> /*使用或者包含系统里面的程序*/ main() /*程序入口点*/ { /*起点*/ +; /*叫计算机执行的指令*/ } / ...

  5. eclipse添加maven环境

    一.打开eclipse,选择Window->preference,如下图所示 二.Maven-> installation->add,见下图: 三.选择Directory,选择mav ...

  6. bash处理一条命令的步骤

    Shell执行一条命令步骤 参考链接: <Learning the bash Shell, 3rd Edition  -- 7.3. Command-Line Processing> &l ...

  7. 第二个spring

    由于第一个spring已经完成,我们现在进去第二个spring! 陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法   plan好布局,分配任务,控制时间!

  8. HDOJ2010_水仙花数

    一道水题.一直出现Output Limit Exceeded的原因是在while循环中没有终止条件的时候会自动判断并报错,写的时候忘记加!=EOF结束标识了. HDOJ2010_水仙花数 #inclu ...

  9. octave基本指令5

    octave基本指令5 控制语句 for >> v=zreos(5,1) v = 0 0 0 0 0 >> for i=1:10, > v(i) = 2^i; > ...

  10. Aspose for Maven 使用

    https://blog.aspose.com/2014/08/12/aspose-for-maven-aspose-cloud-maven-repository/ https://marketpla ...