码迷,mamicode.com
首页 > 其他好文 > 详细

P1126 机器人搬重物

时间:2019-11-14 21:50:24      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:print   mem   fine   span   ==   com   tor   bfs   stream   

题目链接:https://www.luogu.org/problem/P1126

 

思路:

首先我们需要把题意的图给转化一下,变成下面这种

技术图片

 

 

然后我们再定义一个方向:

4代表 "N"  3代表"S" 2代表"W" 1代表"E"

 

于是,开始写BFS

用队列存储每一个格点的信息,然后起点入队,每次从队首取出一个元素,根据这个元素旋转,直行,得到一个新的坐标,然后判断由这种方式到达的这个点是否是耗时最短的一种方式(类似于Dijkstra中的dis数组),此处用ans[]数组表示,然后队列为空后,遍历ans数组去找一个最小的就可以了。

 

  1 #include <math.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <string>
  7 #include <string.h>
  8 #include <vector>
  9 #include <map>
 10 #include <stack>
 11 #include <set>
 12 #include <queue>
 13 
 14 
 15 #define LL long long
 16 #define INF 0x3f3f3f3f
 17 #define ls nod<<1
 18 #define rs (nod<<1)+1
 19 const int maxn = 1e5+10;
 20 const double eps = 1e-9;
 21 
 22 
 23 int vis[60][60];
 24 bool map[60][60];
 25 
 26 struct Node {
 27     int x,y,dir;
 28     int step;
 29 };
 30 
 31 int ans[5010] = {-1};
 32 int tot,min;
 33 int dir[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
 34 
 35 int main() {
 36     min = 0x7fffff;
 37     int n,m;
 38     std::cin >> n >> m;
 39     for (int i=1;i<=n;i++) {
 40         for (int j=1;j<=m;j++) {
 41             bool a;
 42             std::cin >> a;
 43             if (a) {
 44                 map[i][j] = 1;
 45                 map[i][j-1] = 1;
 46                 map[i-1][j] = 1;
 47                 map[i-1][j-1] = 1;
 48             }
 49         }
 50     }
 51     int sx,sy,ex,ey;
 52     char di;
 53     std::cin >> sx >> sy >> ex >> ey >> di;
 54     std::queue<Node> q;
 55     Node a;
 56     a.x = sx;
 57     a.y = sy;
 58     a.step = 0;
 59     if (di == E) {
 60         a.dir = 1;
 61     }
 62     if (di == W) {
 63         a.dir = 2;
 64     }
 65     if (di == S) {
 66         a.dir = 3;
 67     }
 68     if (di == N) {
 69         a.dir = 4;
 70     }
 71     q.push(a);
 72     memset(vis,1, sizeof(vis));
 73     while (!q.empty()) {
 74         Node k = q.front();
 75         q.pop();
 76         if (k.x == ex && k.y == ey) {
 77             ans[++tot] = k.step;
 78         }
 79         for (int i=1;i<=4;i++) {
 80             int step = k.step;
 81             if (i!=k.dir) {
 82                 step = k.step + 1;
 83                 if ((i==1&&k.dir==2) || (i==2&&k.dir==1) || (i==3&&k.dir==4) || (i==4&&k.dir==3)) {
 84                     step = k.step + 2;
 85                 }
 86             }
 87             for (int j=1;j<=3;j++) {
 88                 int x,y;
 89                 x = k.x + j*dir[i][0];
 90                 y = k.y + j*dir[i][1];
 91                 if (x>=n || x<1 || y>=m || y<1 || map[x][y]) {
 92                     break;
 93                 }
 94                 if (vis[x][y] > step) {
 95                     Node ed;
 96                     ed.x = x;
 97                     ed.y = y;
 98                     ed.step = step + 1;
 99                     ed.dir = i;
100                     q.push(ed);
101                     vis[x][y] = step;
102                 }
103             }
104         }
105     }
106     for (int i=1;i<=tot;i++) {
107         if (ans[i] < min) {
108             min = ans[i];
109         }
110     }
111     if (min < 0x7fffff) {
112         printf("%d\n",min);
113     }
114     else
115         printf("-1\n");
116     return 0;
117 }

 

 

P1126 机器人搬重物

标签:print   mem   fine   span   ==   com   tor   bfs   stream   

原文地址:https://www.cnblogs.com/-Ackerman/p/11862523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!