USACO 2.1 Overfencing 题解

本文介绍了一个迷宫寻路算法的实现细节,通过构建迷宫地图并使用广度优先搜索找到从不同入口到迷宫内各点的最短路径。代码采用C++编写,展示了如何定义迷宫结构、进行地图构建及遍历。

    该题为水题,代码如下:

  1. /*
  2. ID: millky
  3. PROG: maze1
  4. LANG: C++
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <cmath>
  10. #include <cstdio>
  11. #include <algorithm>
  12. #include <vector>
  13. #include<iomanip>
  14. using namespace std;
  15. #define sqare(x) (x)*(x)
  16. struct pt 
  17. {
  18.     bool l,r,u,b;
  19.     pt(){l=r=u=b=false;}
  20. };
  21. struct haha 
  22. {
  23.     int pt,step;
  24. };
  25. char ormp[210][100];
  26. pt mps[4000];
  27. bool visited[4000];
  28. int ans[4000];
  29. haha fbfs[4000];
  30. int head,tail,longest=0;
  31. int m,n,ns;
  32. vector<int> holes;
  33. void buildMap()
  34. {
  35.     for (int i=1,lim=2*m+1;i<lim;i+=2)
  36.         for (int j=1,haha=2*n+1;j<haha;j+=2)
  37.         {
  38.             if(ormp[i-1][j]==' ') mps[ns].u=true;
  39.             if(ormp[i+1][j]==' ') mps[ns].b=true;
  40.             if(ormp[i][j-1]==' ') mps[ns].l=true;
  41.             if(ormp[i][j+1]==' ') mps[ns].r=true;
  42.             ++ns;
  43.         }
  44.         for (int i=1,j=2*m,lim=2*n+1;i<lim;i+=2){
  45.             if(ormp[0][i] == ' ') holes.push_back(i>>1);
  46.             if(ormp[j][i] == ' ') holes.push_back(n*(m-1)+i/2);
  47.         }
  48.         for (int i=1,j=2*n,lim=2*m+1;i<lim;i+=2)
  49.         {
  50.             if(ormp[i][0] == ' ') holes.push_back(n*(i/2));
  51.             if(ormp[i][j] == ' ') holes.push_back(n*(i/2+1)-1);
  52.         }
  53. }
  54. int bfs(int exit)
  55. {
  56.     ans[exit]=1;
  57.     int maxL=0,cur,temp;
  58.     for (int i=0;i<ns;++i) visited[i]=false;
  59.     visited[exit]=true; head=0;tail=1;fbfs[0].pt=exit;fbfs[0].step=1;
  60.     while (head<tail)
  61.     {
  62.         maxL = fbfs[head].step+1; cur=fbfs[head++].pt;
  63.         temp=cur-n;
  64.         if (mps[cur].u && cur>=n && !visited[temp])
  65.         {
  66.             visited[temp]=true;
  67.             fbfs[tail].pt=cur-n;
  68.             fbfs[tail].step=maxL;
  69.             ++tail;
  70.             ans[temp]=min(ans[temp],maxL);
  71.         }
  72.         temp=cur+n;
  73.         if (temp<ns && !visited[temp] && mps[cur].b)
  74.         {
  75.             visited[temp]=true;
  76.             fbfs[tail].pt=temp;
  77.             fbfs[tail].step=maxL;
  78.             ++tail;
  79.             ans[temp]=min(ans[temp],maxL);
  80.         }
  81.         temp=cur+1;
  82.         if (temp%n && !visited[temp] && mps[cur].r)
  83.         {
  84.             visited[temp]=true;
  85.             fbfs[tail].pt=temp;
  86.             fbfs[tail].step=maxL;
  87.             ++tail;
  88.             ans[temp]=min(ans[temp],maxL);
  89.         }
  90.         temp=cur-1;
  91.         if (mps[cur].l && cur%n && !visited[temp])
  92.         {
  93.             visited[temp]=true;
  94.             fbfs[tail].pt=temp;
  95.             fbfs[tail].step=maxL;
  96.             ++tail;
  97.             ans[temp]=min(ans[temp],maxL);
  98.         }
  99.     }
  100.     return fbfs[tail-1].step;
  101. }
  102. int main()
  103. {
  104.     int a;
  105.     freopen("maze1.in","r",stdin);
  106.     freopen("maze1.out","w",stdout);
  107.     scanf("%d%d",&n,&m);
  108.     a=m*2+1;
  109.     getchar();
  110.     for (int i=0;i<a;++i) gets(ormp[i]);
  111.     buildMap();
  112.     a=0;
  113.     for (int i=0;i<ns;++i) ans[i]=100000;
  114.     for (int i=0;i<holes.size();++i)
  115.         bfs(holes[i]);
  116.     for (int i=0;i<ns;++i) if(ans[i]<100000) a=max(a,ans[i]);
  117.     printf("%d/n",a);
  118.     return 0;
  119. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值