zoukankan      html  css  js  c++  java
  • poj 3026 (bfs+prim)

    这个题有一个意思描述使的题目简单了。。囧。。开始还以为数据水呢。其实是我题目没看清楚。。。只能在A,S的时候才能分成很多的路径。

    1
    5 8
    A###A
     ###
         
    ## ##
    ## ##
    ## ##
    ##S##
    #####

    比如这个数据输出的是16。ps:假如没有那个条件的话,就是应该输出12。。。。(怎么做?。。)

    这题的代码:

    View Code
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <queue>
      5 
      6 using namespace std;
      7 
      8 #define inf 0x7ffffff
      9 #define MAXN 52
     10 int map[MAXN<<1][MAXN<<1];
     11 char c[MAXN][MAXN];
     12 int id[MAXN][MAXN];
     13 int y,x,cnt;
     14 int dir[4][2]={0,-1,0,1,-1,0,1,0};
     15 
     16 struct point
     17 {
     18     int x,y;
     19     int step;
     20     point(){}
     21     point(int a,int b,int c){x=a,y=b,step=c;}
     22 };
     23 
     24 bool is_ok(int i,int j)
     25 {
     26     if(i>=0 && i<x && j>=0 && j<y)
     27         return true;
     28     return false;
     29 }
     30 
     31 void bfs(int tx,int ty)
     32 {
     33     queue<point> q;
     34     while(!q.empty()) q.pop();
     35     bool vis[MAXN][MAXN];
     36     point sp=point(tx,ty,0);
     37     memset(vis,0,sizeof(vis));
     38     vis[tx][ty]=1;
     39     q.push(sp);
     40     while(!q.empty())
     41     {
     42         point cur,next;
     43         cur=q.front();q.pop();
     44         if(c[cur.x][cur.y]=='A' || c[cur.x][cur.y]=='S')
     45             map[id[tx][ty]][id[cur.x][cur.y]]=cur.step;
     46         for(int i=0;i<4;i++)
     47         {
     48             next.x=cur.x+dir[i][0];
     49             next.y=cur.y+dir[i][1];
     50             next.step=cur.step+1;
     51             if(!is_ok(next.x,next.y)) continue;
     52             if(c[next.x][next.y]=='#') continue;
     53             if(vis[next.x][next.y]) continue;
     54             vis[next.x][next.y]=1;
     55             q.push(next);
     56         }
     57     }
     58 }
     59 //---------------prim--------------
     60 int prim(int N)
     61 {
     62     int i,j;
     63     int ans=0;
     64     int dis[MAXN<<1];
     65     bool vis[MAXN<<1];
     66     for(i=0;i<N;i++)
     67     {
     68         vis[i]=0;
     69         dis[i]=map[0][i];
     70     }
     71     vis[0]=1;
     72     for(i=0;i<N-1;i++)
     73     {
     74         int MIN=inf,flag=-1;
     75         for(j=0;j<N;j++)
     76         {
     77             if(!vis[j] && dis[j]<MIN)
     78             {
     79                 flag=j;
     80                 MIN=dis[j];
     81             }
     82         }
     83         vis[flag]=1;
     84         ans+=MIN;//答案
     85         for(j=0;j<N;j++)
     86             if(!vis[j] && map[flag][j]<dis[j])
     87                 dis[j]=map[flag][j];
     88     }
     89     return ans;
     90 }
     91 //---------------------------------
     92 
     93 
     94 int main()
     95 {
     96     int t;
     97     scanf("%d",&t);
     98     while(t--)
     99     {
    100         cnt=0;
    101         scanf("%d%d",&y,&x);
    102         gets(c[0]);
    103         memset(c,0,sizeof(c));
    104         for(int i=0;i<x;i++)
    105         {
    106             gets(c[i]);
    107             for(int j=0;j<y;j++)
    108                 if(c[i][j]=='A' || c[i][j]=='S')
    109                     id[i][j]=cnt++;
    110         }
    111         for(int i=0;i<x;i++)
    112         {
    113             for(int j=0;j<y;j++)
    114                 if(c[i][j]=='A' || c[i][j]=='S')
    115                     bfs(i,j);
    116         }
    117         /*------------------
    118         for(int i=0;i<cnt;i++)
    119         {
    120             for(int j=0;j<cnt;j++)
    121                 cout<<map[i][j];
    122             cout<<endl;
    123         }
    124         */
    125         printf("%d\n",prim(cnt));
    126 
    127     }
    128 }
    129 /*
    130 1
    131 5 8
    132 A###A
    133  ###
    134      
    135 ## ##
    136 ## ##
    137 ## ##
    138 ##S##
    139 #####
    140  */
  • 相关阅读:
    LeetCode Array Easy 414. Third Maximum Number
    LeetCode Linked List Medium 2. Add Two Numbers
    LeetCode Array Easy 283. Move Zeroes
    LeetCode Array Easy 268. Missing Number
    LeetCode Array Easy 219. Contains Duplicate II
    LeetCode Array Easy 217. Contains Duplicate
    LeetCode Array Easy 189. Rotate Array
    LeetCode Array Easy169. Majority Element
    LeetCode Array Medium 11. Container With Most Water
    LeetCode Array Easy 167. Two Sum II
  • 原文地址:https://www.cnblogs.com/Missa/p/poj3026.html
Copyright © 2011-2022 走看看