hihocoder_#1092 have Lunch Together (shortest way)

Source: Internet
Author: User

#1092: There are Lunch TogetherTime limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Everyday Littile Hi and Little Ho meet in the school cafeteria to has lunch together. The cafeteria is often so crowded, and adjacent seats is hard to find.

School cafeteria can be considered as a matrix of n*m blocks. Each block can is empty or occupied by people, obstructions and seats. Little Hi and Little Ho starts from the same block. They need to find-adjacent seats (both seats is adjacent if and only if their blocks share a common edge) without Passi ng through occupied blocks. Further more, they want the total distance to the seats is minimal.

Little Hi and Little Ho can move in 4 directions (up, down, left, right) and they can not move outside the matrix.

Input

Input cantains a single testcase.

First line contains, integers N, M, the length and width of school cafeteria.

The next is a matrix consists of N lines and each line containing M characters. Each character describe a block: '. ' For the empty block, ' P ' for People, ' # ' for obstructions, ' S ' for seats and ' H ' for Litt Le Hi and Little Ho ' s starting position.

Ten <= N, M <= 100

Output

Output the minimal distance they need to move to reach, adjacent seats. If no such adjacent seats output a line "Hi and Ho would not have lunch." Without quotes.

Sample input

10 10########### ... p##. # #S #...#. p# #S #. #...##...#.#####.#...#. h###......###. p#. s.###.......###########
Sample output

25

Test instructions: Little hi and Little ho meet every day to eat in the cafeteria, they have to sit in the adjacent position. Give the canteen interior map (n*m), ' # ' stands for the point as an obstacle, ' P ' stands for someone occupying, ' S ' represents the seat, '. ' Represents the feasible point, ' H ' stands for small hi and small ho's starting point (only one at the starting point). Now ask, they go to the shortest distance of adjacent position.

Analysis: Shortest path. It is equivalent to having a n*m point and then building a map. The method of building the map is to traverse all the points, and then determine whether the four points around it can be connected to the edge, it is worth noting that adjacent seats are not connected to each other.

Title Link: http://hihocoder.com/problemset/problem/1092

Code Listing:

#include <map> #include <queue> #include <stack> #include <cmath> #include <cstdio># include<string> #include <cstring> #include <iostream> #include <algorithm>using namespace std ; typedef long long ll;const int MAXN = + 5;const int max_dis = 1e9 + 5;int N,m,start;char str[maxn][maxn];vector<in    T>graph[maxn*maxn];int dis[maxn*maxn];bool vis[maxn*maxn];int x[4]={-1,1,0,0};int y[4]={0,0,-1,1};void init () { for (int i=0;i<maxn*maxn;i++) graph[i].clear ();}    void input () {scanf ("%d%d", &n,&m); for (int i=0;i<n;i++) scanf ("%s", Str[i]);} BOOL Check (int x,int y) {if (x>=0&&x<n&&y>=0&&y<m&&str[x][y]!= ' P ' &    &str[x][y]!= ' # ') return true; return false;}            void Creategraph () {for (int. i=0;i<n;i++) {for (int j=0;j<m;j++) {if (!check (i,j)) continue;            if (str[i][j]== ' S ') continue; for (int k=0;k<4;k++) {int ii=i+x[k];                int jj=j+y[k];            if (check (II,JJ)) Graph[i*n+j].push_back (II*N+JJ);        } if (str[i][j]== ' H ') {start=i*n+j;}    }}}void SPFA (int s) {fill (Dis,dis+n*m,max_dis);    memset (vis,false,sizeof (VIS));    queue<int>q;    while (!q.empty ()) Q.pop ();    Vis[s]=true;    dis[s]=0;    Q.push (s);        while (!q.empty ()) {int U=q.front (); Q.pop ();        Vis[u]=false;            for (int i=0;i<graph[u].size (); i++) {int v=graph[u][i];                if (dis[v]>dis[u]+1) {dis[v]=dis[u]+1;                    if (!vis[v]) {vis[v]=true;                Q.push (v);    }}}}}void solve () {creategraph ();    SPFA (start);    int Min_cost=max_dis;                for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (str[i][j]== ' S ') {for (int k=0;k<4;k++) {                int ii=i+x[k];                int jj=j+y[k]; if (check (II,JJ) &&str[ii][jj]== ' S ') {min_cost=min (MIN_COST,DIS[I*N+J]+DIS[II*N+JJ]);    }}}}} if (Min_cost==max_dis) printf ("Hi and Ho won't have lunch.\n"); else printf ("%d\n", Min_cost);}    int main () {init ();    Input ();    Solve (); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hihocoder_#1092 have Lunch Together (shortest way)

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.