题目传送门

  1. /*
  2. 题意:问一个点到另一个点的最少转向次数。
  3. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS
  4. 关键理解坐标离散化,BFS部分可参考HDOJ_1728
  5. */
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <queue>
  10. #include <vector>
  11. #include <map>
  12. #include <cmath>
  13. using namespace std;
  14. const int MAXN = * + ;
  15. const int INF = 0x3f3f3f3f;
  16. vector<int> nx, ny;
  17. map<int, int> mx, my;
  18. struct Point
  19. {
  20. int x, y, t, d;
  21. Point (int _x = , int _y = ) {x = _x, y = _y, t = ;}
  22. void read(void)
  23. {
  24. scanf ("%d%d", &x, &y);
  25. nx.push_back (x); ny.push_back (y);
  26. }
  27. void updata(void) {x = mx[x]; y = my[y];}
  28. bool operator < (const Point &r) const {return t > r.t;}
  29. }s, e, p[][];
  30. bool maze[*MAXN][*MAXN];
  31. int dp[MAXN][MAXN][];
  32. bool can[MAXN][MAXN][];
  33. int dx[] = {, , , -};
  34. int dy[] = {, , -, };
  35. int w;
  36. int compress(vector<int> &x, map<int, int> &mp)
  37. {
  38. vector<int> xs;
  39. sort (x.begin (), x.end ());
  40. x.erase (unique (x.begin (), x.end ()), x.end ());
  41. for (int i=; i<x.size (); ++i)
  42. {
  43. for (int d=-; d<=; ++d) xs.push_back (x[i] + d);
  44. }
  45. sort (xs.begin (), xs.end ());
  46. xs.erase (unique (xs.begin (), xs.end ()), xs.end ());
  47. for (int i=; i<xs.size (); ++i) mp[x[i]] = find (xs.begin (), xs.end (), x[i]) - xs.begin ();
  48. return xs.size ();
  49. }
  50. bool check(Point &a)
  51. {
  52. if ( <= a.x && a.x <= w && <= a.y && a.y <= w && dp[a.x][a.y][a.d] > a.t)
  53. {
  54. dp[a.x][a.y][a.d] = a.t;
  55. return true;
  56. }
  57. return false;
  58. }
  59. int BFS(void)
  60. {
  61. memset (dp, INF, sizeof (dp));
  62. priority_queue<Point> Q; s.t = ;
  63. for (s.d=; s.d<; ++s.d)
  64. {
  65. Q.push (s); dp[s.x][s.y][s.d] = ;
  66. }
  67. while (!Q.empty ())
  68. {
  69. Point now = Q.top (); Q.pop ();
  70. if (dp[now.x][now.y][now.d] < now.t) continue;
  71. if (now.x == e.x && now.y == e.y) return now.t;
  72. for (int d=-; d<=; ++d)
  73. {
  74. Point to = now;
  75. to.d = (to.d + + d) % ;
  76. if (!can[to.x][to.y][to.d]) continue;
  77. int x = * to.x, y = * to.y;
  78. if (maze[x][y] && maze[x+][y+] &&
  79. ((now.d % == && d != ) || (now.d % == && d != -))) continue;
  80. if (maze[x+][y] && maze[x][y+] &&
  81. ((now.d % == && d != -) || (now.d % == && d != ))) continue;
  82. if (d != ) to.t++;
  83. to.x += dx[to.d]; to.y += dy[to.d];
  84. if (check (to)) Q.push (to);
  85. }
  86. }
  87. return -;
  88. }
  89. int main(void) //HDOJ 4444 Walk
  90. {
  91. // freopen ("C.in", "r", stdin);
  92. while (true)
  93. {
  94. nx.clear (); ny.clear (); mx.clear (); my.clear ();
  95. s.read (); e.read ();
  96. if (!s.x && !s.y && !e.x && !e.y) break;
  97. memset (maze, false, sizeof (maze));
  98. int n; scanf ("%d", &n);
  99. for (int i=; i<=n; ++i)
  100. {
  101. Point *t = p[i];
  102. t[].read (); t[].read ();
  103. if (t[].x > t[].x) swap (t[], t[]);
  104. if (t[].y > t[].y)
  105. {
  106. Point a = t[], b = t[];
  107. t[].y = b.y; t[].y = a.y;
  108. }
  109. t[] = (Point) {t[].x, t[].y};
  110. t[] = (Point) {t[].x, t[].y};
  111. }
  112. w = max (compress (nx, mx), compress (ny, my));
  113. s.updata (); e.updata ();
  114. for (int i=; i<=n; ++i)
  115. {
  116. Point *t = p[i];
  117. for (int j=; j<; ++j) t[j].updata ();
  118. for (int j=; j<; ++j) t[j] = Point (*t[j].x, *t[j].y);
  119. for (int j=t[].x+; j<=t[].x; ++j)
  120. {
  121. for (int k=t[].y+; k<=t[].y; ++k) maze[j][k] = true; //离散化后将矩形涂黑
  122. }
  123. }
  124. memset (can, true, sizeof (can));
  125. for (int i=; i<w; ++i)
  126. {
  127. for (int j=; j<w; ++j)
  128. {
  129. int x = i * , y = j * ;
  130. bool *d = can[i][j];
  131. if (maze[x][y+] && maze[x+][y+]) d[] = false; //判断4个方向能不能走
  132. if (maze[x+][y] && maze[x+][y+]) d[] = false;
  133. if (maze[x][y] && maze[x+][y]) d[] = false;
  134. if (maze[x][y] && maze[x][y+]) d[] = false;
  135. }
  136. }
  137. printf ("%d\n", BFS ());
  138. }
  139. return ;
  140. }

