先贴个题目:
以及原题链接:P1002 [NOIP2002 普及组] 过河卒 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P1002 经典dp题好吧,也没啥好说的,注意下int会爆,开个long long即可。代码如下:
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 30;
int ex, ey, hx, hy;
bool map[N][N];
bool st[N][N];
ll dp[N][N];
int cnt;
int main()
{
cin >> ex >> ey >> hx >> hy;
ex++;
ey++;
hx++;
hy++;
map[hx][hy] = true;
map[hx-2][hy-1]=true;
map[hx-1][hy-2]=true;
map[hx + 1][hy - 2] = true;
map[hx + 2][hy - 1] = true;
map[hx + 2][hy + 1] = true;
map[hx + 1][hy + 2] = true;
map[hx - 1][hy + 2] = true;
map[hx-2][hy+1]=true;
dp[1][1] = 1;
for (int i = 1; i < ex + 1;++i)
for (int j = 1; j < ey + 1;++j)
{
if(i==1&&j==1)
continue;
if(map[i][j])
{
dp[i][j] = 0;
continue;
}
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
cout << dp[ex][ey];
return 0;
}
一个技巧是为了避免越界,直接把所有坐标加一,这样就不会出现越界访问了。
by————2024.4.18刷题记录