题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人、障碍物、椅子所占据,小hi和小ho想要找两个相邻的椅子。起初两个人都在同一个点,求两人到达满足要求的椅子所移动的最少步骤。

思路:先BFS找出每个S到达每个椅子的最短路径长度,然后遍历每一行和每一列找出最优解。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cstdlib>
#define INF 100000000
using namespace std; int n,m,d[10010],dir[4][2] = {-1,0,1,0,0,-1,0,1};
bool vis[10010];
char ch[110][110];
queue<int>Q; void bfs(int sx,int sy){
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
k = i*m + j;
vis[k] = false;
d[k] = INF;
}
k = sx * m + sy ;
d[k] = 0;
while(!Q.empty())
Q.pop();
Q.push(k);
while(!Q.empty()){
int x = Q.front();
int tx = x/m ;
int ty = x%m ;
Q.pop();
if(vis[x])
continue;
vis[x] = true;
for(i=0;i<4;i++){
int ex = tx + dir[i][0];
int ey = ty + dir[i][1];
if(ex<0 || ex>=n || ey<0 || ey>=m || ch[ex][ey] == '#' || ch[ex][ey] == 'P')
continue;
k = ex*m + ey;
if(vis[k])
continue;
if(ch[ex][ey] == 'S'){
d[k] = d[k] < d[x] + 1 ? d[k] : d[x] + 1;
continue;
}
d[k] = d[k] < d[x] + 1 ? d[k] : d[x] + 1;
Q.push(k);
}
}
} int main(){
int i,j,p1,p2,ans,sx,sy;
bool tag;
while(scanf("%d%d",&n,&m) == 2){
ans = INF;
bool flag = false;
for(i=0;i<n;i++)
scanf("%s",ch[i]);
for(i=0;i<n && !flag;i++)
for(j=0;j<m && !flag;j++)
if(ch[i][j] == 'H'){
sx = i;
sy = j;
flag = true;
} bfs(sx,sy); int t1,t2;
for(i=0;i<n;i++){
p1 = p2 = INF ;
for(j=0;j<m;j++){
if(ch[i][j] == 'S'){
int temp = d[i*m+j];
if(p1 == INF)
p1 = temp;
else if(p2 == INF){
p2 = temp;
ans = ans < p1 + p2 ? ans : p1 + p2 ;
p1 = p2;
p2 = INF ;
}
}
else if(ch[i][j] == 'P' || ch[i][j] == '#' || ch[i][j] == '.'){
p1 = INF ;
p2 = INF ;
}
}
} for(j=0;j<m;j++){
p1 = p2 = INF ;
for(i=0;i<n;i++){
if(ch[i][j] == 'S'){
int temp = d[i*m+j];
if(p1 == INF)
p1 = temp;
else if(p2 == INF){
p2 = temp;
ans = ans < p1 + p2 ? ans : p1 + p2 ;
p1 = p2;
p2 = INF ;
}
}
else if(ch[i][j] == 'P' || ch[i][j] == '#' || ch[i][j] == '.'){
p1 = INF ;
p2 = INF ;
}
}
}
if(ans < INF)
printf("%d\n",ans);
else
printf("Hi and Ho will not have lunch.\n");
}
return 0;
}

hihoCoder 1092 : Have Lunch Together的更多相关文章

  1. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  2. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  3. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  4. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  5. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  6. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  7. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

  8. 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词   代码注意点: 1. getline(istre ...

  9. 【hihoCoder】1121:二分图一·二分图判定

      题目   http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...

随机推荐

  1. lucene 高亮显示

    原文地址: http://blog.csdn.net/javaman_chen/article/details/8224407 Lucene针对高亮显示功能提供了两种实现方式,分别是Highlight ...

  2. kafka-分布式消息系统

    消息中间件MessageQuene 解耦且可扩展:业务复杂度的提升带来的也是耦合度的提高,消息队列在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一接口.这允许你独立的扩展或 ...

  3. [RxJS] Reactive Programming - New requests from refresh clicks -- merge()

    Now we want each time we click refresh button, we will get new group of users. So we need to get the ...

  4. [Javascript] How to use JavaScript's String.replace

    In JavaScript, you can change the content of a string using the replace method. This method signatur ...

  5. poj 2041 Unreliable Message 字符串处理

    水的问题.直接附着到代码. //poj 2041 //sep9 #include <iostream> using namespace std; char mode[128]; char ...

  6. STM32外部中断具体解释

      一.基本概念 ARM Coetex-M3内核共支持256个中断,当中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32眼下支持的中断共84个(16个内部+68个外部), ...

  7. WAS集群系列(6):集群搭建:步骤4:安装WAS升级软件

    逐步点击"下一步",注意一处流程,例如以下列举: "升级软件"安装的路径设置,建议与之前的WAS及IHS安装的绝对路径同样,例如以下所看到的: 逐步点击,完毕安 ...

  8. [MVC4-基礎] 使用DataAnnotations+jQuery進行表單驗證

    我目前有以下表單,Select部分因為必須上一層有選擇下層才有資料,因此使用jQuery驗證問題類型是否有選擇就好,而問題描述要驗證是否為空,這裡採用MVC內建的DataAnnotations來驗證. ...

  9. asp.net读取文件

    context.Response.ContentType = "text/html"; string fullpath = context.Server.MapPath(" ...

  10. ios学习资料(一)

    IT社区: http://www.cnblogs.com/ http://www.csdn.net/ http://www.51cto.com/ http://www.cocoachina.com/ ...