HDU 2612 Find a way (广搜,队列)

来源:互联网 发布:js cookie大小 编辑:程序博客网 时间:2024/05/18 03:35


Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9040    Accepted Submission(s): 2897


Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 

Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 

Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 

Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866


题目:两个人在不同位置打算在KFC见面,给出地图,其中包括两人位置,肯德基位置,以及障碍物的位置。求两个人在肯德基碰面的最短路径。


#include <iostream>#include <algorithm>#include <string.h>#include <queue>#define pp 202char map[pp][pp];//地图int a[pp][pp]; //第一个人到各点的步数int b[pp][pp];//第二个点到各点的步数int dx[4]={0,0,-1,1};//四个搜索方向int dy[4]={1,-1,0,0};int yx,yy,mx,my;//记录两个人的位置int m,n;int i,j;using namespace std;struct node{    int x,y;};void getmap(){    for(i=0;i<n;i++)        for(j=0;j<m;j++)    {        cin>>map[i][j];       if(map[i][j]=='Y')//记录位置       {           yx=i;           yy=j;       }       if(map[i][j]=='M')       {           mx=i;           my=j;       }    }}void bfs(int c,int d,int s[pp][pp]){    queue<node>q;    node aa,bb;    aa.x=c;    aa.y=d;    s[c][d]=0;    q.push(aa);                      //入队    while(!q.empty())    {        bb=q.front();             //访问队首元素,把值复制给bb        q.pop();                        //弹出队首元素        for(i=0;i<4;i++)        {            aa.x=bb.x+dx[i];            aa.y=bb.y+dy[i];            if(aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<m&&map[aa.x][aa.y]!='#'&&s[aa.x][aa.y]==0)            {                s[aa.x][aa.y]=s[bb.x][bb.y]+1;                q.push(aa);               //入队            }        }    }}int main(){    while (cin>>n>>m)    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        getmap();        int minn=1000000;        bfs(yx,yy,a);        bfs(mx,my,b);        for(i=0;i<n;i++)            for(j=0;j<m;j++)        {            if (map[i][j]=='@'&&a[i][j]!=0)         //要确定该点被访问过            {                minn=min(minn,a[i][j]+b[i][j]);            }        }        cout<<minn*11<<endl;    }    return 0;}


知识点:、

c++queue容器介绍

定义queue对象的示例代码如下:

queue<int>q1;

queue<double>q2;

queue的基本操作有:

1.入队:如q.push(x):将x元素接到队列的末端;

2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值;

3,访问队首元素:如q.front()

4,访问队尾元素,如q.back();

5,访问队中的元素个数,如q.size();


0 0
原创粉丝点击