【luogu P2385 青铜莲花池】 题解

题目链接:https://www.luogu.org/problemnew/show/P2385

莲花池什么的最漂亮啦!

最近刷了两天搜索= =我搜索一直是弱菜

直接套bfs

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring> 
 5 using namespace std;
 6 int n, m, m1, m2, sx, sy, ex, ey;
 7 struct point{
 8     int x, y, t;
 9 }q[4000001];
10 int ma[31][31];
11 int fx[9];
12 int fy[9];
13 void bfs()
14 {
15     int tail = 2,head = 1; 
16     q[head].x = sx, q[head].y = sy, q[head].t = 0;
17     while(head!=tail)
18     {     
19         for(int i = 1; i <= 8; i++)
20         {
21             int nowx = q[head].x + fx[i];
22             int nowy = q[head].y + fy[i];
23             if(nowx == ex && nowy == ey)
24             {
25                 printf("%d",q[head].t+1);
26                 return ;
27             }
28             if(nowx > m || nowx <= 0 || nowy > n || nowy <= 0 || ma[nowx][nowy] == 2 ) continue;
29             ma[nowx][nowy] = 2;
30             q[tail].x = nowx;
31             q[tail].y = nowy;
32             q[tail].t = q[head].t + 1;
33             tail++;
34         }    
35         head++;
36     }
37     
38 }
39 int main()
40 {
41     memset(ma,999,sizeof(ma));
42     scanf("%d%d%d%d",&m,&n,&m1,&m2);
43     for(int i = 1; i <= m; i++)
44     for(int j = 1; j <= n; j++)
45     {
46         scanf("%d",&ma[i][j]);
47         if(ma[i][j]==3)
48         {
49             sx = i;
50             sy = j;
51         }
52         if(ma[i][j]==4)
53         {
54             ex = i;
55             ey = j;
56         }
57         if(ma[i][j] == 0)
58         {
59             ma[i][j] = 2;    
60         } 
61     }
62     fx[1] = m1, fx[2] = m1, fx[3] = 0-m1, fx[4] = 0-m1, fx[5] = m2, fx[6] = m2, fx[7] = 0-m2, fx[8] = 0-m2; 
63     fy[1] = m2, fy[2] = 0-m2, fy[3] = m2, fy[4] = 0-m2, fy[5] = m1, fy[6] = 0-m1, fy[7] = m1, fy[8] = 0-m1;
64     bfs();
65     return 0;
66 }

隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。

隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。

原文地址:https://www.cnblogs.com/MisakaAzusa/p/8551922.html