SGU 239 Minesweeper 模拟题

题目链接:点击打开链接




#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1000 + 10;

map<int, int> cnt;
ll d[N][4];
int a[N];

int main() {
	int n, g, to;
	ll ans;
	for (int i = 0; i < 4; ++i) {
		g = 0;
		for (int j = 0; j < 2; ++j)
			if (i >> j & 1)
				++ g;
		cnt[i] = g;
	}
	while (~scanf("%d", &n)) {
		memset(d, 0, sizeof d);
		for (int i = 1; i <= n; ++i)
			scanf("%d", &a[i]);
		if (1 == n) {
			if (a[1] == 1 || a[1] == 0)
				puts("1");
			else
				puts("0");
			continue;
		}
		if (a[1] == 1)
			d[1][1] = d[1][2] = 1;
		else if (a[1] == 0)
			d[1][0] = 1;
		else if (a[1] == 2)
			d[1][3] = 1;
		for (int i = 1; i < n; ++i)
			for (int j = 0; j < 4; ++j)
				if (d[i][j] > 0) {
					g = cnt[j];
					if (a[i + 1] == g) {
						to = (j << 1) & 3;
						d[i + 1][to] += d[i][j];
					} else if (a[i + 1] == g + 1) {
						to = (j << 1 | 1) & 3;
						d[i + 1][to] += d[i][j];
					}
				}
		ans = 0;
		for (int i = 0; i < 4; ++i) 
			if (cnt[i] == a[n]) {
				ans += d[n - 1][i];
			}
		
		cout << ans << endl;
	}
	return 0;
}


你可能感兴趣的:(SGU 239 Minesweeper 模拟题)