ZJYYCOJ-路线方案数

题目链接: http://acm.oinsm.com/problem.php?cid=1022&pid=1

在这里插入图片描述
在这里插入图片描述

思路

比赛的时候,我看到这题,emmmm,一个递推题?,也许可以搜一下找规律,emmmm,n * n 的正方形,好像有点不好搜啊!!
晚上,又看了一边题目,2 * n, !!!!想都不想,直接搜一下找规律!!

  • 暴搜找规律
1 : 2
2 : 8
3 : 16 = 8 + 8
4 : 28 = 16 + 8 + 4
5 : 44 = 28 + 8 + 4 + 4
6 : 64		...
7 : 88		...
8 : 116		...
9 : 148		...
10 : 184	...
这规律应该好找的,1的时候要特判,后面都是由4开始递增的!

暴搜,打表输出!

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 155;
const int INF = 1e9;
bool vis[3][MAXN];
int n;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
long long ans;
bool check(int x, int y) {
    return !(x < 1 || x > 2 || y < 1 || y > n || vis[x][y]);
}
void dfs(int x, int y, int cnt) {
    // cout << x << " " << y << " " << cnt << endl;
    if(cnt == 2 * n){
        ans++;
        return ;
    }
    if(x == y && y == -1) {
        for(int i = 1; i <= 2; i++){
            for(int j = 1; j <= n; j++){
                if(vis[i][j]) continue;
                vis[i][j] = true;
                dfs(i, j, cnt + 1);
                vis[i][j] = false;
            }
        }
    }
    else {
        for(int i = 0; i < 4; i++){
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if(check(xx, yy)) {
                vis[xx][yy] = true;
                dfs(xx, yy, cnt + 1);
                vis[xx][yy] = false;
            }
        }
    }
}
void getans(int m) {
    for(int i = 1; i <= m; i++){
        n = i;
        fill(vis[0], vis[0] + (MAXN * 3), false);
        ans = 0;
        dfs(-1, -1, 0);
        cout << i << " : " << ans << endl;
    }
}
long long num[100005];		// 什么递推题,dp题,直接无脑上long long
int main()
{
    ios::sync_with_stdio(false);
//    getans(10);
    num[1] = 2;
    num[2] = 8;
    int now = 8;
    for(int i = 3; i <= 100000; i++){
        num[i] = num[i - 1] + now;
        now += 4;
    }
    while(cin >> n){
        cout << num[n] << endl;
    }
    return 0;
}
  • 递推思路
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值