SRM 614

这个矩阵很好构造出来,然后算期望要高斯消元,直接算肯定超时,有神奇的方法。


但是大家都迭代的我也先把迭代的丢过来吧。。。。(等不懒了再想高斯消元,会有这一天么0 0

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

class TorusSailing {
public:
	double expectedTime(int, int, int, int);
};
double p[105][105];
double TorusSailing::expectedTime(int n, int m, int goalX, int goalY) {
	int i, j, k;
	memset(p, 0, sizeof(p));
	for (k = 0; k < 10005; ++k) {
		for (i = 0; i < n; ++i) {
			for (j = 0; j < m; ++j) {
				if (i == 0 && j == 0)
					continue;
				p[i][j] = 1 + 0.5 * (p[(i - 1 + n) % n][j] + p[i][(j - 1 + m)
						% m]);
			}
		}
	}
	return p[goalX][goalY];
}


你可能感兴趣的:(topcoder,SRM)