标签:uva
题意:给出一些单位线段, 问各种大小的正方形有多少个。要控制输出格式。
解题思路:模拟存储横线和竖线,暴力枚举各种尺寸,O(N ^ 4),此题数据弱, 实际所用时间很短。
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19423
Memory: 0 KB | Time: 16 MS | |
Language: C++ 4.8.2 | Result: Accepted |
#include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cctype> #include<list> #include<iostream> #include<map> #include<queue> #include<set> #include<stack> #include<vector> using namespace std; #define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i) #define REP(i, n) for(int i = 0 ; i < (n) ; ++i) int buf[10]; inline long long read() { long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } inline void writenum(int i) { int p = 0; if(i == 0) p++; else while(i) { buf[p++] = i % 10; i /= 10; } for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]); } /**************************************************************/ #define MAX_N 11 const int INF = 0x3f3f3f3f; int h[MAX_N][MAX_N]; int v[MAX_N][MAX_N]; int main() { int n, m; char op[5]; int cas = 1; while(~scanf("%d%d", &n, &m)) { memset(h, 0, sizeof(h)); memset(v, 0, sizeof(v)); for(int i = 0 ; i < m ; i++) { scanf("%s", &op); int x = read(); int y = read(); if(op[0] == 'H') { h[x][y] = 1; } else { v[y][x] = 1; } } int ok = 0; if(cas != 1) printf("\n**********************************\n\n"); printf("Problem #%d\n\n", cas++); for(int i = 1 ; i <= n ; i++)//正方形的size { int cnt = 0; for(int j = 1 ; j + i <= n ; j++)// { for(int k = 1 ; k + i <= n ; k++)// { int flag = 1; for(int r = k ; r < k + i ; r++) { if(!h[j][r] || !h[j + i][r]) { flag = 0; break; } } for(int c = j ; c < j + i ; c++) { if(!v[c][k] || !v[c][k + i]) { flag = 0; break; } } if(flag) { cnt++; ok = 1; } } } if(cnt) printf("%d square (s) of size %d\n", cnt, i); } if(!ok) printf("No completed squares can be found.\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:uva
原文地址:http://blog.csdn.net/qwe585p/article/details/48087055