POJ 3026 Borg Maze wide search (BFS) + Minimum Spanning Tree, pojborg

Source: Internet
Author: User

POJ 3026 Borg Maze wide search (BFS) + Minimum Spanning Tree, pojborg

Starting from S, capture each A and find the shortest length of the total path. People at S and A can be divided into two people, but only one person can go at a time.

The idea is to first use BFS to find the distance between each point, build a graph, and then apply the minimum spanning tree template.

One-time. However, I think it is a little troublesome to judge the number.

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace std; const int inf = 1000000; const int maxn = 205; char mapp [maxn] [maxn]; bool visit [maxn] [maxn]; bool vis [maxn]; int dx [] = {-1, 0, 1, 0 }; // Four Directions: int dy [] = {0,-, 1}; int mat [maxn] [maxn], dis [maxn]; int cou, ans; struct Node {int x; int y; int s; // record number from int num; // vertex} next, nodes [maxn]; // nodes [] Save all vertices void bfs (Node node, int a) {me Mset (visit, 0, sizeof (visit); node. s = 0; mapp [node. y] [node. x] = ''; // After the access, change 's' or 'A' to''. You do not need to access the queue again next time <Node> q; visit [node. x] [node. y] = true; q. push (node); while (! Q. empty () {node = q. front (); if (mapp [node. y] [node. x] = 'A') {if (A = 0) {nodes [cou] = node; mat [a] [cou] = mat [cou] [a] = node. s; cou ++; // number of records} else {int B; for (int j = 0; j ++) if (nodes [j]. x = node. x & nodes [j]. y = node. y) {B = nodes [j]. num; break;} mat [a] [B] = mat [B] [a] = node. s; // write the distance to the diagonal matrix} q. pop (); for (int I = 0; I <4; I ++) {next = node; next. x = node. x + dx [I]; next. y = node. y + dy [I]; if (! Visit [next. x] [next. y] & (mapp [next. y] [next. x] = ''| mapp [next. y] [next. x] = 'A') {visit [next. x] [next. y] = true; next. s = node. s + 1; q. push (next) ;}}return ;}bool prim () {memset (vis, 0, sizeof (vis); for (int I = 0; I <cou; I ++) dis [I] = (I = 0? 0: inf); ans = 0; for (int I = 0; I <cou; I ++) {int temp = inf, k = 0; for (int j = 0; j <cou; j ++) {if (! Vis [j] & dis [j] <temp) {temp = dis [j]; k = j ;}} if (temp = inf) return false; vis [k] = true; ans + = temp; for (int j = 0; j <cou; j ++) {if (! Vis [j] & dis [j]> mat [k] [j]) dis [j] = mat [k] [j] ;}} return true ;} int main () {// freopen ("in.txt", "r", stdin); int n; scanf ("% d", & n); while (n --) {int x, y; memset (mapp, 0, sizeof (mapp); scanf ("% d", & x, & y ); char c [maxn] = {0}; gets (c); for (int I = 0; I <y; I ++) gets (mapp [I]); for (int I = 0; I <x; I ++) for (int j = 0; j <y; j ++) if (mapp [I] [j] ='s ') {nodes [0]. y = I; nodes [0]. x = j;} bool flag = true; cou = 1; for (int I = 0; I <cou; I ++) {bfs (nodes [I], I ); if (flag) {for (int j = 0; j <cou; j ++) nodes [j]. num = j; // number each vertex. Run only once flag = false ;}} prim (); printf ("% d \ n", ans );} 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.