用广度搜索解动态规划题——HDOJ 1176 免费馅饼

Problem Description
都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:



为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)

Input
输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。

Output
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。


Sample Input
6
5 1
4 1
6 1
7 2
7 2
8 3
0

Sample Output

4

上代码

#include<stdio.h>
#define M 100000
int n, i, j, a, b, max, map[100000][11], book[100000][11], head, tail, m, sum=0, max_t=0;
typedef struct node{
	int x, y;
	int step;
}Node;
Node que[1000];
int bfs();
int main()
{
	while(scanf("%d", &n) && n) {
		for(i = 0; i < n; i++) {
			scanf("%d %d", &a, &b);
			map[b][a]++;
			if(max_t < b) {
				max_t = b;
			}
		}
		//
		head = tail = 1;
		for(i = 0; i <= 10; i++) {
			if(map[1][i] != 0) {
				que[tail].x = 1;
				que[tail].y = i;
				que[tail].step = map[1][i];
				book[1][i] = 1;
				tail++;
			}
		}
		bfs();
		printf("%d\n", sum);
		//sum += que[head].step;
	}
	
	return 0;
}
int bfs()
{
	int next[3][2] = {{1, 0}, {1, -1}, {1, 1}};//原地 左xia 右xia  
	int tx, ty;
	while(head < tail) {
		for(i = 0; i < 3; i++) {
		tx = que[head].x + next[i][0];
		ty = que[head].y + next[i][1];
		if(book[tx][ty] != 1 && tx <= max_t && ty>=0 && ty <= 10 && tx >= 1) {
				que[tail].x = tx;
				que[tail].y = ty;
				que[tail].step = que[head].step + map[tx][ty];
				book[tx][ty] = 1;
				tail++;		
		}
	}
	head++;
	//printf(" %d %d %d\n" ,que[head].x,que[head].y, que[head].step);
	} 
	for(i = tail; i >= 0 ; i-- ) {
		if(que[i].step > sum) {
			sum = que[i].step;
		}
	}
}


猜你喜欢

转载自blog.csdn.net/dk1543100966/article/details/75007308