您的位置:首页 > 其它

Hduoj1242【广搜+优先队列】

2014-11-26 16:14 190 查看
/*Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16997    Accepted Submission(s): 6151

Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs,
ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays.
When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 
1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, 
of course.)

Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you 
should output a line containing "Poor ANGEL has to stay in the prison all his life." 

Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 

Sample Output
13

Author
CHEN, Xue

Source
ZOJ Monthly, October 2003 
*/ 
#include<stdio.h>
#include<string.h>
char map[220][220];
int n, m, min, step, dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0}, vis[220][220];
int quee[40000][3];
void bfs(int x, int y)
{
<span style="white-space:pre">	</span>int i, j, nx, ny, front = 0, rear = 0, num;
<span style="white-space:pre">	</span>quee[rear][0] = x;
<span style="white-space:pre">	</span>quee[rear][1] = y;
<span style="white-space:pre">	</span>quee[rear++][2] = 0;
<span style="white-space:pre">	</span>while(front < rear)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>x = quee[front][0];
<span style="white-space:pre">		</span>y = quee[front][1];
<span style="white-space:pre">		</span>num = quee[front++][2];
<span style="white-space:pre">		</span>for(i = 0; i < 4; ++i)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>nx = x + dx[i];
<span style="white-space:pre">			</span>ny = y + dy[i];
<span style="white-space:pre">			</span>if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '#' )
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>int k ;
<span style="white-space:pre">				</span>if(map[nx][ny] == 'x')
<span style="white-space:pre">				</span>k = num+2;
<span style="white-space:pre">				</span>else
<span style="white-space:pre">				</span>k = num+1;
<span style="white-space:pre">				</span>if(map[nx][ny] == 'r')
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>if(k < min)
<span style="white-space:pre">					</span>min = k;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>map[nx][ny] = '#';//change wall
<span style="white-space:pre">				</span>if(front == rear)
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>quee[rear][0] = nx;
<span style="white-space:pre">					</span>quee[rear][1] = ny;
<span style="white-space:pre">					</span>quee[rear][2] = k;
<span style="white-space:pre">					</span>rear++;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>else
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>for(j = front; j < rear; ++j)//find   k
<span style="white-space:pre">					</span>{
<span style="white-space:pre">						</span>if(quee[j][2] > k)
<span style="white-space:pre">						</span>{
<span style="white-space:pre">							</span>for(int l = rear; l > j; l--)//move behind
<span style="white-space:pre">							</span>{
<span style="white-space:pre">								</span>quee[l][0] = quee[l-1][0];
<span style="white-space:pre">								</span>quee[l][1] = quee[l-1][1];
<span style="white-space:pre">								</span>quee[l][2] = quee[l-1][2];
<span style="white-space:pre">							</span>}
<span style="white-space:pre">							</span>quee[j][0] = nx;//put in
<span style="white-space:pre">							</span>quee[j][1] = ny;
<span style="white-space:pre">							</span>quee[j][2] = k;
<span style="white-space:pre">							</span>break;
<span style="white-space:pre">						</span>}
<span style="white-space:pre">					</span>}
<span style="white-space:pre">					</span>if(j == rear)//not find
<span style="white-space:pre">					</span>{
<span style="white-space:pre">						</span>quee[rear][0] = nx;
<span style="white-space:pre">						</span>quee[rear][1] = ny;
<span style="white-space:pre">						</span>quee[rear][2] = k;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">					</span>rear++;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
}
int main()
{
<span style="white-space:pre">	</span>int i, j, k, x, y;
<span style="white-space:pre">	</span>while(scanf("%d%d", &n, &m) != EOF)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>getchar();
<span style="white-space:pre">		</span>for(i = 0;  i < n; ++i)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>for(j = 0; j < m; ++j)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>scanf("%c", &map[i][j]);
<span style="white-space:pre">				</span>if(map[i][j] == 'a')
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>x = i;
<span style="white-space:pre">					</span>y = j;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>getchar();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>min = 100000000;
<span style="white-space:pre">		</span>memset(quee, 0, sizeof(quee));
<span style="white-space:pre">		</span>bfs(x,y);
<span style="white-space:pre">		</span>if(min == 100000000)
<span style="white-space:pre">		</span>printf("Poor ANGEL has to stay in the prison all his life.\n");
<span style="white-space:pre">		</span>else
<span style="white-space:pre">		</span>printf("%d\n", min);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}


题意:给一个迷宫,求起点到终点的时间,如果遇到警卫单位时间加1,输出最小的时间,若不能到达 则输出。。。。

思路:用广搜+有限队列来做,由于有警卫的存在所以要用优先队列来求出每个点到起点的最小距离。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: