2020 蓝桥杯大学 B 组省赛模拟赛(一)——迷宫

2020 蓝桥杯大学 B 组省赛模拟赛(一)——迷宫

 

别人写的其他题解

题目:有一个 n × m 的迷宫,其中 . 表示空地, * 表示障碍物。除此之外,有 qq 个单向传送门:如果进入格子 (a_i,b_i)(ai,bi) ,那么会被立即传送到 (c_i,d_i)(ci,di) 。保证每个点至多是一个传送门的入口。

如果传送门最终传送到障碍物上,那么将会卡在障碍物上不能移动。

在传送门内部传送的花费是 0,在空地上每次可以往周围四个格子移动,花费是 1。(因为传送门内部花费为0,所以要比较花费的多少)

如果无法到达终点,输出 “No solution”

                                                                                                                        

通过com数组记录这个是第几个传送门,这个数值唯一,通过door数组利用数值找到这个传送门传到的横坐标和纵坐标(door[传送门][0] 和 door[传送门][1])

 1 #include<bits/stdc++.h>1
 2 using namespace std;
 3 #define ll long long
 4 #define N 1010
 5 #define inf 0x3f3f3f3f
 6 char g[N][1010];//地图
 7 int com[N][N] = { 0 };//用来标记是否为传送门
 8 int door[N][3] = { 0 };//用来记录传送到哪door[i][0]和door[i][1]分别用来x,y
 9 int vis[N][N];//当到这一点时所用的花费
10 int dir[2][4] = { -1,+1,0,0,0,0,-1,+1 };
11 int x, y;
12 int n, m;
13 int ans = inf;
14 struct node
15 {
16     int x, y, num;
17 
18 };
19 int bfs(int p, int q)
20 {
21     queue<node>que;
22     node w;
23     w.x = p, w.y = q, w.num = 0;
24     vis[p][q] = 0;
25     que.push(w);
26     while (!que.empty())
27     {
28         node w;
29         w = que.front();
30         que.pop();
31         int pp = w.x, qq = w.y;
32         //cout<<"sjbd  "<<pp<<" "<<qq<<" "<<w.num<<endl;
33         if(pp==x&&qq==y)
34         {
35             return w.num;
36         }
37         int u = com[pp][qq];
38         // cout<<"u="<<u<<endl;
39         if (u != 0)
40         {
41             int ppp = door[u][0];
42             int qqq = door[u][1];
43             if (g[ppp][qqq] == '.'&&w.num<vis[ppp][qqq])
44             {//传送到的点不是障碍且比较这么走的花费小
45                 vis[ppp][qqq] = w.num;
46                 node e = w;
47                 e.x = ppp, e.y = qqq;
48                 que.push(e);
49             }
50         }
51         else
52         {
53             for (int i = 0; i < 4; i++)
54             {
55                 int xx = w.x+dir[0][i], yy = w.y+dir[1][i];
56                 if (vis[xx][yy]>w.num+1&& g[xx][yy] == '.'&&xx >= 1 && xx <= n && yy >= 1 && yy <= m)
57                 {
58                     // cout << xx << " " << yy << endl;
59                     node e=w;
60                     e.num += 1;
61                     e.x = xx, e.y = yy;
62                     vis[xx][yy] = w.num+1;
63                     que.push(e);
64                 }
65             }
66         }
67 
68     }
69     return inf;
70 }
71 int main()
72 {
73     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
74     fill(vis[0],vis[0]+(N*N),inf);
75     cin >> n >> m;
76     for (int i = 1; i <= n; i++)
77     {
78         for (int j = 1; j <= m; j++)
79             cin >> g[i][j];
80     }
81     int p;
82     cin >> p;
83     while (p--)
84     {
85         int x1, y1, x2, y2;
86         cin >> x1 >> y1 >> x2 >> y2;
87         com[x1][y1] = p+1;
88         //因为P值不会重复,所以可以用来标记是否为传送门的同时可以同时看他所传送到的地方
89         door[p+1][0] = x2;
90         door[p+1][1] = y2;
91     }
92     cin >> x >> y;
93     ans=bfs(1, 1);
94     if(ans!=inf)
95         cout<<ans<<endl;
96     else
97         cout<<"No solution"<<endl;
98     return 0;
99 }

猜你喜欢

转载自www.cnblogs.com/thief-of-book/p/12236502.html