题目链接: 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;
}
- 递推思路