这道题,没什么,就查找正方形, 特别注意一下 \n 的使用。
//UVa 201 //Square //#define LOCAL #include <stdio.h> #include <string.h> //use memset int dot[11][11][11][11], N;int getS(int size) {int cnt = 0, flag;for(int i = 1; i <= N-size; i++)for(int j = 1; j <= N-size; j++) { flag = 0;int x = i, y = j;for(int k = 1; k <= size; k++) {if(!dot[x][y][x][y+1]) flag = 1;y++; }x = i; y = j; for(int k = 1; k <= size; k++) {if(!dot[x][y][x+1][y]) flag = 1;x++; }x = i + size; y = j; for(int k = 1; k <= size; k++) {if(!dot[x][y][x][y+1]) flag = 1; y++;}x = i; y = j + size;for(int k = 1; k <= size; k++) {if(!dot[x][y][x+1][y]) flag = 1; x++; } if(!flag) { // printf("%d %d\n", i, j);cnt++;}}return cnt; }int main() { #ifdef LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); #endif int cnt = 0, n, ans, f;while(scanf("%d", &N) == 1) {memset(dot, 0, sizeof(dot));scanf("%d", &n);char s[5]; int x, y;for(int i = 0; i < n; i++) { scanf("%s%d%d", s, &x, &y);if(s[0] == 'H') dot[x][y][x][y+1] = 1;else dot[y][x][y+1][x] = 1; }if(cnt > 0) printf("\n**********************************\n\n");printf("Problem #%d\n\n", ++cnt);f = 0;for(int i = 1; i <= N; i++) {ans = getS(i); if(ans) { f = 1; printf("%d square (s) of size %d\n", ans, i); }}if(!f) printf("No completed squares can be found.\n");}return 0; }