BFS treasure box

Source: Internet
Author: User

Question connection http://openoj.awaysoft.com/JudgeOnline/problem.php? Id = 1944

Treasure chest

Time Limit: 1 sec memory limit: 128 MB
Submit: 235 solved: 53
[Submit] [Status] [web board]

Description as an obsessive-compulsive disorder patient, John had to collect all his treasure boxes in the maze of the game before he could stop.
Now I will give you a n m maze with obstacles, space and treasure. John is at a starting point, and every step of Y can go up and down to the left.
Go right, of course, the premise is not out of the maze and the points are not obstacles. If John reaches a treasure chest point, the treasure chest will be
He collected the data and changed it to open space.
Now you need to calculate the minimum number of steps required for small y to collect all the treasure boxes. The input contains multiple groups of data. For each group of data, the first row has two positive integers n; m (1 <= N; m <= 100), indicating the size of the maze. In the next n rows, each row has m integers, and the j integers in the I + 1 indicate the J column in the I column of the maze. 0 indicates the open space, and-1 indicates the obstacle, 1 indicates the treasure chest, 2 indicates the starting point of small y. Ensure that 2 has only one and the number of treasure boxes cannot exceed 5. The data ends with two zeros. Output outputs a row of data in each group, which contains an integer, indicating the minimum number of steps of Y. If John cannot collect all the bins, output
-1. Sample Input

3 51 -1 1 -1 20 -1 0 -1 00 0 0 0 00 0
Sample output

12
 
 
Solution: record the num treasure chest and its location (including the starting point), calculate the shortest distance between each two, and then enumerate the class cases of num (that is, enumeration by arrangement and combination)
AC code:
# Include <iostream> # include <cstring> # include <stdio. h >#include <queue >#include <algorithm> using namespace STD; const int M = 205; int n, m; int map [m] [m]; // maze struct point {int R; int L;} p [7]; // record the chest position and initial position int run [4] [2] =, -, 0,-1}; // BFS direction array int BFS (point X, point y) // calculates the shortest distance between the two treasure boxes {int R, l, i, now, next, a [m] [m] = {0}, MP [m] [m]; queue <int> qu; now = x. R * m + X. l; qu. push (now); for (I = 0; I <n; I ++) // The map cannot be changed because it needs to be called multiple times. Map initial mpfor (L = 0; L <m; l ++) MP [I] [l] = map [I] [l]; while (! Qu. empty () {now = Qu. front (); qu. pop (); for (I = 0; I <4; I ++) {r = now/m + run [I] [0]; L = now % m + run [I] [1]; next = r * m + L; if (r> = 0 & R <n & L> = 0 & L <M & amp; MP [r] [l]! =-1) {A [r] [l] + = A [now/m] [now % m] + 1; qu. push (next); MP [r] [l] =-1; if (r = y. R & l = y. l) return a [r] [l] ;}} return-1; // returns} int main () {int I, j, num, Min, dis [m] [m]; // dis array saves the shortest distance from the I chest to the J chest while (scanf ("% d", & N, & M )! = EOF & n! = 0 & M! = 0) {memset (DIS, 0, sizeof (DIS); int flag = 0; // If a treasure chest cannot reach the sign min = 10000000; num = 1; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {CIN> map [I] [J]; if (Map [I] [J] = 2) {P [0]. R = I; P [0]. L = J;} If (Map [I] [J] = 1) {P [num]. R = I; P [num ++]. L = J ;}for (I = 0; I <num; I ++) {for (j = 0; j <num; j ++) if (I! = J) {dis [I] [J] = BFS (P [I], p [J]); If (DIS [I] [J] =-1) {flag = 1; break ;}} if (FLAG) Break ;}if (FLAG) {printf ("-1 \ n"); continue ;} char A [7] = {"012345"}; // because there are a maximum of five treasure boxes, use this string to represent the order of arrangement. I will understand it below, which is a bit amazing! Do {int S = 0; for (I = 0; I <num-1; I ++) {S + = dis [A [I]-'0'] [A [I + 1]-'0'];} If (min> S) // determine each case, min = s;} while (next_permutation (a + 1, A + num )); // after a library function passes through it for the first time, click the link printf ("% d \ n", min) below for introduction );} return 0 ;}
Next_permutation (a + 1, A + num) function Introduction

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.