hihoCoder_#1092 Have Lunch Together(最短路)

#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个点,然后建图。建图方法就是遍历所有的点,然后判断其上下左右的四个点是否可连边,值得注意的是,相邻的座位之间不可连边。

题目链接:http://hihocoder.com/problemset/problem/1092

代码清单:

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值