非常标准的BFS

第一次写错了很多

1、到达4时设置为墙就好了  避免了死循环

2、不用开d数组   在结构体里面就行了

3、结构体初始化函数的写法: Node(int x=0,int y=0,int oil=0):x(x),y(y),oil(oil){}

4、bfs的FOR里面的判断条件可以写的很清晰!就判断可以的  不可以的直接不处理!

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int sx,sy,ex,ey;
  4. int d[][],a[][],d2[][];
  5. bool f[][];
  6. int n,m;
  7. struct aa
  8. {
  9. int x;
  10. int y;
  11. int oil;
  12. aa(int x=,int y=,int oil=):x(x),y(y),oil(oil){}
  13. };
  14.  
  15. void bfs()
  16. {
  17.  
  18. const int dr[]={-,,,};
  19. const int dc[]={,,,-};
  20.  
  21. queue<aa>q;
  22. memset(d,-,sizeof(d));
  23.  
  24. memset(f,true,sizeof(f));
  25. aa u(sx,sy,);d[sx][sy]=;
  26. q.push(u);
  27. // printf("u:%d %d %d\n",u.x,u.y,u.oil);
  28. while(!q.empty())
  29. {
  30.  
  31. aa u=q.front();q.pop();
  32.  
  33. if(u.x==ex&&u.y==ey) {printf("%d\n",d[ex][ey]);return;}
  34. for(int i=;i<=;i++)
  35. {
  36. aa v(u.x+dr[i],u.y+dc[i],u.oil-);
  37. d[v.x][v.y]=d[u.x][u.y]+;
  38.  
  39. if(v.x<||v.x>n||v.y<||v.y>m) continue ;
  40. if(v.oil==)continue ;
  41. if(a[v.x][v.y]==){ v.oil=; a[v.x][v.y]=; q.push(v); }
  42. if(a[v.x][v.y]!=)
  43. {
  44. //printf("v:%d %d %d\n",v.x,v.y,v.oil);
  45. q.push(v);}
  46.  
  47. }
  48.  
  49. }
  50.  
  51. printf("-1\n");
  52. }
  53.  
  54. int main()
  55. {
  56. int cas;cin>>cas;
  57. while(cas--)
  58. {
  59.  
  60. cin>>n>>m;
  61. for(int i=;i<=n;i++)
  62. for(int j=;j<=m;j++)
  63. {
  64. scanf("%d",&a[i][j]);
  65. if(a[i][j]==){sx=i;sy=j;}
  66. if(a[i][j]==){ex=i;ey=j;}
  67. }
  68. bfs();
  69.  
  70. }
  71.  
  72. return ;
  73. }

第二次:简洁了许多

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int world[][];int sx,sy,ex,ey;int n,m;
  5.  
  6. struct node
  7. {
  8. int x,y,d,oil;
  9.  
  10. node(int x=,int y=,int d=,int oil=):x(x),y(y),d(d),oil(oil){}
  11. };
  12.  
  13. void bfs()
  14. {
  15. int dx[]={,,,-};
  16. int dy[]={,,-,};
  17.  
  18. node u(sx,sy,,);
  19. queue<node>q;
  20. q.push(u);
  21.  
  22. while(!q.empty())
  23. {
  24. node u=q.front();q.pop();
  25. if(u.x==ex&&u.y==ey){printf("%d\n",u.d);return;}
  26.  
  27. for(int i=;i<;i++)
  28. {
  29. node v(u.x+dx[i],u.y+dy[i],u.d+,u.oil-);
  30. if(v.x>=&&v.x<=n&&v.y>=&&v.y<=m&&v.oil)
  31. {
  32. if(world[v.x][v.y]==){v.oil=;world[v.x][v.y]=;q.push(v);}
  33.  
  34. else if(world[v.x][v.y]>) q.push(v);
  35. }
  36. }
  37. }
  38. printf("-1\n");
  39. }
  40.  
  41. int main()
  42. {
  43. int cas;cin>>cas;
  44. while(cas--)
  45. {
  46. cin>>n>>m;
  47. for(int i=;i<=n;i++)
  48. for(int j=;j<=m;j++)
  49. {scanf("%d",&world[i][j]);
  50. if(world[i][j]==){sx=i;sy=j;}
  51. if(world[i][j]==){ex=i;ey=j;}
  52.  
  53. }
  54.  
  55. bfs();
  56.  
  57. }
  58.  
  59. return ;
  60. }

