洛谷P1515 旅行

题面

输入可转化到存在大于等于14小于等于34个宾馆,位置不重复且都在0~7000km内,要求一次最少行进Akm,最多行进Bkm,问有多少种行进方案

分析

宾馆数量很少,并且假设当前处于宾馆u,到u的路径和u之后的路径并不存在关系(位置的状态是固定的),和dp有些类似,就可使用类似的方法做,但本质上还是暴力

可以从第一个宾馆(0位置)开始递推,其中满足加法原理,即如果只存在A→C,且B→C,则从A到C的方案数就是到A和到B的方案数之和

代码

#include "cstdlib"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<algorithm>
#include <string>
#include<vector>
using namespace std;
int pos[35] = { 0,990,1010,1970,2030,2940,3060,3930,4060,4970,5030,5990,6010,7000 };
int f[35];//f[i]代表到i的方案数
int main()
{
	ios::sync_with_stdio(false);
	int a, b, n;
	cin >> a >> b >> n;
	for (int i = 14; i < n+14; i++)cin >> pos[i];
	n = n + 14;//元素真实个数
	sort(pos, pos + n);
	f[0] = 1;//到第0个宾馆(位置0)的方案数为1
	for (int i = 1; i < n; i++)
	{
		for (int j = 0; j < i; j++)
		{
			if (pos[i] - pos[j] <= b && pos[i] - pos[j] >= a)f[i] += f[j];
		}
	}
	cout << f[n - 1];
	return 0;
}
发布了32 篇原创文章 · 获赞 0 · 访问量 1190

猜你喜欢

转载自blog.csdn.net/engineoid/article/details/104199415