HDU 1242 RESCUE

很久不做题了。。早上找状态。。水一个

DFS(15MS):

#include<stdio.h>

int N,M;
char map[200][200];
char c;
int ai,aj,ri,rj;

int min;

int mintime[200][200];

int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};		//0:x, 1:y

int inarea(int x,int y)
{
	return x>=0 && y>=0 && x<N && y<M && map[x][y]!='#';
}

void dfs(int x,int y,int time,int count)
{
	mintime[x][y]=time;
	
	for (int i=0; i<4; i++) {
		if (inarea(x+move[i][0], y+move[i][1]) && time+1+(map[x+move[i][0]][y+move[i][1]]=='x'? 1:0)<mintime[x+move[i][0]][y+move[i][1]]) {
			dfs(x+move[i][0], y+move[i][1],time+1+(map[x+move[i][0]][y+move[i][1]]=='x'? 1:0),count+1);
		}
	}
}

int main()
{
	while (~scanf("%d%d",&N,&M)) {
		
		for (int i=0; i<N; i++) {
			for (int j=0; j<M; j++) {
				mintime[i][j]=100000000;
			}
		}
		
		getchar();
		for (int i=0; i<N; i++) {
			for (int j=0; j<M; j++) {
				c=getchar();
				if (c=='a') {
					ai=i;
					aj=j;
					map[i][j]='.';
				}
				else if (c=='r') {
					ri=i;
					rj=j;
					map[i][j]='.';
				}
				else {
					map[i][j]=c;
				}

			}
			getchar();
		}
		dfs(ri,rj,0,0);
		
		if(mintime[ai][aj]==100000000) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",mintime[ai][aj]);
		
	}
}

BFS(15MS):

#include<stdio.h>

int N,M;
char map[200][200];
char c;
int ai,aj,ri,rj;

int min;

int mintime[200][200];
int mincount[200][200];

int queue_i[40000];
int queue_j[40000];
int queue_time[40000];
int queue_count[40000];

int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};		//0:x, 1:y

int inarea(int x,int y)
{
	return x>=0 && y>=0 && x<N && y<M && map[x][y]!='#';
}

void bfs()
{
	int head=0,tail=0;
	queue_i[tail]=ri;
	queue_j[tail]=rj;
	queue_time[tail]=0;
	queue_count[tail++]=0;
	while (head<tail) {
		for (int i=0; i<4; i++) {
			if (inarea(queue_i[head]+move[i][0],queue_j[head]+move[i][1]) && mintime[queue_i[head]+move[i][0]][queue_j[head]+move[i][1]]>queue_time[head]+1+((map[queue_i[head]+move[i][0]][queue_j[head]+move[i][1]]=='x')? 1:0)) {
				queue_i[tail]=queue_i[head]+move[i][0];
				queue_j[tail]=queue_j[head]+move[i][1];
				queue_time[tail]=queue_time[head]+1;
				if (map[queue_i[head]+move[i][0]][queue_j[head]+move[i][1]]=='x') {
					queue_time[tail]++;
				}
				queue_count[tail]=queue_count[head]+1;
				
				mintime[queue_i[head]+move[i][0]][queue_j[head]+move[i][1]]=queue_time[tail];
				mincount[queue_i[head]+move[i][0]][queue_j[head]+move[i][1]]=queue_count[tail];
				
				tail++;
			}
		}
		head++;
	}
}

int main()
{
	while (~scanf("%d%d",&N,&M)) {
		
		for (int i=0; i<N; i++) {
			for (int j=0; j<M; j++) {
				mintime[i][j]=100000000;
			}
		}
		
		getchar();
		for (int i=0; i<N; i++) {
			for (int j=0; j<M; j++) {
				c=getchar();
				if (c=='a') {
					ai=i;
					aj=j;
					map[i][j]='.';
				}
				else if (c=='r') {
					ri=i;
					rj=j;
					map[i][j]='.';
				}
				else {
					map[i][j]=c;
				}

			}
			getchar();
		}
		bfs();
		
		if(mintime[ai][aj]==100000000) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",mintime[ai][aj]);
		
	}
}
posted on 2011-07-07 08:49  Eucalyptus  阅读(194)  评论(0编辑  收藏  举报