码迷,mamicode.com
首页 > 其他好文 > 详细

hihoCoder_#1092 Have Lunch Together(最短路)

时间:2017-05-27 21:14:02      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:eof   nes   ...   size   ring   span   int   清单   ima   

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



hihoCoder_#1092 Have Lunch Together(最短路)

标签:eof   nes   ...   size   ring   span   int   清单   ima   

原文地址:http://www.cnblogs.com/ljbguanli/p/6914395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!