HihoCoder _ #1092 Have Lunch Together (Shortest Path ),
#1092: Have Lunch Together time limit: Limit ms single-point time limit: 256 Ms memory limit: MB
Description
Everyday Littile Hi and Little Ho meet in the school cafeteria to have lunch together. The cafeteria is often so crowded that two adjacent seats are hard to find.
School cafeteria can be considered as a matrix of N * M blocks. each block can be empty or occupied by people, obstructions and seats. little Hi and Little Ho starts from the same block. they need to find two adjacent seats (two seats are adjacent if and only if their blocks share a common edge) without passing 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 ctions (up, down, left, right) and they can not move outside the matrix.
Input
Input cantains a single testcase.
First line contains two integers N, M, the length and width of school cafeteria.
The next is a matrix consists of N lines, each line containing M characters. each character describe a block :'. 'For empty block, 'P' for people, '#' for obstructions ctions, 'S' for seats and 'H' for Little Hi and Little Ho's starting position.
10 <= N, M <= 100
Output
Output the minimal distance they need to move to reach two adjacent seats. If no such adjacent seats output a line "Hi and Ho will not have lunch." without quotes.
-
Sample Input
10 10###########...P##..##S#...#.P##S#..#...##...#.#####.#...#.H###......###..P#..S.###.......###########
Sample output
25
Xiao hi and Xiao ho have dinner in the dining room every day. They all need to sit in adjacent places. The internal map of the canteen is provided (N * M). '#' indicates that this point is an obstacle, 'P' indicates that this point is occupied by someone, and 's' indicates the seat ,'. 'indicates a feasible point. 'H' indicates the starting point of small hi and small ho (the starting point is only one ). Now they ask, the shortest distance to the adjacent location.
Analysis: the shortest path. It is equivalent to having N * M points and then creating a graph. The graph creation method is to traverse all vertices and determine whether the four vertices at the top, bottom, and bottom can be connected to edges. It is worth noting that the adjacent seats cannot be connected to edges.
Link: http://hihocoder.com/problemset/problem/1092
Code List:
#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 = 100 + 5;const int max_dis = 1e9 + 5;int n,m,start;char str[maxn][maxn];vector<int>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 will not have lunch.\n"); else printf("%d\n",min_cost);}int main(){ init(); input(); solve(); return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.