Uva221矩形个数判断
2016-04-03 18:37
323 查看
这题我本来想的是如何储存的,在储存中寻找解决方案,结果在一番思考毫无思路以后,在google中发现了方案很简单。
存数组,在读取时判断。
这里的判断方法也很巧妙,采用的是数组循环,加l进行正方形的长度递增。
存数组,在读取时判断。
#include <cstdio> #include <iostream> #include <cstring> using namespace std; int H[10][10], V[10][10]; int main() { //freopen("cin", "r", stdin); //freopen("cout", "w", stdout); int n, m, T = 0; while (~scanf("%d %d", &n, &m)) { getchar(); memset(H, 0, sizeof(H)); memset(V, 0, sizeof(V)); for (int i = 0; i < m; i++) { int a, b; char c; scanf("%c%d%d", &c, &a, &b); getchar(); if (c == 'H') H[a][b] = 1; else V[b][a] = 1; } if (T++) printf("\n**********************************\n\n"); printf("Problem #%d\n\n", T); int sum = 0; for (int l = 1; l <= n; l++) { int count = 0, flag = 0; for (int i = 1; i + l <= n; i++) for (int j = 1; j + l <= n; j++) { flag = 1; for (int h = j; h < j + l; h++) if (!H[i][h] || !H[i + l][h]) flag = 0; for (int v = i; v < i + l; v++) if (!V[v][j] || !V[v][j + l]) flag = 0; count += flag; } sum += count; if (count) printf("%d square (s) of size %d\n", count, l); } if (!sum) printf("No completed squares can be found.\n"); } return 0; }
这里的判断方法也很巧妙,采用的是数组循环,加l进行正方形的长度递增。
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析