Topic Links:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1835
Main topic:
Give you an n-order matrix that determines whether n-order magic squares. The numbers used are not necessarily 1~n^2, but
The numbers must not be duplicated.
Problem Solving Ideas:
According to the definition of magic square, traverse and judge all the rows, vertical lines, oblique and equal, and use the flag[] array
To determine if a duplicate number exists.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int map[20][20],sum[40],flag[1100];int Judge (int N) {memset (sum,0,sizeof (Sum)); memset (flag,0,sizeof (Flag)); for (int i = 0, i < n; ++i) for (int j = 0; j < N; ++j) {if (! FLAG[MAP[I][J]]) flag[map[i][j] = 1; else return 0; } for (int i = 0, i < n; ++i) {for (int j = 0; j < N; ++j) sum[i] + = map[i][j]; } for (int i = 0, i < n; ++i) {for (int j = 0; j < N; ++j) sum[i+n] + = Map[j][i]; } for (int i = 0; i < N; ++i) sum[n+n] + = Map[i][i]; for (int i = 0; i < N; ++i) sum[n+n+1] + = map[i][n-1-i]; int tmp = sum[0]; for (int i = 0; I <= 2*n+1; ++i) if (sum[i]! = tmp) return 0; return 1;} int main () {int N; while (~SCANF ("%d", &n) && N) {for (int i = 0; i < N ++i) for (int j = 0; j < N; ++j) scanf ("%d", &map[i][j]); if (Judge (N)) printf ("yes\n"); else printf ("no\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ2835 Magic Square "magic square Verification"