某一个位置上的途径数其实源于左边和下面,也就是说某一个位置只来自左边和下边。那么动态方程就可以出来了。
又有可以使用BFS来走这个地图所以在BFS的过程中加入动态规划记录累计的途径数就可以了。
#include <bits/stdc++.h>
using namespace std;
int read() {
int ans=0, k = 1;
char ch = getchar();
while (ch=='-') {
k = -k;
ch = getchar();
}
while (ch>='0'&&ch<='9') {
ans = ans*10 + (ch-'0');
ch = getchar();
}
return ans*k;
}
const int maxn = 3000+10;
int n, m;
int a[maxn][maxn];
int dp[maxn][maxn];
int vis[maxn][maxn];
int mod = 2333;
int mv[2][2] = {
{-1,0},
{0,1}
};
void BFS() {
queue<pair<int, int> > q;
q.push({n, 1});
dp[n][1] = 1;
while (!q.empty()) {
pair<int, int> p = q.front();q.pop();
for (int i=0;i<2;i++) {
int next_x = p.first+mv[i][0];
int next_y = p.second+mv[i][1];
if (next_x<=0||next_x>n||next_y<=0||next_y>m||a[next_x][next_y]==1) continue;
dp[next_x][next_y] = dp[next_x][next_y]%mod + dp[p.first][p.second]%mod;
if (vis[next_x][next_y]) {
continue;
}
q.push({next_x, next_y});
vis[next_x][next_y] = 1;
}
}
}
int main() {
n = read();m = read();
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
a[i][j] = read();
}
}
BFS();
cout<<dp[1][m]%mod;
return 0;
}

京公网安备 11010502036488号