hdu 1242 Rescue [bfs+ 우선 순위 대기열(c 언어 시뮬레이션)]

3652 단어 ACM-- 검색HDUOJ

#include
#include
#include
using namespace std;
#define N 210
#define inf 0x3f3f3f3f
char map[N][N];
int book[N][N],m,n,si,sj,ans;

struct node{
	int x,y,time;
}q[N*N];

int cmp(struct node a,struct node b)
{
	return a.time < b.time ;
}

void bfs()
{
	int i,j,flag;
	int k[4][2] = {0,1,0,-1,1,0,-1,0};
	int tail,head;
	struct node now;
	ans = inf;
	tail = head = 0;//c       
	q[tail].x = si;
	q[tail].y = sj;
	q[tail].time = 0;
	tail ++;
	flag = 0;
	while(head < tail)
	{
		for(i = 0; i < 4; i ++)
		{
			now.x = q[head].x + k[i][0];
			now.y = q[head].y + k[i][1];
			now.time = q[head].time ;
			if(now.x < 1||now.y < 1||now.x > n||now.y > m)
				continue;
			if(!book[now.x][now.y]&&map[now.x][now.y]!='#')
			{
				book[now.x][now.y] = 1;
				if(map[now.x][now.y] == 'x')//  x  +2 
					now.time = now.time +2;
				else
					now.time = now.time +1;
				q[tail].x = now.x ;
				q[tail].y = now.y ;
				q[tail++].time = now.time ;
				if(map[q[tail-1].x][q[tail-1].y] == 'r')
				{
					ans = q[tail-1].time ;//          
					flag = 1;
				}
			}
		}
		head ++;
		sort(q,q+tail,cmp);//      
		if(flag)
			break;
	} 
	return;
}

int main()
{
	int j,i;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(book,0,sizeof(book));
		for(i = 1; i <= n; i ++)
		{
			scanf("%s",map[i]+1);
			for(j = 1; j <= m; j ++)
			{
				if(map[i][j] == 'a')
				{
					si = i;
					sj = j;
					book[i][j] = 1;
				}
			}
		}
		bfs();
		if(ans == inf)
			printf("Poor ANGEL has to stay in the prison all his life.
"); else printf("%d
",ans); } return 0; }

Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32607    Accepted Submission(s): 11400
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
 
제목: r에서 a까지의 최단 시간을 찾을 수 없습니다. "Poor ANGEL has to stay in the prison all his life."를 출력합니다.'.'를 만나면,시간+1,'x'를 만나면 시간+2.
사고방식: r가 여러 개가 있기 때문에 우리는 a에서 r를 찾고 가장 짧은 시간을 요구한다. 그러면 우리가 검색할 때 다음 단계로 갈 때마다 가장 적은 시간을 찾아야 하기 때문에 c++의 우선 대기열을 사용하지만 이번에는 c로 시뮬레이션한 우선 대기열이다.

좋은 웹페이지 즐겨찾기