B「木」迷雾森林
题目地址:
基本思路:
比较经典的的简单dp思路,从最下往右上递推就是了具体递推式如下:
当这个位置不是树时 ,是树的话跳过,注意取模。
参考代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define IO std::ios::sync_with_stdio(false) #define ll long long #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, _) memset(s, _, sizeof(s)) #define pb push_back #define pii pair <int, int> #define mp(a, b) make_pair(a, b) #define INF 0x3f3f3f3f inline int read() { int x = 0, neg = 1; char op = getchar(); while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); } while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); } return neg * x; } inline void print(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } const int maxn = 3050; int m,n,a[maxn][maxn]; int dp[maxn][maxn]; const int mod = 2333; signed main() { m = read(),n = read(); rep(i,1,m){ rep(j,1,n) a[i][j] = read(); } mset(dp,0);//初始是0; dp[m][1] = 1; for(int i = m ;i >= 1 ; i--){ for(int j = 1; j <= n; j++){ if(a[i][j] == 1) continue;//是树就跳过; if(i + 1 <= m) dp[i][j] += dp[i + 1][j]; dp[i][j] %= mod; if(j - 1 >= 1) dp[i][j] += dp[i][j - 1]; dp[i][j] %= mod; } } cout << dp[1][n] % mod << '\n'; return 0; }