哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)
https://ac.nowcoder.com/acm/contest/301#question
F.小乐乐下象棋
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const long long mod = 1000000007; int n, m, k; int sx, sy; int ex, ey; long long dp[220][220][220]; int dir[8][2] = { {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2} }; int out(int x, int y) { if(x <= 0 || x > n) return 1; if(y <= 0 || y > m) return 1; return 0; } int main() { while(~scanf("%d%d%d", &n, &m, &k)) { memset(dp, 0, sizeof(dp)); dp[1][1][0] = 1; for(int step = 1; step <= k; step ++) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { for(int d = 0; d < 8; d ++) { int ii = i + dir[d][0]; int jj = j + dir[d][1]; if(out(ii, jj)) continue; dp[i][j][step] = (dp[i][j][step] + dp[ii][jj][step - 1]) % mod; } } } } cout << dp[n][m][k] << endl; } return 0; }
(判断是否可以 k 步走到还可以剪枝 明天再写)
#include <bits/stdc++.h> using namespace std; int n, m, k; int mp[110][110]; int vis[110][110]; int sx, sy; int ex, ey; bool flag = false; int dir[8][2] = { {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2} }; void dfs(int x, int y, int step) { if(x == ex && y == ey && step == k) { flag = true; return ; } for(int i = 0; i < 8; i ++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(xx >= 0 && xx < n && yy >= 0 && yy < m && step < k) { vis[xx][yy] = 1; dfs(xx, yy, step + 1); vis[xx][yy] = 0; } } } int main() { scanf("%d%d%d", &n, &m, &k); sx = 0, sy = 0; ex = n - 1, ey = m - 1; memset(vis, 0, sizeof(vis)); vis[sx][sy] = 1; dfs(0, 0, 0); if(flag) printf("yes\n"); else printf("no\n"); return 0; }