当前位置: 代码迷 >> 综合 >> BFS-hihoCoder-第六十六周-Have Lunch Together
  详细解决方案

BFS-hihoCoder-第六十六周-Have Lunch Together

热度:62   发布时间:2023-12-20 21:27:05.0

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

比较水的题,题中对于座位(S)、人(P)所在的位置能否通行的描述并不是太清楚,实际上除了”.”以外,都是不可通行的。
直接对全图进行BFS,然后遍历一下找一下相邻的座位,有多个的时候进行比较取较小值即可。
也可先找相邻的座位,再去BFS,但是估计耗时会长一点吧。

//
// main.cpp
// hihocoder-Have Lunch Together
//
// Created by 袁子涵 on 15/10/8.
// Copyright (c) 2015年 袁子涵. All rights reserved.
//#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define minof(a,b) ((a)>(b)?(b):(a))
#define Max 10005using namespace std;
int dir[4][2]={
   {-1,0},{
   1,0},{
   0,-1},{
   0,1}};
int N,M,minout=Max;
char maze[105][105];
bool visit[105][105];
int s[105][105];typedef struct coor
{int x,y;
}Coor;typedef struct step
{Coor c;int step;
}Step;typedef struct queue
{int head,tail;Step data[10005];
}Queue;Coor start,temp;
Queue q;void queue_init()
{q.head=q.tail=0;memset(q.data, 0, sizeof(q.data));
}void queue_in(Step k)
{q.data[q.tail++]=k;visit[k.c.x][k.c.y]=1;s[k.c.x][k.c.y]=k.step;
}Step queue_out()
{return q.data[q.head++];
}bool queue_empty()
{if(q.head==q.tail){return 1;}return 0;
}bool coor_judge(Coor k)
{if (visit[k.x][k.y]==1) {return 0;}if (k.x<0 || k.x>N || k.y<0 || k.y>M) {return 0;}if (maze[k.x][k.y]=='#' || maze[k.x][k.y]=='P' ) {return 0;}return 1;
}void bfs()
{Step k,t;queue_init();k.c=start;k.step=0;queue_in(k);while (!queue_empty()) {k=queue_out();for (int i=0; i<4; i++) {t=k;t.step++;t.c.x+=dir[i][0];t.c.y+=dir[i][1];if (coor_judge(t.c)) {if (maze[t.c.x][t.c.y]=='S') {visit[t.c.x][t.c.y]=1;s[t.c.x][t.c.y]=t.step;}else{queue_in(t);}}}}
}int main(int argc, const char * argv[]) {memset(maze, 0, sizeof(maze));cin >> N >> M;for (int i=0; i<N; i++) {for (int j=0; j<M; j++) {cin >> maze[i][j];s[i][j]=Max;if (maze[i][j]=='H') {start.x=i;start.y=j;}}}bfs();for (int i=0; i<N; i++) {for (int j=0; j<M; j++) {if (maze[i][j]!='S') {continue;}for (int k=0; k<4; k++) {temp.x=i+dir[k][0];temp.y=j+dir[k][1];if (maze[temp.x][temp.y]=='S') {minout=minof(minout, s[i][j]+s[temp.x][temp.y]);}}}}if (minout >=Max) {cout << "Hi and Ho will not have lunch." <<endl;}elsecout << minout << endl;return 0;
}
  相关解决方案