离散化+BFS HDOJ 4444 Walk
- /*
- 题意:问一个点到另一个点的最少转向次数。
- 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS
- 关键理解坐标离散化,BFS部分可参考HDOJ_1728
- */
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- #include <vector>
- #include <map>
- #include <cmath>
- using namespace std;
- const int MAXN = * + ;
- const int INF = 0x3f3f3f3f;
- vector<int> nx, ny;
- map<int, int> mx, my;
- struct Point
- {
- int x, y, t, d;
- Point (int _x = , int _y = ) {x = _x, y = _y, t = ;}
- void read(void)
- {
- scanf ("%d%d", &x, &y);
- nx.push_back (x); ny.push_back (y);
- }
- void updata(void) {x = mx[x]; y = my[y];}
- bool operator < (const Point &r) const {return t > r.t;}
- }s, e, p[][];
- bool maze[*MAXN][*MAXN];
- int dp[MAXN][MAXN][];
- bool can[MAXN][MAXN][];
- int dx[] = {, , , -};
- int dy[] = {, , -, };
- int w;
- int compress(vector<int> &x, map<int, int> &mp)
- {
- vector<int> xs;
- sort (x.begin (), x.end ());
- x.erase (unique (x.begin (), x.end ()), x.end ());
- for (int i=; i<x.size (); ++i)
- {
- for (int d=-; d<=; ++d) xs.push_back (x[i] + d);
- }
- sort (xs.begin (), xs.end ());
- xs.erase (unique (xs.begin (), xs.end ()), xs.end ());
- for (int i=; i<xs.size (); ++i) mp[x[i]] = find (xs.begin (), xs.end (), x[i]) - xs.begin ();
- return xs.size ();
- }
- bool check(Point &a)
- {
- if ( <= a.x && a.x <= w && <= a.y && a.y <= w && dp[a.x][a.y][a.d] > a.t)
- {
- dp[a.x][a.y][a.d] = a.t;
- return true;
- }
- return false;
- }
- int BFS(void)
- {
- memset (dp, INF, sizeof (dp));
- priority_queue<Point> Q; s.t = ;
- for (s.d=; s.d<; ++s.d)
- {
- Q.push (s); dp[s.x][s.y][s.d] = ;
- }
- while (!Q.empty ())
- {
- Point now = Q.top (); Q.pop ();
- if (dp[now.x][now.y][now.d] < now.t) continue;
- if (now.x == e.x && now.y == e.y) return now.t;
- for (int d=-; d<=; ++d)
- {
- Point to = now;
- to.d = (to.d + + d) % ;
- if (!can[to.x][to.y][to.d]) continue;
- int x = * to.x, y = * to.y;
- if (maze[x][y] && maze[x+][y+] &&
- ((now.d % == && d != ) || (now.d % == && d != -))) continue;
- if (maze[x+][y] && maze[x][y+] &&
- ((now.d % == && d != -) || (now.d % == && d != ))) continue;
- if (d != ) to.t++;
- to.x += dx[to.d]; to.y += dy[to.d];
- if (check (to)) Q.push (to);
- }
- }
- return -;
- }
- int main(void) //HDOJ 4444 Walk
- {
- // freopen ("C.in", "r", stdin);
- while (true)
- {
- nx.clear (); ny.clear (); mx.clear (); my.clear ();
- s.read (); e.read ();
- if (!s.x && !s.y && !e.x && !e.y) break;
- memset (maze, false, sizeof (maze));
- int n; scanf ("%d", &n);
- for (int i=; i<=n; ++i)
- {
- Point *t = p[i];
- t[].read (); t[].read ();
- if (t[].x > t[].x) swap (t[], t[]);
- if (t[].y > t[].y)
- {
- Point a = t[], b = t[];
- t[].y = b.y; t[].y = a.y;
- }
- t[] = (Point) {t[].x, t[].y};
- t[] = (Point) {t[].x, t[].y};
- }
- w = max (compress (nx, mx), compress (ny, my));
- s.updata (); e.updata ();
- for (int i=; i<=n; ++i)
- {
- Point *t = p[i];
- for (int j=; j<; ++j) t[j].updata ();
- for (int j=; j<; ++j) t[j] = Point (*t[j].x, *t[j].y);
- for (int j=t[].x+; j<=t[].x; ++j)
- {
- for (int k=t[].y+; k<=t[].y; ++k) maze[j][k] = true; //离散化后将矩形涂黑
- }
- }
- memset (can, true, sizeof (can));
- for (int i=; i<w; ++i)
- {
- for (int j=; j<w; ++j)
- {
- int x = i * , y = j * ;
- bool *d = can[i][j];
- if (maze[x][y+] && maze[x+][y+]) d[] = false; //判断4个方向能不能走
- if (maze[x+][y] && maze[x+][y+]) d[] = false;
- if (maze[x][y] && maze[x+][y]) d[] = false;
- if (maze[x][y] && maze[x][y+]) d[] = false;
- }
- }
- printf ("%d\n", BFS ());
- }
- return ;
- }
离散化+BFS HDOJ 4444 Walk的更多相关文章
- BFS+贪心 HDOJ 5335 Walk Out
题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- Codeforces243C-Colorado Potato Beetle(离散化+bfs)
Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. Th ...
- BFS HDOJ 1728 逃离迷宫
题目传送门 /* BFS:三维BFS,加上方向.用dp[x][y][d]记录当前需要的最少转向数 */ #include <cstdio> #include <algorithm&g ...
- BFS HDOJ 2102 A计划
题目传送门 题意:中文题面 分析:双层BFS,之前写过类似的题.总结坑点: 1.步数小于等于T都是YES 2. 传送门的另一侧还是传送门或者墙都会死 3. 走到传送门也需要一步 #include &l ...
- hdoj 5335 Walk Out
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...
- hdu 4400 Mines(离散化+bfs+枚举)
Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...
随机推荐
- 移动端click事件延迟300ms该如何解决
window.addEventListener( "load", function() { FastClick.attach( document.body ); }, fa ...
- jQuery插件之ajaxFileUpload(ajax文件上传)
一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...
- hunnu - 11545 小明的烦恼——找路径 (最大流)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11545 只是要求不经过相同的边,那么每次找出一条增广路T-- ...
- Delphi第三方控件安装方式
由于组件提供的方式不同,所以安装的方法也是不一样的,下面就目前常见的各种形式的组 件的安装方法介绍一下. 1只有一个DCU文件的组件.DCU文件是编译好的单元文件,这 ...
- [bzoj1895][Pku3580]supermemo_非旋转Treap
supermemo bzoj-1895 Pku-3580 题目大意:给定一个n个数的序列,需支持:区间加,区间翻转,区间平移,单点插入,单点删除,查询区间最小值. 注释:$1\le n\le 6.1\ ...
- HDU——1285 确定比赛名次
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 创建简单的spring-mvc项目
1.第一步:创建项目 new—>Dynamic Web Project 项目创建成功后,展示如图: 2.第二步:导入springmvc的jar包和common-logging.jar 3.第三步 ...
- [52ABP系列] - 001、SPA免费项目模版搭建教程
前言 这个项目是基于 ABP ASPNetCore 免费版,整合 NG-Alian 和 NG-Zorro 的项目,所以比较适合熟悉 ABP 和 Angular2+ 的开发人员, 如果你是新手,学习的话 ...
- 使用RoboCopy 命令[转载]
经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...
- {head first} --- networking 1
Head first系列的书确实非常好,深入浅出解说网络的组成.让曾经那些生涩的概念生动起来. Chapter 1 维修物理网络 CAT5电缆: 两端为RJ-45接头(水晶头).内部为UTP(非屏蔽双 ...