广搜+优先队列 HDU-2822 D

来源:互联网 发布:黑客帝国文字矩阵 编辑:程序博客网 时间:2024/05/18 02:50
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 5
6 3

0 0
Sample Output
3



Hint
Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).

题意
一个小狗找朋友,它初始坐标为(sx,sy),朋友家的坐标为(ex,ey),中途经过的点有X有. ,在走‘.’的时候必须挖一单位地,但是在走‘X’的时候就不用,求这只小狗到朋友家最少需要挖多少单位地,为了简便,下面就用步数代替。

题解
从题目上可以看出是广搜,但是在用广搜写的时候给出的这组数据就没运行正确,运行出4。找不出来错误后,于是决定用深搜,没让我失望,超时了.........其实本题应用广搜+优先队列写,因为有时候需要走一步,有时候不用,可能存在一种情况,走一步的先走到了目标点,但是它的步数不是最小的,所以队列每一次的扩展都应该把步数从小到大排好。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;char a[1005][1005];int n,m,next1[4][2]= {0,1,0,-1,-1,0,1,0};int book[1005][1005],ex,ey,sx,sy,flag;struct node{    int x;    int y;    int step;    friend bool operator<(node a,node b) //优先队列    {        return a.step>b.step;            //在这里大于号其实表示的是小于的意思    }};void bfs(int x,int y){    priority_queue<node>Q;             //    node now,tmp;    int i,j,ty,tx;    now.x=x;    now.y=y;    now.step=0;    Q.push(now);    while(!Q.empty())    {        now=Q.top();               //不是Q.front(),而是Q.top(),读取队首元素        Q.pop();        if(now.x==ex&&now.y==ey)        {            printf("%d\n",now.step);            return ;        }        for(i=0; i<4; i++)        {            tmp.x=now.x+next1[i][0];            tmp.y=now.y+next1[i][1];            if(tmp.x<1||tmp.y<1||tmp.x>n||tmp.y>m||book[tmp.x][tmp.y]==1)                continue;            if(book[tmp.x][tmp.y]==0)            {                book[tmp.x][tmp.y]=1;                if(a[tmp.x][tmp.y]=='X')                    tmp.step=now.step;                else if(a[tmp.x][tmp.y]=='.')                    tmp.step=now.step+1;                Q.push(tmp);            }        }    }}int main(){    while(~scanf("%d%d",&n,&m))    {        if(n==0&&m==0) break;        memset(book,0,sizeof(book));        int i,j;        for(i=1; i<=n; i++)        {            getchar();                                //如果从(1,1)输入,不加getchar()会错            for(j=1; j<=m; j++)                scanf("%c",&a[i][j]);        }        scanf("%d%d",&sx,&sy);        scanf("%d%d",&ex,&ey);        book[sx][sy]=1;        bfs(sx,sy);    }    return 0;}