点击打开链接
思路:
飞巨给我讲的,说是模拟填数,要想最大以贪心的思想就是把n*n的放在对角线上,然后到下一个对角线最近要走两步,
然后继续填,然后去发现规律,(剩下最后一个对角线上的点就不填了,因为要保证按顺序)
PS:我也求了一个验证代码去跑了一下
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+10; typedef long long ll; ll n,sum,k; int main() { while(~scanf("%lld",&n)) { k=n*n; if(n==1) sum=1; else sum=0; for(int i=1;i<n;i++) { sum+=k; if(i!=n-1) k-=2; } sum+=k/2; printf("%lld\n",sum); } return 0; }
dfs验证 只能跑出6.7个
要想保证连续填,就四个方向去dfs填数
#include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<queue> #include<stack> #include<math.h> #include<string> #include<algorithm> #include<map> using namespace std; int d[4][2] = {1,0,0,1,-1,0,0,-1}; int r[100][100]; int book[100][100]; int rs; int n; void solve(int x,int y,int t); int main(){ while(scanf("%d",&n)!=EOF){ for(int i=0;i<n/2+1;i++){ for(int j=0;j<n/2+1;j++){ memset(book,0,sizeof(book)); book[i][j] = 1; solve(i,j,1); } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("M",r[i][j]); } printf("\n"); } printf("r %d\n",rs); } return 0; } void solve(int x,int y,int t){ if(t == n*n){ int temp1 = 0; int temp2 = 0; for(int i=0;i<n;i++) temp1 += book[i][i]; for(int i=0,j=n-1;i<n,j>=0;i++,j--) temp2 += book[i][j]; if(temp1 > rs || temp2 > rs){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ r[i][j] = book[i][j]; } } rs = max(temp1,temp2); } return ; } for(int i=0;i<4;i++){ int tx = x + d[i][0]; int ty = y + d[i][1]; if(tx >= 0 && tx < n && ty >= 0 && ty < n && book[tx][ty] == 0){ book[tx][ty] = t+1; solve(tx,ty,t+1); book[tx][ty] = 0; } } }
Marcus-Bao 认证博客专家 推荐系统 ACM算法竞赛 机器学习 本科毕业于国内知名四非大学,现中国科学院大学博士生,中国科学院计算技术研究所vipl实验室,老年ACM铁牌退役选手,喜欢算法竞赛,会点数据结构和算法,熟悉c++,python等;现阶段研究方向主要为机器学习与数据挖掘,比较关注推荐系统,发过顶会,炼过丹,平时博客主要记录些关于算法、数据结构,人工智能技术以及平时看的论文总结分享等,欢迎大家关注我,一起多多交流共同进步!