TYVJ 1117 拯救ice-cream 解题报告

  果断直接SPFA,f[i][j]代表到达(i,j)的时间,f[i][j] = f[a][b] + num[i][j] (a, b) 是(i, j)相邻的..

  代码:

#include <stdio.h>
#include <stdlib.h>
int map[25][25];
#define MAX 4000
struct box{
	int x, y;
}queue[MAX];
int head, rear;
int used[25][25];
int dis[25][25];
int m, n;

void enqueue(int x, int y)
{
	if(used[x][y]){
		return;
	}
	used[x][y] = 1;
	queue[rear].x = x;
	queue[rear].y = y;
	rear = (rear + 1) % MAX;
}

void exqueue(int *x, int *y)
{
	*x = queue[head].x;
	*y = queue[head].y;
	head = (head + 1) % MAX;
	used[*x][*y] = 0;
}

int check(int a, int b)
{
	if(a < 0 || a >= n || b < 0 || b >= m){
		return 0;
	}
	return 1;
}

int main(int argc, char **argv)
{
	int t;
	int i, j;
	int x, y;
	char ch;
	scanf("%d%d%d\n", &t, &m, &n);
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++){
			dis[i][j] = 0xFFFFFF;
			scanf("%c", &ch);
			switch(ch){
			case '.': map[i][j] = 1;break;
			case '#': map[i][j] = 2;break;
			case 'o': map[i][j] = 0xFFFFFF;break;
			case 's': enqueue(i, j); break;
			case 'm': map[i][j] = 1;x = i; y = j; break;
			}
		}
		scanf("\n");
	}
	dis[queue[0].x][queue[0].y] = 0;
	while(head != rear){
		exqueue(&i, &j);
#define deal(a, b) if(check(a, b) && dis[a][b] > dis[i][j] + map[a][b]){\
	enqueue(a, b);\
	dis[a][b] = dis[i][j] + map[a][b];\
}
		deal(i - 1, j);
		deal(i + 1, j);
		deal(i, j - 1);
		deal(i, j + 1);
	}
	if(dis[x][y] < t){
		printf("%d\n", dis[x][y]);
	}else{
		printf("55555\n");
	}
	return 0;
}

  

posted @ 2011-08-07 17:33  zqynux  阅读(234)  评论(0编辑  收藏  举报