White Rectangles

Bertha 。 2022-06-17 05:59 189阅读 0赞

You are given a chessboard made up of N squares by N squares with equal size. Some of the squares are colored black, and the others are colored white. Please write a program to calculate the number of rectangles which are completely made up of white squares.

Input

There are multiple test cases. Each test case begins with an integer N (1 <= N <= 100), the board size. The following N lines, each with N characters, have only two valid character values:

# - (sharp) representing a black square;
. - (point) representing a white square.

Process to the end of file.

Output

For each test case in the input, your program must output the number of white rectangles, as shown in the sample output.

Sample Input

2
.#
..
4
..#.
##.#
.#..
.#.#

Sample Output

5
15

题意:地图中找出由 点 构成的矩形的个数

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. #define inf 0x3f3f3f3f
  6. char a[110][110];
  7. int coun[110][110];
  8. int n;
  9. void A()
  10. {
  11. for(int i=1;i<=n;i++)
  12. {
  13. for(int j=1;j<=n;j++)
  14. {
  15. if(a[i][j]=='.')
  16. coun[i][j]=coun[i][j-1]+1;
  17. //printf("%d ",coun[i][j]);
  18. }
  19. //printf("\n");
  20. /*
  21. 1 2 0 1
  22. 0 0 1 0
  23. 1 0 1 2
  24. 1 0 1 0
  25. */
  26. }
  27. }
  28. int AA()
  29. {
  30. int ans=0;
  31. for(int i=1;i<=n;i++)
  32. {
  33. for(int j=1;j<=n;j++)//
  34. {
  35. int m=inf;
  36. for(int l=i;l<=n&&coun[l][j]!=0;l++)//1列1列的判断,点不连续就结束
  37. {
  38. m=min(m,coun[l][j]);
  39. ans+=m;
  40. }
  41. }
  42. }
  43. return ans;
  44. }
  45. int main()
  46. {
  47. while(scanf("%d",&n)!=EOF)
  48. {
  49. memset(coun,0,sizeof(coun));
  50. for(int i=1;i<=n;i++)
  51. scanf("%s",a[i]+1);
  52. A();
  53. printf("%d\n",AA());
  54. }
  55. return 0;
  56. }

发表评论

表情:
评论列表 (有 0 条评论,189人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS white-space

    在html里面多余的空格和换行都是无效的,如果想展示格式话的数据,就需要用到css 的 white-space属性了。 -------------------- white