当前位置:网站首页>蓝桥杯 方格填数
蓝桥杯 方格填数
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;
}
边栏推荐
- openjudge:大小写字母互换
- Review of metallurgical physical chemistry --- electrodeposition and reduction process of metals
- Delete specific elements in order table OJ
- Simpledateformat thread unsafe and datetimeformatter thread safe
- SVG了解与绘图应用
- JVM notes 3: class loading and bytecode Technology
- Event_looop-事件循环机制
- 函数基础知识以及特殊点
- 图片根据屏幕自适应
- 冶金物理化学复习 --- 冶金反应动力学基础与多相反应动力学特征
猜你喜欢

GD32F407 移植FreeRTOS+Lwip
![[idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea](/img/d6/4e69480c5ad5040ee48941ca0fcb37.png)
[idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea

How Visio accurately controls the size, position and angle of graphics

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

Shell operation principle

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

Review of metallurgical physical chemistry --- electrodeposition and reduction process of metals

wangeditor(@4.7.15)-轻量级的富文本编辑器

环形链表问题

冶金物理化学复习 --- 液 - 液相反应动力学
随机推荐
ArcGIS之Model Builder
Idea uses dev tool to realize hot deployment
Arrangement of main drawings of the latest 54 papers of eccv22
Centos7 install MySQL 5.7
openjudge:石头剪刀布
media-搭建直播服务器
基于Highcharts平台的桑基图(Sankey diagram)绘制
js数据类型检测与修改检测
openjudge:找出全部子串位置
wangeditor(@4.7.15)-轻量级的富文本编辑器
GET与POST区别
Idea configures the service (run dashboard) service, and multiple modules are started at the same time
C语言回顾(字节对齐篇)
ArcMap连接表格(Join)相关问题整理
openjudge:统计数字字符个数
记录某某小卢的第一篇文章
DOM——页面的渲染、style属性操作、预加载与懒加载、防抖与节流
树莓派蓝牙调试过程
截图传入后台
Docker deploy mysql5.7.35