POJ 3206 Borg Maze:bfs+prim

Source: Internet
Author: User

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

To give you a m*n maze, you can go up and down the left and right, can only take a space or letters, to find all the letters connected to the minimum cost.

Train of thought: first use BFS to find out the distance of S to all a, then use prim to find the minimum spanning tree, find out the minimum cost. This problem is not the problem of the pit, is the data too pit, in the space processing did not fix, contributed several WA and CE, see discuss just know very pit, finally with g++ over the code, C + +, really do not know what to say good =. =

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

#include <stdio.h> #include <iostream> #include <string.h> #define INF 0xfffffff using namespace std;
Char str[55][55];
int map[55][55];
int dis[55];
int dist[105][105];
int edge[105][105];
  
int num, n, M, p;
  
int move[4][2] = {0,-1}, {0, 1}, {-1, 0}, {1, 0}};
  
int min (int a, int b) {return a > b b:a;}
    void BFS (int i, int j) {int head = 0, tail = 0;
    int q_x[2550], q_y[2550];
    BOOL Vis[55][55];
    Memset (Vis, false, sizeof (VIS));
    memset (Dist, 0, sizeof (Dist));
    VIS[I][J] = true;
    Q_x[tail] = i;
    q_y[tail++] = j;
        while (Head < tail) {int x = Q_x[head];
        int y = q_y[head++];
        if (Map[x][y]) {Edge[map[i][j]][map[x][y]] = dist[x][y];
            for (int t = 0; t < 4; t++) {int dx = x+move[t][0];
            int dy = y+move[t][1];
  if (DX >= 1 && dx <= m && dy >= 1 && dy <= n) {              if (!vis[dx][dy] && Str[dx][dy]!= ' # ') {q_x[tail] = dx;
                    q_y[tail++] = dy;
                    Vis[dx][dy] = true;
                Dist[dx][dy] = dist[x][y]+1;
    ()}}} int Prim () {int Ans;
    int Min_ele, Min_node;
    for (int i = 1; I <= num; i++) {dis[i] = INF;
    Ans = 0;
    int r = 1;
        for (int i = 1; I <= num-1; i++) {min_ele = INF;
        DIS[R] =-1; for (int j = 1; j <= Num; j +) {if (Dis[j] >= 0) {dis[j] = min (dis[j
                ], edge[r][j]);
                    if (Dis[j] < Min_ele) {Min_ele = Dis[j];
                Min_node = j;
        }} R = Min_node;
    Ans + + Min_ele;
return Ans;
    } void Solve () {int i, J;
    CIN >> P;
while (p--) {        memset (map, 0, sizeof (map));
        num = 0;
        CIN >> n >> m;
        Char s[100];
            Gets (s);///here too pit for (int i = 1; I <= m; i++) {gets (str[i]);
                    for (int j = 0; J < N; j +) {if (str[i][j] = = ' A ' | | str[i][j] = = ' S ') {
                MAP[I][J] = ++num;
            for (int i = 1; I <= m. i++) {for (int j = 1; J <= N; j +)
                {if (Map[i][j]) {BFS (i, j);
    } printf ("%d\n", Prim ());
  
    int main () {Solve ()};
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.