离散化+BFS HDOJ 4444 Walk的更多相关文章

  1. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)

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

  4. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  5. Codeforces243C-Colorado Potato Beetle(离散化+bfs)

    Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. Th ...

  6. BFS HDOJ 1728 逃离迷宫

    题目传送门 /* BFS:三维BFS,加上方向.用dp[x][y][d]记录当前需要的最少转向数 */ #include <cstdio> #include <algorithm&g ...

  7. BFS HDOJ 2102 A计划

    题目传送门 题意:中文题面 分析:双层BFS,之前写过类似的题.总结坑点: 1.步数小于等于T都是YES 2. 传送门的另一侧还是传送门或者墙都会死 3. 走到传送门也需要一步 #include &l ...

  8. hdoj 5335 Walk Out

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...

  9. hdu 4400 Mines(离散化+bfs+枚举)

    Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...

随机推荐

  1. 移动端click事件延迟300ms该如何解决

    window.addEventListener( "load", function() {     FastClick.attach( document.body ); }, fa ...

  2. jQuery插件之ajaxFileUpload(ajax文件上传)

    一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...

  3. hunnu - 11545 小明的烦恼——找路径 (最大流)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11545 只是要求不经过相同的边,那么每次找出一条增广路T-- ...

  4. Delphi第三方控件安装方式

    由于组件提供的方式不同,所以安装的方法也是不一样的,下面就目前常见的各种形式的组      件的安装方法介绍一下.             1只有一个DCU文件的组件.DCU文件是编译好的单元文件,这 ...

  5. [bzoj1895][Pku3580]supermemo_非旋转Treap

    supermemo bzoj-1895 Pku-3580 题目大意:给定一个n个数的序列,需支持:区间加,区间翻转,区间平移,单点插入,单点删除,查询区间最小值. 注释:$1\le n\le 6.1\ ...

  6. HDU——1285 确定比赛名次

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. 创建简单的spring-mvc项目

    1.第一步:创建项目 new—>Dynamic Web Project 项目创建成功后,展示如图: 2.第二步:导入springmvc的jar包和common-logging.jar 3.第三步 ...

  8. [52ABP系列] - 001、SPA免费项目模版搭建教程

    前言 这个项目是基于 ABP ASPNetCore 免费版,整合 NG-Alian 和 NG-Zorro 的项目,所以比较适合熟悉 ABP 和 Angular2+ 的开发人员, 如果你是新手,学习的话 ...

  9. 使用RoboCopy 命令[转载]

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  10. {head first} --- networking 1

    Head first系列的书确实非常好,深入浅出解说网络的组成.让曾经那些生涩的概念生动起来. Chapter 1 维修物理网络 CAT5电缆: 两端为RJ-45接头(水晶头).内部为UTP(非屏蔽双 ...