题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2822
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2585 Accepted Submission(s):
1000
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 ).
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 ).
用C++超时...用G++过了...amazing
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 using namespace std; 7 int n,m; 8 int ex,ey; 9 char a[1005][1005]; 10 int rx[]={0,0,1,-1}; 11 int ry[]={1,-1,0,0}; 12 bool vis[1005][1005]; 13 struct node 14 { 15 int x,y; 16 int time; 17 }s; 18 bool check(node b) 19 { 20 if(b.x>=1&&b.x<=n&&b.y>=1&&b.y<=m) 21 return true; 22 return false; 23 } 24 struct cmp 25 { 26 bool operator()(node q,node p) 27 { 28 return q.time>p.time; 29 } 30 }; 31 int bfs() 32 { 33 priority_queue<node,vector<node>,cmp>Q; 34 Q.push(s); 35 node cur,next; 36 while(!Q.empty()) 37 { 38 cur=Q.top(); 39 Q.pop(); 40 if(cur.x==ex&&cur.y==ey) 41 return cur.time; 42 for(int i=0;i<4;i++) 43 { 44 next.x=cur.x+rx[i]; 45 next.y=cur.y+ry[i]; 46 if(check(next)&&!vis[next.x][next.y]) 47 { 48 if(a[next.x][next.y]=='X')next.time=cur.time; 49 else next.time=cur.time+1; 50 vis[next.x][next.y]=true; 51 Q.push(next); 52 } 53 } 54 } 55 } 56 int main() 57 { 58 while(cin>>n>>m&&n&&m) 59 { 60 int i,j; 61 for(i=1;i<=n;i++) 62 for(j=1;j<=m;j++) 63 cin>>a[i][j]; 64 cin>>s.x>>s.y; 65 s.time=0; 66 memset(vis,false,sizeof(vis)); 67 vis[s.x][s.y]=true; 68 cin>>ex>>ey; 69 cout<<bfs()<<endl; 70 } 71 return 0; 72 }