题解 ABC293C【Make Takahashi Happy】

样例二已经告诉我们路径数最多为 \(48620=\binom{18}{9}\)(九步向右走、九步向下走,任意排列)。这个路径数是非常少的,考虑暴力。

首先将所有数离散化,然后可以塞到一个大小为 \(10^2\) 的 bitset 中,在搜索过程中利用 bitset 判断经过的数是否重复即可。

// Problem: C - Make Takahashi Happy
// Contest: AtCoder - AtCoder Beginner Contest 293
// URL: https://atcoder.jp/contests/abc293/tasks/abc293_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
	uniform_int_distribution<int> dist(L, R);
	return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

const int N = 10;

int n, m, a[N][N], buc[N*N], tot, ans;
bitset<N*N> qwq;

void dfs(int x, int y, bitset<N*N>& now) {
	if(x == n - 1 && y == m - 1) {++ans; return;}
	if(x < n - 1 && !now.test(a[x+1][y])) {
		now.set(a[x+1][y]);
		dfs(x+1, y, now);
		now.reset(a[x+1][y]);
	}
	if(y < m - 1 && !now.test(a[x][y+1])) {
		now.set(a[x][y+1]);
		dfs(x, y+1, now);
		now.reset(a[x][y+1]);
	}
}

int main() {
	scanf("%d%d", &n, &m);
	rep(i, 0, n-1) {
		rep(j, 0, m-1) {
			scanf("%d", &a[i][j]);
			buc[tot++] = a[i][j];
		}
	}
	sort(buc, buc+tot);
	tot = unique(buc, buc+tot) - buc;
	rep(i, 0, n-1) rep(j, 0, m-1) a[i][j] = lower_bound(buc, buc+tot, a[i][j]) - buc;
	qwq.set(a[0][0]);
	dfs(0, 0, qwq);
	printf("%d\n", ans);
	return 0;
}
posted @ 2023-03-12 08:23  rui_er  阅读(69)  评论(0编辑  收藏  举报