HDU 2822 Dogs

来源:互联网 发布:女鞋淘宝鞋店知乎 编辑:程序博客网 时间:2024/05/16 06:04

题目大意:有一只狗想要去拜访另一只狗,但这只狗非常懒,它想要挖最少的洞。X代表这些是路是通的,且四个方向只要连着的X都不需要挖洞,“  . ”代表需要挖洞。

每组最后两行分别代表狗的起始点和终点。问小狗最少需要挖多少洞才能去拜访他的朋友。

解题思路:最短路第一反应应该是BFS,但是还需要加点什么,因为走X步数是不变的,走“ . ”步数需要加一,也就是每个点的权值是不一样的,所以小狗每移动一个方向走的步数可能不一样,所以每次都要从最小的步数出发,这里就需要用到优先队列了。


Prairie dog comes again! Someday one little prairie dog Tim wants to visit one of his friends on the farmland, but he is as lazy as his friend (who required Tim to come to his place instead of going to Tim's), So he turn to you for help to point out how could him dig as less as he could. 

We know the farmland is divided into a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each other in any case, they belong to the same house. Then the little Tim start from his home located at (x0, y0) aim at his friend's home ( x1, y1 ). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but he must dig some distance to reach another house. The farmland will be as big as 1000 * 1000, and the up left corner is labeled as ( 1, 1 ). 
Input
The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.
Output
For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.
Sample Input
6 6..X...XXX.X.....X.X.....X.....X.X...3 56 30 0
Sample Output
3           
Hint
Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).

#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;char s[1005][1005];int n,m,x1,y1,x2,y2,book[1005][1005];int dir[4][2]= {0,-1,0,1,-1,0,1,0};struct node{    int x,y,step;    bool operator <(node a)const //优先队列从小到大排序    {        return a.step<step;    }};int bfs(){    priority_queue<node>q;    node now,tmp;    now.x=x1;    now.y=y1;    now.step=0;    book[x1][y1]=1;    q.push(now);    while(!q.empty())    {        now=q.top();        q.pop();        if(now.x==x2&&now.y==y2)            return now.step;        for(int i=0; i<4; i++)        {            int tx=now.x+dir[i][0];            int ty=now.y+dir[i][1];            if(tx>=0&&tx<m&&ty>=0&&ty<n&&!book[tx][ty])            {                tmp.x=tx;                tmp.y=ty;                tmp.step=now.step;                book[tx][ty]=1;                if(s[tx][ty]=='.')   //只有是点的情况步数才加一                    tmp.step=now.step+1;                q.push(tmp);            }        }    }}int main(){    int i,j;    while(~scanf("%d%d",&m,&n))    {        if(m==0&&n==0)break;        memset(book,0,sizeof(book));        for(i=0; i<m; i++)            scanf("%s",s[i]);        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        //输入时从0开始的,所以起点和终点都减一        x1--;        y1--;        x2--;        y2--;        printf("%d\n",bfs());    }    return 0;}


原创粉丝点击