NOJ - T050
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】C 程序設計課程
[
9 瀏覽
0 評論
2 贊
2 踩
]
概要
完美矩陣
正文
此題需要注意,題目所說「完美矩陣」乃長寬一致的方陣也。
#include<stdio.h> #include<stdlib.h> #include<math.h> #define Check1(pMatrix, idx) {if (!*(pMatrix + idx)) return false;} //#define MY_DEBUG bool IsPerfectMatrix(int m, int n, int* pMatrix, int x, int y, int w, int h) { int lt = n * y + x; int rt = n * y + x + w - 1; int lb = n * (y + h - 1) + x; int rb = n * (y + h - 1) + x + w - 1; for (int i = lt; i <= rt; i++) Check1(pMatrix, i); for (int i = lb; i <= rb; i++) Check1(pMatrix, i); for (int i = lt; i <= lb; i += n) Check1(pMatrix, i); for (int i = rt; i <= rb; i += n) Check1(pMatrix, i); if (!(*(pMatrix + lt) && *(pMatrix + rt) && *(pMatrix + lb) && *(pMatrix + rb))) return false; int ones = 0, zeros = 0; for (int j = y + 1; j < y + h - 1; j++) { for (int i = x + 1; i < x + w - 1; i++) { *(pMatrix + n * j + i) ? ones++ : zeros++; } } #ifdef MY_DEBUG printf("\n---\n"); for (int j = y; j < y + h; j++) { for (int i = x; i < x + w; i++) { printf("%d ", *(pMatrix + n * j + i)); } printf("\n"); } #endif int dif = ones - zeros; if (dif == 1 || dif == -1 || dif == 0) return true; else return false; } int main() { int m, n; scanf("%d %d", &m, &n); //int a[m][n]; int* a = (int*)malloc((long long)m * n * sizeof(int)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", a + i * n + j); } } int sum = 0; for (int w = 2, h = 2; h <= m && w <= n; w++, h++) { for (int x = 0; x + w - 1 < n; x++) { for (int y = 0; y + h - 1 < m; y++) { sum += IsPerfectMatrix(m, n, (int*)a, x, y, w, h); } } } printf("%d", sum); free(a); return 0; }