离散化+BFS HDOJ 4444 Walk

news/2025/5/31 23:39:40

 

题目传送门

  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 
 15 const int MAXN = 8 * 50 + 100;
 16 const int INF = 0x3f3f3f3f;
 17 vector<int> nx, ny;
 18 map<int, int> mx, my;
 19 struct Point
 20 {
 21     int x, y, t, d;
 22     Point (int _x = 0, int _y = 0)    {x = _x, y = _y, t = 0;}
 23     void read(void)
 24     {
 25         scanf ("%d%d", &x, &y);
 26         nx.push_back (x);    ny.push_back (y);
 27     }
 28     void updata(void) {x = mx[x];    y = my[y];}
 29     bool operator < (const Point &r) const    {return t > r.t;}
 30 }s, e, p[55][4];
 31 bool maze[2*MAXN][2*MAXN];
 32 int dp[MAXN][MAXN][4];
 33 bool can[MAXN][MAXN][4];
 34 int dx[4] = {0, 1, 0, -1};
 35 int dy[4] = {1, 0, -1, 0};
 36 int w;
 37 
 38 int compress(vector<int> &x, map<int, int> &mp)
 39 {
 40     vector<int> xs;
 41     sort (x.begin (), x.end ());
 42     x.erase (unique (x.begin (), x.end ()), x.end ());
 43     for (int i=0; i<x.size (); ++i)
 44     {
 45         for (int d=-1; d<=1; ++d)    xs.push_back (x[i] + d);
 46     }
 47     sort (xs.begin (), xs.end ());
 48     xs.erase (unique (xs.begin (), xs.end ()), xs.end ());
 49     for (int i=0; i<xs.size (); ++i)    mp[x[i]] = find (xs.begin (), xs.end (), x[i]) - xs.begin ();
 50     return xs.size ();
 51 }
 52 
 53 bool check(Point &a)
 54 {
 55     if (0 <= a.x && a.x <= w && 0 <= a.y && a.y <= w && dp[a.x][a.y][a.d] > a.t)
 56     {
 57         dp[a.x][a.y][a.d] = a.t;
 58         return true;
 59     }
 60     return false;
 61 }
 62 
 63 int BFS(void)
 64 {
 65     memset (dp, INF, sizeof (dp));
 66     priority_queue<Point> Q;    s.t = 0;
 67     for (s.d=0; s.d<4; ++s.d)
 68     {
 69         Q.push (s);    dp[s.x][s.y][s.d] = 0;
 70     }
 71 
 72     while (!Q.empty ())
 73     {
 74         Point now = Q.top ();    Q.pop ();
 75         if (dp[now.x][now.y][now.d] < now.t)    continue;
 76         if (now.x == e.x && now.y == e.y)    return now.t;
 77         for (int d=-1; d<=1; ++d)
 78         {
 79             Point to = now;
 80             to.d = (to.d + 4 + d) % 4;
 81             if (!can[to.x][to.y][to.d])    continue;
 82             int x = 2 * to.x, y = 2 * to.y;
 83             if (maze[x][y] && maze[x+1][y+1] &&
 84                 ((now.d % 2 == 0 && d != 1) || (now.d % 2 == 1 && d != -1)))    continue;
 85             if (maze[x+1][y] && maze[x][y+1] &&
 86                 ((now.d % 2 == 0 && d != -1) || (now.d % 2 == 1 && d != 1)))    continue;
 87             if (d != 0)    to.t++;
 88             to.x += dx[to.d];    to.y += dy[to.d];
 89             if (check (to))    Q.push (to);
 90         }
 91     }
 92 
 93     return -1;
 94 }
 95 
 96 int main(void)        //HDOJ 4444 Walk
 97 {
 98 //    freopen ("C.in", "r", stdin);
 99 
100     while (true)
101     {
102         nx.clear ();    ny.clear ();    mx.clear ();    my.clear ();
103         s.read ();    e.read ();
104         if (!s.x && !s.y && !e.x && !e.y)    break;
105         memset (maze, false, sizeof (maze));
106 
107         int n;    scanf ("%d", &n);
108         for (int i=1; i<=n; ++i)
109         {
110             Point *t = p[i];
111             t[0].read ();    t[2].read ();
112             if (t[0].x > t[2].x)    swap (t[0], t[2]);
113             if (t[0].y > t[2].y)
114             {
115                 Point a = t[0], b = t[2];
116                 t[0].y = b.y;    t[2].y = a.y;
117             }
118             t[1] = (Point) {t[2].x, t[0].y};
119             t[3] = (Point) {t[0].x, t[2].y};
120         }
121 
122         w = max (compress (nx, mx), compress (ny, my));
123         s.updata ();    e.updata ();
124         for (int i=1; i<=n; ++i)
125         {
126             Point *t = p[i];
127             for (int j=0; j<4; ++j)    t[j].updata ();
128             for (int j=0; j<4; ++j)    t[j] = Point (2*t[j].x, 2*t[j].y);
129             for (int j=t[0].x+1; j<=t[2].x; ++j)
130             {
131                 for (int k=t[0].y+1; k<=t[2].y; ++k)    maze[j][k] = true;        //离散化后将矩形涂黑
132             }
133         }
134 
135         memset (can, true, sizeof (can));
136         for (int i=0; i<w; ++i)
137         {
138             for (int j=0; j<w; ++j)
139             {
140                 int x = i * 2, y = j * 2;
141                 bool *d = can[i][j];
142                 if (maze[x][y+1] && maze[x+1][y+1])    d[0] = false;        //判断4个方向能不能走
143                 if (maze[x+1][y] && maze[x+1][y+1])    d[1] = false;
144                 if (maze[x][y] && maze[x+1][y])    d[2] = false;
145                 if (maze[x][y] && maze[x][y+1])    d[3] = false;
146             }
147         }
148 
149         printf ("%d\n", BFS ());
150     }
151 
152     return 0;
153 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4656795.html

文章来源:https://blog.csdn.net/weixin_30905133/article/details/99285869
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-17201.html

相关文章

jenkins持续集成入门5 - Maven项目 编译和打包-方式2

前提&#xff1a; jenkins中配置maven,jdk环境变量信息 jenkins持续集成入门4 - MAVEN&#xff0c;jdk等环境配置_小哇-CSDN博客 1 安装插件 Maven Integration 2 创建项目&#xff0c;项目类型为maven类型 3 配置拉取代码&#xff0c;再配置如下的

SQL Server 内存压力解决方案

外部压力&#xff1a; 表现形式&#xff1a; 1、total server memory ↓ 2、avilable Mbyte 平衡 3、working set ↓ 如果说SQL server的内存压库来自于外部、我们是去满足SQL server 的内存使用还是去满足外部的内存使用。 如果想SQL server省着点用的…

APP端和PHP端进行数据互通

昨天给APP端提供了一个查询用户收获地址的接口 这个接口是参考了原有其他APP接口文件写出来的&#xff0c;测试的时候总是通不过我的验证(验证手机端COOKIE与PHPCOOKIE的比对。) 百思不得其解&#xff0c;发现COOKIE获取不到&#xff0c;请教大神&#xff0c;而后看APP请求文件…

jenkins持续集成入门6 - 后缀名为.WAR的项目(tomcat运行的) 编译和打包

前提&#xff1a;配置好tomcat的项目管理权限&#xff0c;如下配置tomcat的web项目管理界面_小哇-CSDN博客 1 安装 Deploy to container插件 Jenkins本身无法实现远程部署到Tomcat的功能&#xff0c;需要安装Deploy to container插件实现 2 jenkins 添加一个用户名&#xff0c…

vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式

&#xfeff;&#xfeff;vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式 #Region "WPF 当浏览器窗体关闭时触发 Quit事件 "#If OnSourceInitialized ThenProtected Overrides Sub OnSourceInitialized(e As EventArgs) onloa…

优先队列运用 TOJ 4123 Job Scheduling

链接&#xff1a;http://acm.tju.edu.cn/toj/showp4123.html4123. Job SchedulingTime Limit: 1.0 Seconds Memory Limit: 65536KTotal Runs: 130 Accepted Runs: 29Given N jobs, each denoted by a 2-tuples integer (pi, ri) where pi is the processing time and ri …

jenkins持续集成入门7 - Pipeline流水线项目 两种语法方式Demo讲解

1 声明式写法 pipeline {agent anystages {stage(拉取代码) {steps {echo 拉取代码}}stage(编译构建) {steps {echo 编译构建}}stage(项目部署) {steps {echo 项目部署}}} } stages&#xff1a;代表整个流水线的所有执行阶段。通常stages只有1个&#xff0c;里面包含多个stag…

Leetcode: Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难度&#xff1a;87&#xff0c;这道…

Composite Pattern

1.将对象组合成树形结构以表示“部分--整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。 2.Composite 模式结构图 3.实现 1 #ifndef _COMPONENT_H_ 2 #define _COMPONENT_H_3 4 class Component 5 { 6 public: 7 Component();8 virtual ~Com…

jenkins持续集成入门8 - Pipeline流水线项目 构建maven类型项目案例 从gitlab拉取代码,编译代码

新建一个Pipeline的项目&#xff0c;代码如下 pipeline {agent anystages {stage(gitr拉取代码) {steps {checkout([$class: GitSCM, branches: [[name: */master]], extensions: [], userRemoteConfigs: [[credentialsId: e4880c19-77c8-4a6e-ac82-123e2119039a, url: http:/…