踩方格
时间限制: 1000 ms 内存限制: 65536 KB
【题目描述】
有一个方格矩阵,矩阵边界在无穷远处。我们做如下假设:
- 每走一步时,只能从当前方格移动一格,走到某个相邻的方格上;
- 走过的格子立即塌陷无法再走第二次;
- 只能向北、东、西三个方向走;
请问:如果允许在方格矩阵上走 n n n 步,共有多少种不同的方案。 2 2 2 种走法只要有一步不一样,即被认为是不同的方案。
【输入】
允许在方格上行走的步数 n ( n ≤ 20 ) n(n≤20) n(n≤20)
【输出】
计算出的方案数量。
【输入样例】
2
【输出样例】
7
/*** Amber ***/
#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <algorithm>
//#include <unordered_map>
#include <vector>
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
template <typename T>
inline void read(T &x) {
x = 0;
static int p;
p = 1;
static char c;
c = getchar();
while (!isdigit(c)) {
if (c == '-')p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - 48);
c = getchar();
}
x *= p;
}
template <typename T>
inline void print(T x) {
if (x<0) {
x = -x;
putchar('-');
}
static int cnt;
static int a[50];
cnt = 0;
do {
a[++cnt] = x % 10;
x /= 10;
} while (x);
for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
puts("");
}
const double Pi=acos(-1);
const double eps=1e-6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+10;
ll n;
bool vis[110][110];
ll dfs(int x,int y,int n) {
if (!n) return 1;
int res = 0;
vis[x][y] = 1;
if (!vis[x - 1][y]) res += dfs(x - 1, y, n - 1);
if (!vis[x][y - 1]) res += dfs(x, y - 1, n - 1);
if (!vis[x][y + 1]) res += dfs(x, y + 1, n - 1);
vis[x][y] = 0;
return res;
}
inline void work() {
read(n);
print(dfs(25, 25, n));
}
int main() {
//freopen("1.txt","r",stdin);
int T = 1;
//read(T);
while (T--) {
work();
}
return 0;
}