当前位置:网站首页>Blue Bridge Cup square filling number
Blue Bridge Cup square filling number
2022-07-28 07:02:00 【White mouse zero】
Fill in the squares
As follows 10 Lattice
fill 0~9 The number of . requirement : Two consecutive numbers cannot be adjacent .
( about 、 Up and down 、 The opposite angles are all adjacent )
How many kinds of possible filling schemes are there ?

answer
1580
Their thinking
The topic should be requirements 0~9 It's not repeated . Then I want to pass a[10] take 0~9 Make a full arrangement , Then assign the value to the two-dimensional array m[3][4]. thus m[][] Record the number of each square . I just need to write judgement sentences . This is for next_permutation() Make a full arrangement , It's usually used do-while() Add next_permutation() Can complete the full arrangement , And can do-while Write the conditions to be satisfied by the full arrangement in the sentence .
Attach code
#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;
}
边栏推荐
猜你喜欢

NFS shared storage service

DNS forward resolution experiment

JSON notes

QGraphicsView提升为QChartView

What is the most practical gift for Tanabata? A gift that will never go wrong is worth buying

KVM热迁移

Common models in software development

技术分享 | 接口测试价值与体系

MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组

bond模式配置
随机推荐
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
Detailed explanation of LNMP construction process
Custom component -- communication between parent and child components
Custom component -- pure data field & component life cycle
单元测试框架Jest搭配TypeScript的安装与配置
Software testing (concept)
Custom components -- slots
Hdu-1097-a hard puzzle (fast power)
[learning notes] linked list operation
Build php7 private warehouse
360 compatibility issues
VSphere esxi 7.0 update 3 release notes
Method of designing test cases
手把手教你三步完成测试监控系统搭建
Archery database audit platform deployment
LNMP搭建过程详解
iptables防火墙
Technology sharing | do you know the functions of the server interface automated testing and requests library?
QGraphicsView提升为QChartView
Technology sharing | send requests using postman