zoukankan      html  css  js  c++  java
  • POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026

    感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值。

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <queue>
      4 #include <algorithm>
      5 using namespace std;
      6 
      7 char maze[50][50];
      8 int par[50];
      9 struct Point
     10 {
     11     int x, y;
     12 }point[110];
     13 
     14 struct Edge
     15 {
     16     int u, v, w;
     17     bool operator<(const struct Edge &b)const
     18     {
     19         return w < b.w;
     20     }
     21 }edge[10000];
     22 
     23 struct node
     24 {
     25     int x, y, step;
     26 };
     27 
     28 int find_set(int x)
     29 {
     30     return x == par[x] ? x : par[x] = find_set(par[x]);
     31 }
     32 
     33 queue<struct node>q;
     34 bool vis[50][50];
     35 int bfs(int x, int y, int ex, int ey)
     36 {
     37     while(!q.empty())q.pop();
     38     memset(vis, 0, sizeof(vis));
     39     int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
     40     q.push((struct node){x, y, 0});
     41     vis[x][y] = 1;
     42     while(!q.empty())
     43     {
     44         struct node u = q.front();
     45         q.pop();
     46         if(u.x == ex && u.y == ey)
     47             return u.step;
     48         for(int i = 0; i < 4; i++)
     49         {
     50             if(!vis[u.x+dir[i][0]][u.y+dir[i][1]] && maze[u.x+dir[i][0]][u.y+dir[i][1]] != '#')
     51             {
     52                 vis[u.x+dir[i][0]][u.y+dir[i][1]] = 1;
     53                 q.push((struct node){u.x+dir[i][0], u.y+dir[i][1], u.step+1});
     54             }
     55         }
     56     }
     57 }
     58 
     59 int main()
     60 {
     61     int t, n, m;
     62     char fuck_space[100];
     63     scanf("%d%*c", &t);
     64     while(t--)
     65     {
     66         gets(fuck_space);
     67         sscanf(fuck_space, "%d %d", &m, &n);
     68         int point_rear = 0;
     69         for(int i = 0; i < n; i++)
     70         {
     71             gets(maze[i]);
     72             for(int j = 0; j < m; j++)
     73                 if(maze[i][j] == 'S' || maze[i][j] == 'A')
     74                     point[point_rear++] = (struct Point){i, j};
     75         }
     76         int edge_rear = 0;
     77         for(int i = 0; i < point_rear; i++)
     78         {
     79             for(int j = i+1; j < point_rear; j++)
     80             {
     81                 int w = bfs(point[i].x, point[i].y, point[j].x, point[j].y);
     82                 edge[edge_rear++] = (struct Edge){i, j, w};
     83             }
     84         }
     85         int ans = 0;
     86         for(int i = 0; i < point_rear; i++)
     87             par[i] = i;
     88         sort(edge, edge+edge_rear);
     89         for(int i = 0; i < edge_rear; i++)
     90         {
     91             int x = find_set(edge[i].u);
     92             int y = find_set(edge[i].v);
     93             if(x != y)
     94             {
     95                 ans += edge[i].w;
     96                 par[x] = y;
     97             }
     98         }
     99         printf("%d
    ", ans);
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    Mat
    分治法-最近点对问题
    动态规划作业-最长公共子序列问题
    动态规划作业-多段图的最短路径问题
    OpenCV+VisualStudion2017配置
    R入门(二)-对象以及它们的模式和属性
    Big number
    R入门(一)
    Spring-aop
    Spring-ioc
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3242392.html
Copyright © 2011-2022 走看看