#1092 Have Lunch Together(最短路)

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

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 (up, down, left, right) and they can not move outside the matrix.

输入

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, ‘S’ for seats and ‘H’ for Little Hi and Little Ho’s starting position.

10 <= N, M <= 100

输出

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.

样例输入10 10###########…P##..##S#…#.P##S#..#…##…#.#####.#…#.H###……###..P#..S.###…….###########样例输出25

题意:小hi和小ho每天都相约去食堂吃饭,她们都要坐在相邻的位置上。给出食堂内部地图(N*M),’#’代表该点为障碍物,’P’代表该点为有人占据,‘S’代表座位,‘.’代表可行点,‘H’代表小hi和小ho的起始点(起始点只有一个)。现在问,她们走到相邻位置的最短路程。

分析:最短路。相当于有N*M个点,然后建图。建图方法就是遍历所有的点,然后判断其上下左右的四个点是否可连边,值得注意的是,相邻的座位之间不可连边。

题目链接:

代码清单:

#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;}

,人的价值,在遭受诱-惑的一瞬间被决定

#1092 Have Lunch Together(最短路)

相关文章:

  • 【算法】直接插入排序C语言实现
  • 嵌入式 FAAC1.28 在海思HI3518C/HI3518A平台linux中的编译优化
  • Android 动画animation 深入分析
  • Mybatis极其(最)简(好)单(用)的一个分页插件
  • Ext JS Kitchen Sink [Learning by doing](2)ArrayGrid
  • API开发第三篇:PHP的设计模式之完美的单例模式
  • 你感兴趣的文章:

    标签云:

    亚洲高清电影在线, 免费高清电影, 八戒影院夜间, 八戒电影最新大片, 出轨在线电影, 午夜电影院, 在线影院a1166, 在线电影院, 在线观看美剧下载, 日本爱情电影, 日韩高清电影在线, 电影天堂网, 直播盒子app, 聚合直播, 高清美剧, 高清美剧在线观看 EhViewer-E站, E站, E站绿色版, qqmulu.com, qq目录网, qq网站目录,