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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-05 03:58:26

hihoCoder_#1092 Have Lunch Together(最短路)的相关文章

机房测试:lunch(贪心+最短路)

题目: 分析: 由数据3得:既然所有人都要学会,肯定是越早学越优.(贪心重要思路) 所以转移就是:dis[v]=max( dis[u] ,L ),u学会之后传授给v的条件是:u先学会,传授的时间在吃饭的时间内 在最短路上转移即可 再考虑有人必须学不会的限制. 如果有一个人u没有学会,就会给他周围的人v一个限制:v不能太早学会,否则吃饭的时候v就会传授给u 所以将lim[v]定为L+1,即他们在L的时候吃饭,L+1的时候v才学会,不会传给u 先将这种传递关系用spfa预处理 再跑一边dij求出每个

Travel(最短路)

Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected b

CSU 1092 Barricade

1092: Barricade Time Limit: 1 Sec  Memory Limit: 32 MBSubmit: 240  Solved: 71[Submit][Status][Web Board] Description GBQC国一共有N个城市,标号分别为1, 2, …, N.N个城市间一共有M条单向通行的道路. 不幸的是,GBQC国的城市1连续暴雨,使得整个城市淹没在汪洋洪水中,于是GBQC国领导人小明决定让城市1的居民暂时移居到城市N,于是一场浩浩荡荡的搬迁运动开始了. 但还有

彻底搞懂最短路算法

转载自:戳 彻底弄懂最短路径问题 只想说:温故而知新,可以为师矣.我大二的<数据结构>是由申老师讲的,那时候不怎么明白,估计太理论化了(ps:或许是因为我睡觉了):今天把老王的2011年课件又看了一遍,给大二的孩子们又讲了一遍,随手谷歌了N多资料,算是彻底搞懂了最短路径问题.请读者尽情享用…… 我坚信:没有不好的学生,只有垃圾的教育.不过没有人理所当然的对你好,所以要学会感恩. 一.问题引入 问题:从某顶点出发,沿图的边到达另一顶点所经过的路径中,各边上权值之和最小的一条路径——最短路径.解决

Lunch Time(费用流变型题,以时间为费用)

Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 183 Problem Description The campus of Nanjing University

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define MAX 105 int lim, n; int p[M