2017蓝桥杯九宫幻方(C++B组)

题目:九宫幻方


    小明最近在教邻居家的小朋友小学奥数,而最近正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不重复的填入一个3*3的矩阵当中,使得每一行、每一列和每一条对角线的和都是相同的。

    三阶幻方又被称作九宫格,在小学奥数里有一句非常有名的口诀:“二四为肩,六八为足,左三右七,戴九履一,五居其中”,通过这样的一句口诀就能够非常完美的构造出一个九宫格来。

4 9 2
3 5 7
8 1 6


    有意思的是,所有的三阶幻方,都可以通过这样一个九宫格进行若干镜像和旋转操作之后得到。现在小明准备将一个三阶幻方(不一定是上图中的那个)中的一些数抹掉,交给邻居家的小朋友来进行还原,并且希望她能够判断出究竟是不是只有一个解。

    而你呢,也被小明交付了同样的任务,但是不同的是,你需要写一个程序~


输入格式:
输入仅包含单组测试数据。
每组测试数据为一个3*3的矩阵,其中为0的部分表示被小明抹去的部分。
对于100%的数据,满足给出的矩阵至少能还原出一组可行的三阶幻方。


输出格式:
如果仅能还原出一组可行的三阶幻方,则将其输出,否则输出“Too Many”(不包含引号)。


样例输入
0 7 2
0 5 0
0 3 0


样例输出
6 7 2
1 5 9
8 3 4


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms




请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。


注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。


提交程序时,注意选择所期望的语言类型和编译器类型。


--------------


笨笨有话说:
    我最喜欢这类题目了。既然九宫幻方一共也没有多少,我就不辞辛劳地一个一个写出来好了。

也不能太过分,好歹用个数组。


代码:

    #include<iostream>


using namespace std;


#define M 50


int stand[3][3] = {4,9,2,3,5,7,8,1,6};
int all[M][3][3] = {0};
int order = 0;


void all_way();
void conversion(int array[3][3]);
void view();
void input();
bool is_same(int array[3][3]);


int main(int argc,char** argv){

all_way();

input();
}


void input(){

bool jud = true;
int nums = 0;
int nums_same = 0;
int state_same;

int test[3][3];

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

cin >> test[i][j];
}
}

for(int order_ = 0;order_ < order;order_++){

nums = 0;

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

if(test[i][j] == 0){

nums++;

}else{

if(test[i][j] == all[order_][i][j]){

nums++;

}else{

jud = false;
}
}

if(nums == 9){

nums_same++;
state_same = order_;
}
}
}
}

if(nums_same == 0){

cout << "no same" << endl;

}else if(nums_same == 1){

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

cout << all[state_same][i][j] << "  ";
}

cout << endl;
}

}else{

cout << "Too many" << endl;
}
}


void view(){

for(int dns = 0;dns < order;dns++){

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

cout << all[dns][i][j] << "  ";
}

cout << endl;
}

cout << endl << endl;
}
}


void all_way(){

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = stand[i][j];
}
}

order++;

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = stand[2 - j][i];
}
}
order++;

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = stand[2 - i][2 - j];
}
}
order++;

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = stand[j][2 - i];
}
}
order++;

conversion(all[0]);
conversion(all[1]);
conversion(all[2]);
conversion(all[3]);
}


bool is_same(int array[3][3]){

int nums = 0;
int sign = 0;

for(int sub = 0;sub < order;sub++){

nums = 0;
sign = 0;

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

if(array[i][j] == all[sub][i][j]){

sign++;
}
nums++;
}
}

if(nums == 9){

if(sign == 9){

return true;;
}
}
}

return false;
}




void conversion(int array[3][3]){

int array_temp1[3][3];
int array_temp2[3][3];
int nums = 0;
bool jud[2];

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

array_temp1[i][j] = array[i][2 - j];
}
}

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

array_temp2[i][j] = array[2 - i][j];
}
}

if(!is_same(array_temp1)){

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = array_temp1[i][j];
}
}
order++;
}

if(!is_same(array_temp2)){

for(int i = 0;i < 3;i++){

for(int j = 0;j < 3;j++){

all[order][i][j] = array_temp2[i][j];
}
}

order++;
}

}




原文地址:https://www.cnblogs.com/viplanyue/p/12700746.html