123456fish @ 2024-11-23 23:24:19
求助,只会输出1,悲
题目https://www.luogu.com.cn/problem/P1644
#include "bits/stdc++.h"
using namespace std;
int n, m, f[25][25];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] += 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] += f[i + 1][j + 2] + f[i + 2][j + 1];
}
}
cout << f[n][m];
return 0;
}
by akaryan @ 2024-11-24 11:39:10
用dfs会更好一些
我的代码:
#include<bits/stdc++.h>
using namespace std;
int n , m , ans = 0 , dx[8] = {-2 , -1 , 1 , 2 , 2 , 1 , -1 , -2} , dy[8] = {1 , 2 , 2 , 1 , -1 , -2 , -2 , -1};
void dfs(int u , int v , int cost) {
if (u == m && v == n) {
ans ++;
return;
}
for (int i = 0 ; i < 8 ; i ++) {
int nx = u + dx[i] , ny = v + dy[i];
if (nx >= 0 && nx <= m && ny >= 0 && ny <= n && nx >= u)
dfs(nx , ny , cost + 1);
}
}
int main() {
cin >> n >> m;
dfs(0 , 0 , 0);
cout << ans << endl;
return 0;
}