题目:http://acm.hdu.edu.cn/showproblem.php?pid=2563
AC代码(C++):
#include <iostream> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; int n; bool vis[100][100]; int ans; void dfs(int x, int y, int step) { if (step == n) { ans++; return; } vis[x][y] = true; if (!vis[x][y - 1]) { dfs(x, y - 1, step + 1); } if (!vis[x][y + 1]) { dfs(x, y + 1, step + 1); } if (!vis[x + 1][y]) { dfs(x + 1, y, step + 1); } vis[x][y] = false; } int main() { int t; cin >> t; while (t--) { ans = 0; cin >> n; memset(vis, false, sizeof(vis)); dfs(50, 50, 0); cout << ans << endl; } //system("pause"); }总结: 看了网上的解题报告说这题答案可以通过递推得到, 怪我太菜没有想到, 不过用深搜还是能勉强AC. 题意是四个方向有一个方向不能走, 并且走过的地方不能再走, 只需根据这个分3个方向深搜就可以了, 只要代码足够简洁还是能AC的.