Problem description Xiaoming is playing a game recently. It consists of N * M matrices, which randomly generate some black points, these vertices may be connected together or separated. There is no limit on the number of these vertices, but at most one black spot may exist in each 1*1 square. The game requires players to overwrite the large matrix with a small x * y matrix in the shortest time. The requirements for coverage are as follows:
1. A small matrix of x * y must contain x * y black spots.
2. Multiple Small matrices can overlap, but each small matrix must be placed in a unique position, that is, the black spots in different small matrices cannot be exactly the same. For example, a matrix of 1*2 can be placed either horizontally or vertically. The two methods are different, even if they may share black spots.
Xiao Ming is a careless child. He has tried many times and cannot find all qualified small matrices. You are smart, can you tell me how many small matrices meet the requirements in this big matrix? Practice: The key is to calculate the area of each rectangle. At first, the two-dimensional tree array was used to calculate the time-out. In fact, the recursive method can be used to quickly pre-process the rectangle )~ (I, j) area. Next, enumerate each qualified rectangle and determine whether the area is equal to x * Y. View code
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-03-23-21.36 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream>10 #include <cmath>11 #include <cstring>12 #include <algorithm>13 #include <string>14 #include <utility>15 #include <vector>16 #include <queue>17 #include <stack>18 #include <map>19 #include <set>20 using namespace std;21 22 typedef long long ll;23 #define DEBUG(x) cout<< #x << ':' << x << endl24 #define REP(i,n) for(int i=0;i < (n);i++)25 #define REPD(i,n) for(int i=(n-1);i >= 0;i--)26 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)27 #define FORD(i,s,t) for(int i = (s);i >= (t);i--)28 #define PII pair<int,int>29 #define PB push_back30 #define MP make_pair31 #define ft first32 #define sd second33 #define lowbit(x) (x&(-x))34 #define INF (1<<30)35 36 int n,m;37 int C[2011][2011];38 char str[2011][2011];39 40 41 void fun(){42 C[0][0] = 0;43 FOR(i,1,n){44 C[i][0] = 0;45 FOR(j,1,m){46 C[i][j] = C[i][j-1]+(str[i][j]=='*'?1:0);47 }48 }49 FOR(i,2,n){50 FOR(j,1,m){51 C[i][j] += C[i-1][j];52 }53 }54 }55 int Area(int x1,int y1,int x2,int y2){56 if(x1>x2)swap(x1,x2);57 if(y1>y2)swap(y1,y2);58 return C[x2][y2]-C[x2][y1-1]-C[x1-1][y2]+C[x1-1][y1-1];59 }60 61 int main()62 {63 //freopen("in","r",stdin);64 //freopen("out","w",stdout);65 while(~scanf("%d%d",&n,&m),n,m){66 int x,y;67 scanf("%d%d",&x,&y);68 FOR(i,1,n)scanf("%s",str[i]+1);69 fun();70 int ans = 0;71 FOR(i,1,n){72 FOR(j,1,m){73 if(str[i][j] != '*')continue;74 int xx1 = i+x-1;75 int yy1 = j+y-1;76 if(!(xx1<1||yy1<1||xx1>n||yy1>m)){77 if(Area(i,j,xx1,yy1) == x*y)ans++;78 79 }80 81 int xx2 = i+y-1;82 int yy2 = j+x-1;83 if(xx2 == xx1 && yy2==yy1)continue;84 if(xx2<1||yy2<1||xx2>n||yy2>m)continue;85 if(Area(i,j,xx2,yy2) == x*y)ans++;86 87 }88 }89 printf("%d\n",ans);90 }91 return 0;92 }