zoukankan      html  css  js  c++  java
  • poj 3026Borg Maze

    http://poj.org/problem?id=3026

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<algorithm>
      5 #define maxn 100
      6 #define maxn1 100000
      7 #include<queue>
      8 using namespace std;
      9 
     10 char s[maxn][maxn];
     11 const int inf=1<<23;
     12 int n,m;
     13 bool vis[maxn*7][maxn*7];
     14 bool visi[maxn*7];
     15 int dis[maxn*7][maxn*7];
     16 int c[maxn*7][maxn*7];
     17 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
     18 int dist[maxn*6];
     19 int ans;
     20 
     21 struct node
     22 {
     23     int x,y;
     24     int step;
     25 }p[maxn1],st1,st;
     26 
     27 void bfs(struct node st2)
     28 {
     29     st2.step=0;
     30     queue<node>q;
     31     q.push(st2);
     32     memset(vis,false,sizeof(vis));
     33     vis[st2.x][st2.y]=true;
     34     while(!q.empty())
     35     {
     36         st1=q.front();
     37         q.pop();
     38         for(int i=0; i<4; i++)
     39         {
     40             int xx=st1.x+dir[i][0];
     41             int yy=st1.y+dir[i][1];
     42             if(xx>=0&&xx<m&&yy>=0&&yy<n&&!vis[xx][yy]&&s[xx][yy]!='#')
     43             {
     44                 if(s[xx][yy]=='S'||s[xx][yy]=='A')
     45                 {
     46                     dis[c[st2.x][st2.y]][c[xx][yy]]=st1.step+1;
     47                     st.x=xx;
     48                     st.y=yy;
     49                     st.step=st1.step+1;
     50                     q.push(st);
     51                     vis[xx][yy]=true;
     52                 }
     53                 else
     54                 {
     55                     st.x=xx;
     56                     st.y=yy;
     57                     st.step=st1.step+1;
     58                     q.push(st);
     59                     vis[xx][yy]=true;
     60                 }
     61             }
     62         }
     63     }
     64 }
     65 
     66 bool prime(int num)
     67 {
     68      memset(visi,false,sizeof(visi));
     69      for(int i=1; i<=num; i++)
     70      {
     71          dist[i]=inf;
     72      }
     73      ans=0;dist[1]=0;
     74      for(int i=1; i<num; i++)
     75      {
     76          int temp=inf,k=0;
     77          for(int j=1; j<num; j++)
     78          {
     79              if(!visi[j]&&dist[j]<temp)
     80              {
     81                  temp=dist[j];
     82                  k=j;
     83              }
     84          }
     85          if(temp==inf) return false;
     86          visi[k]=true;
     87          ans+=temp;
     88          for(int j=1; j<num; j++)
     89          {
     90              if(!visi[j]&&dist[j]>dis[k][j])
     91                 dist[j]=dis[k][j];
     92          }
     93      }
     94      return true;
     95 }
     96 
     97 int main()
     98 {
     99     int t;
    100     scanf("%d",&t);
    101     while(t--)
    102     {
    103         int num=1;
    104         char s1[100];
    105         scanf("%d%d",&n,&m);
    106         gets(s1);
    107         memset(vis,false,sizeof(vis));
    108         memset(c,-1,sizeof(c));
    109         for(int i=0; i<m; i++)
    110         {
    111             gets(s[i]);
    112             for(int j=0; j<n; j++)
    113             {
    114                 if(s[i][j]=='A'||s[i][j]=='S')
    115                 {
    116                     p[num].x=i;
    117                     p[num].y=j;
    118                     c[i][j]=num;
    119                     num++;
    120                 }
    121             }
    122         }
    123         for(int i=1; i<num; i++)
    124         {
    125             bfs(p[i]);
    126         }
    127         prime(num);
    128         printf("%d
    ",ans);
    129     }
    130     return 0;
    131 }
    View Code
  • 相关阅读:
    vijos 1167 南蛮图腾(打印图案)
    noj 1413 Weight 宁波 (dp)
    noj 1173 (宁波)Birdlike Angry Pig (暴力枚举)
    [1438] Get Up, Soldier! noj(宁波)
    [1441] Babelfish noj(宁波)
    长沙理工大学oj 1486: 文本整齐度 哈理工 1476(dp)
    noj 1414 (宁波) Rectangular Parallelopiped(sort+dp)
    8.6前端之内联框架
    8.5前端之Html样式和文本格式化
    8.5前端之类和id
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3522508.html
Copyright © 2011-2022 走看看