hdu 4405 Aeroplane chess

概率dp的水题

#include <CSTDIO>
#include <STRING.H>
#include <MATH.H>
#include <VECTOR>
using namespace std;
// hdu 4405
/*
	e[n] = 0;
	line[i]==-1... e[i] = 1 + 1/6*({i<j<=i+6 && j<= n }Σe[j]);
	line[i]!=-1... e[i] = e[line[i]]
*/
const int MAXN = 100010;
const double six = 1.0/6.0;
int n, m;
double e[MAXN];
int line[MAXN];
int main()   
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
	int i, j, k;
	while (scanf("%d%d", &n, &m) && n+m)
	{
		memset(line, -1, sizeof line);
		for ( i = 0; i< m; ++i) 
			scanf("%d%d", &j, &k), line[j] = k;
		e[n] = 0.0;
		for (i = n-1; i>= 0; --i)
		{
			if (line[i] != -1)
				e[i] = e[line[i]];
			else
			{
				e[i] = 0.0;
				for (j = 1; j <= 6 && i+j <= n ; ++j)
					e[i] += e[i+j];
				e[i] = 1.0 + e[i] * six;
			}
		}
		printf("%.4lf\n", e[0]);
	}
	return 0;
}


你可能感兴趣的:(hdu 4405 Aeroplane chess)