您的位置:首页 > 其它

hiho一下 第六十六周 Have Lunch Together

2015-10-08 18:09 253 查看
时间限制: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 and Ho will not have lunch. 。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x7f7f7f
char s[102][102];
int n,m,sx,sy;
int pre[102][102];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y;
} f;
int bfs()
{
int i,j;
f.x=sx;
f.y=sy;
queue<node>q;
memset(pre,-1,sizeof(pre));
pre[f.x][f.y]=0;
q.push(f);
while(!q.empty())
{
node ff=q.front();
q.pop();
for(i=0; i<4; i++)
{
f.x=ff.x+dir[i][0];
f.y=ff.y+dir[i][1];
if((s[f.x][f.y]=='.' || s[f.x][f.y]=='S')&&(f.x<n&&f.y<m &&f.x>=0 &&f.y>=0))
{
if(pre[f.x][f.y]==-1)
{
pre[f.x][f.y]=pre[ff.x][ff.y]+1;
//                    cout<<pre[f.x][f.y]<<" ";
if(s[f.x][f.y]!='S')
q.push(f);
}
}
}
}
int min1=999999;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(s[i][j]=='S' &&pre[i][j]!=-1)
{
if(s[i-1][j]=='S'&&pre[i-1][j]!=-1 &&min1>pre[i][j]+pre[i-1][j])
{
min1=pre[i][j]+pre[i-1][j];
}
if(s[i][j-1]=='S' &&pre[i][j-1]!=-1 &&min1>pre[i][j]+pre[i][j-1])
{
min1=pre[i][j]+pre[i][j-1];
}
if(s[i+1][j]=='S' &&pre[i+1][j]!=-1 &&min1>pre[i][j]+pre[i+1][j])
{
min1=pre[i][j]+pre[i+1][j];
}
if(s[i][j+1]=='S' &&pre[i][j+1]!=-1 &&min1>pre[i][j]+pre[i][j+1])
{
min1=pre[i][j]+pre[i][j+1];
}
}
//            cout<<pre[i][j]<<" ";
}
}
return min1;
}
int main()
{
while(cin>>n>>m)
{
int i,j;
for(i=0; i<n; i++)
cin>>s[i];
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(s[i][j]=='H')
{
sx=i;
sy=j;
break;
}
int ans=bfs();
if(ans==999999)
cout<<"Hi and Ho will not have lunch."<<endl;
else
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM算法