大神的简洁代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <memory.h>
  5. #include <stdio.h>
  6. using namespace std;
  7. #define Size 8
  8. /*
  9. 这题目可以重复道路.只需要把控制炸弹的地方使用后,变成墙壁即可.
  10. */
  11. struct Node
  12. {
  13. int x;
  14. int y;
  15. int time;//time代表已用的时间.
  16. int rest;//rest代表剩余的时间.
  17. //按照时间从高到低排列.
  18. bool operator < (Node a) const
  19. {
  20. return this->time > a.time;
  21. }
  22. };
  23.  
  24. int world[Size][Size];
  25. int n, m;
  26. int temp;
  27. int dir[][] = { { , }, { , }, { -, }, { , - } };
  28. int sx, sy;
  29. int rx, ry;
  30. /*
  31. 0代表墙壁.1代表正常路.2代表起点.3代表终点.4代表炸弹控制器.
  32. */
  33. int bfs()
  34. {
  35. priority_queue<Node> temp;
  36. Node now, next, s;
  37. s.x = sx;
  38. s.y = sy;
  39. s.time = ;
  40. s.rest = ;
  41. temp.push(s);
  42.  
  43. while (!temp.empty())
  44. {
  45. now = temp.top();
  46. temp.pop();
  47.  
  48. if (now.x == rx && now.y == ry && now.rest > )
  49. {
  50. return now.time;
  51. }
  52. //减枝.当剩余时间为1时.还没找到出口,说明到不了了.
  53. if (now.rest == )
  54. continue;
  55.  
  56. for (int i = ; i < ; ++i)
  57. {
  58. next.x = now.x + dir[i][];
  59. next.y = now.y + dir[i][];
  60. next.time = now.time + ;
  61. next.rest = now.rest - ;
  62. //判断位置是否合理.
  63. if (next.x >= && next.y >= && next.x < n && next.y < m && world[next.x][next.y] != && next.rest >= )
  64. {
  65. //如果他到了炸弹这里.
  66. if (world[next.x][next.y] == )
  67. {
  68. next.rest = ;
  69. //改为墙壁即可.
  70. world[next.x][next.y] = ;
  71. }
  72. temp.push(next);
  73. }
  74. }
  75. }
  76. return -;
  77. }
  78.  
  79. int main()
  80. {
  81. int t;
  82. scanf("%d", &t);
  83. for (int i = ; i < t; ++i)
  84. {
  85. scanf("%d%d", &n,&m);
  86. memset(world, , sizeof(world));
  87. for (int j = ; j < n; ++j)
  88. {
  89. for (int k = ; k < m; ++k)
  90. {
  91. scanf("%d", &temp);
  92. //初始位置.
  93. if (temp == )
  94. {
  95. sx = j;
  96. sy = k;
  97. }
  98. //目标位置.
  99. else if (temp == )
  100. {
  101. rx = j;
  102. ry = k;
  103. }
  104. world[j][k] = temp;
  105. }
  106. }
  107. printf("%d\n", bfs());
  108. }
  109.  
  110. return ;
  111. }

Nightmare HDU1072的更多相关文章

  1. Nightmare(DFS)

    Nightmare    hdu1072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  3. hdu1072(Nightmare)bfs

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏

    Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...

  5. HDU1072:Nightmare [DFS]

    题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...

  6. HDU1072:Nightmare

    传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...

  7. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  8. Nightmare基于phantomjs的自动化测试套件

    今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API.其API以goto, re ...

  9. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

随机推荐

  1. 堆叠窗口QStackedWidget

    经常将QStackedWidget和QListWidget或者QListView搭配使用 import sys from PyQt5.QtWidgets import QApplication, QW ...

  2. 为小程序开发创建本地mock数据服务器

    开发时使用easy-mock的服务,不是大厂就不是大厂,实在恶心,每天都会有卡的这么一段时间 于是,自己建个本地mock服务算了,想使用express 但是必须把json数据里面的不同对象,分配到不同 ...

  3. try 、catch 、finally 、throw 测试js错误

    try语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块. finally 语句在 try 和 catch 之后无论有无异常都会执 ...

  4. Struts的JSON机制

    需要加入jar包 Struts的JSON帮助我们自动将对象解析为JSON对象,不用我门借助第三方进行JSON的解析 .具体的使用机制如下: 1.Action类 package StrutsJSON; ...

  5. SpringMVC使用Redis作为缓存提供者

    (1)pom添加依赖项 <dependency> <groupId>org.springframework.data</groupId> <artifactI ...

  6. 编码器AE & VAE

    学习总结于国立台湾大学 :李宏毅老师 自编码器 AE (Auto-encoder)    & 变分自动编码器VAE(Variational Auto-encoder)             ...

  7. python日志和异常

    “日志”转载:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html "异常"转载:http://www.cnb ...

  8. vim常用命令总结 (转)【转】

    转自:https://www.cnblogs.com/yangjig/p/6014198.html 在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的 ...

  9. 注解图Annotation

    该图来源于 竹子-博客(.NET/Java/Linux/架构/管理/敏捷) http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html,感 ...

  10. 通过使用CSS字体阴影效果解决hover图片时显示文字看不清的问题

    1.前言 最近需要加入一个小功能,在鼠标越过图片时,提示其大小和分辨率,而不想用增加属性title来提醒,不够好看.然而发现如果文字是一种颜色,然后总有概率碰到那张图上浮一层的文字会看不到,所以加入文 ...