HDU 2822 Dogs

Dogs

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

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 ).

Source

2009 Multi-University Training Contest 1 - Host by TJU
 
思路:
注意细节 + 优先队列 OK。
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int
hash[1010][1010];
char
map[1010][1010];
int
n,m;
int
ax,ay,bx,by;
int
move[4][2] = {1,0,-1,0,0,1,0,-1};
struct
Node
{

    int
x,y;
    int
step;
    friend
bool operator < (const Node & a,const Node & b)
    {

        return
a.step > b.step;
    }
};

void
BFS()
{

    priority_queue <Node> q;
    Node top;
    top.x = ax;top.y = ay;top.step = 0;
    q.push(top);
    while
(!q.empty())
    {

        Node temp;
        temp = q.top();q.pop();
        if
(temp.x == bx && temp.y == by)
        {

            printf("%d ",temp.step);
            break
;
        }

        for
(int i = 0;i < 4;i ++)
        {

            int
x = temp.x + move[i][0];int y = temp.y + move[i][1];
            int
step = temp.step;
            if
(hash[x][y] == 0 && x >= 1 && x <= n && y >= 1 && y <= m)
            {

                if
(map[x][y] == '.')
                   step ++;
                Node xin;
                xin.x = x;xin.y = y;xin.step = step;
                q.push(xin);
                hash[x][y] = 1;
            }
        }
    }
}
  
int
main()
{

    while
(scanf("%d%d",&n,&m),n != 0 && m != 0)
    {

        memset(hash,0,sizeof(hash));
        for
(int i = 1;i <= n;i ++)
          for
(int j = 1;j <= m;j ++)
             scanf(" %c",&map[i][j]);
        scanf("%d%d",&ax,&ay);
        hash[ax][ay] = 1;
        scanf("%d%d",&bx,&by);
        BFS();
    }
}
 
原文地址:https://www.cnblogs.com/GODLIKEING/p/3283570.html