2016年第七届蓝桥杯C/C++ C组国赛 —— 第二题:反幻方
生活随笔
收集整理的這篇文章主要介紹了
2016年第七届蓝桥杯C/C++ C组国赛 —— 第二题:反幻方
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
反幻方
我國古籍很早就記載著
2 9 4
7 5 3
6 1 8
這是一個三階幻方。每行每列以及對角線上的數字相加都相等。
下面考慮一個相反的問題。
可不可以用 1~9 的數字填入九宮格。
使得:每行每列每個對角線上的數字和都互不相等呢?
這應該能做到。
比如:
9 1 2
8 4 3
7 5 6
你的任務是搜索所有的三階反幻方。并統計出一共有多少種。
旋轉或鏡像算同一種。
比如:
9 1 2
8 4 3
7 5 6
7 8 9
5 4 1
6 3 2
2 1 9
3 4 8
6 5 7
等都算作同一種情況。
請提交三階反幻方一共多少種。這是一個整數,不要填寫任何多余內容。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/ // VB_king —— 2016_Finals_C_C++_2.cpp created by VB_KoKing on 2019-05-13:08. /* Procedural objectives:Variables required by the program:Procedural thinking:一個反幻方的鏡像加旋轉總共8次。Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <algorithm> #include <iostream> #include <cstring> #include <set>using namespace std;int num[9],sum[8];void print(){for (int i = 0; i < 9; i++) {if (i%3==0) cout<<endl;cout<<num[i]<<' ';} }bool check(){memset(sum,0, sizeof(sum));for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++)sum[j]+=num[i+j*3];for (int j = 0; j < 3; j++)sum[j+3]+=num[3*i+j];sum[6]+=num[4*i];sum[7]+=num[2*(i+1)];}set <int> s;for (int i = 0; i < 8; i++)s.insert(sum[i]);if (s.size()==8) return true;return false; }int main(){int ans=0;for (int i = 1; i < 10; i++)num[i-1]=i;do {if (check()){ // print();ans++; // cout<<endl;}}while (next_permutation(num,num+9));cout<<endl<<ans/8<<endl;return 0; }總結
以上是生活随笔為你收集整理的2016年第七届蓝桥杯C/C++ C组国赛 —— 第二题:反幻方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2016年第七届蓝桥杯 - 国赛 - J
- 下一篇: 动态规划 —— 01背包问题