当前位置:网站首页>蓝桥杯 方格填数
蓝桥杯 方格填数
2022-07-28 05:19:00 【小白鼠零号】
方格填数
如下的10个格子
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?

答案
1580
解题思路
题目应该是要求0~9是不重复的。那么我想通过a[10]将0~9进行全排列,然后将值赋给二维数组m[3][4]。从而m[][]记录每个方格的数字。我只要写判断语句就行了。这里用来next_permutation()进行全排列,一般用do-while()加next_permutation()就能完成全排列,并且能在do-while语句中书写全排列要满足的条件。
附上代码
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
const int M=100010;
int a[M]={0};
int m[3][4]={0};
int ans=0;
int main()
{
bool is_noteven();
m[0][0]=m[2][3]=20;
for(int i=0;i<10;i++)
a[i]=i;
do{
m[0][1]=a[0];m[0][2]=a[1];m[0][3]=a[2];
m[1][0]=a[3];m[1][1]=a[4];m[1][2]=a[5];m[1][3]=a[6];
m[2][0]=a[7];m[2][1]=a[8];m[2][2]=a[9];
if(is_noteven())
ans++;
}while(next_permutation(a,a+10));
cout<<ans<<endl;
return 0;
}
bool is_noteven()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
int aij=m[i][j];
if(i-1>=0&&j-1>=0&&j+1<=3)
{
if(abs(aij-m[i-1][j]==1))
return false;
if(j-1>=0)
{
if(abs(aij-m[i-1][j-1])==1)
return false;
}
if(j+1<=3)
{
if(abs(aij-m[i-1][j+1])==1)
return false;
}
}
if(i+1<=2)
{
if(j-1>=0)
{
if(fabs(aij-m[i+1][j-1])==1)
return false;
}
if(j+1<=3)
{
if(fabs(aij-m[i+1][j+1])==1)
return false;
}
if(fabs(aij-m[i+1][j])==1)
return false;
}
if(j-1>=0)
{
if(fabs(aij-m[i][j-1])==1)
return false;
}
if(j+1<=3)
{
if(fabs(aij-m[i][j+1])==1)
return false;
}
}
}
return true;
}
边栏推荐
- You must configure either the server or JDBC driver (via the ‘serverTimezone)
- 【博学谷学习记录】超强总结,用心分享 | 集合
- GIS领域竞赛整理(不完全统计)
- Shell operation principle
- Docker deploy mysql5.7.35
- 【uni-app】uni-app中scroll-into-view的使用
- ArcMap连接表格(Join)相关问题整理
- DOM操作的案例
- Interface idempotency problem
- A file upload tool website written by individuals
猜你喜欢

Advanced multithreading: the role and implementation principle of volatile

分支与循环语句

树莓派串口配置

Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade

GIS领域竞赛整理(不完全统计)

Thesis writing function words

Custom JSON return data

DOM——页面的渲染、style属性操作、预加载与懒加载、防抖与节流

Review of metallurgical physical chemistry -- cathodic polarization, overpotential, anode and anode process in metal electrodeposition

Sequence table OJ topic
随机推荐
链表实现增删查改
JVM notes 3: class loading and bytecode Technology
Advanced multithreading: the role and implementation principle of volatile
Openjudge: find all substring positions
Simpledateformat thread unsafe and datetimeformatter thread safe
JS字符串方法大全
冶金物理化学复习 --- 气-液相反应动力学
DOM--事件链、事件冒泡和捕获、事件代理
对极大似然估计、梯度下降、线性回归、逻辑回归的理解
Low illumination image data set
openjudge:找第一个只出现一次的字符
openjudge:矩阵乘法
Oracle view lock table statement and unlocking method
Review of metallurgical physical chemistry --- liquid liquid reaction kinetics
[MySQL] solve the problem of MySQL time zone and 8-hour difference in database time
Docker deploy mysql5.7.35
DOM——页面的渲染、style属性操作、预加载与懒加载、防抖与节流
How Visio accurately controls the size, position and angle of graphics
Mysql database index (InnoDB engine)
Arrangement of main drawings of the latest 54 papers of eccv22