|
Problem E |
Kites |
|
Time Limit |
4 Seconds |
The season of flying kites is well ahead. So what? Let us make an inventory for kites. We are given a square shaped sheet of paper. But many parts of this are already porous. Your challenge here is to count the total number of ways to cut a kite of any size from this sheet. By the way, the kite itself can't be porous :-) AND..................it must be either square shaped or diamond shaped.
x
x xxx xxx xxx
xxx xxxxx xxx x.x x
x xxx xxx xxx
x
In the above figure first three are valid kites but not next two.
Input
Input contains an integer n (n ≤ 500), which is the size of the sheet. Then follows n lines each of which has n characters ('x' or '.'). Here the dotted parts resemble the porous parts of the sheet. Input is terminated by end of file.
Output
Output is very simple. Only print an integer according to the problem statement for each test case in a new line.
|
Sample Input |
Output for Sample Input |
|
4 |
4 6 |
Problemsetter: Mohammad Sajjad Hossain
Bangladesh University of Engineering and Technology
#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
using namespace std;
const int maxn = 500 + 5;
const int INF = 1000000000;
typedef long long LL;
typedef pair<int, int> P;
#define fi first
#define se second
int dps[maxn][maxn], dpd[maxn][maxn];
char maze[maxn][maxn];
int main(){
int n;
while(scanf("%d", &n) != EOF){
memset(maze, -1, sizeof maze);
for(int i = 1;i <= n;i++){
scanf("%s", maze[i]+1);
}
memset(dps, 0, sizeof dps);
memset(dpd, 0, sizeof dpd);
LL ans = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(maze[i][j] == 'x'){
dps[i][j] = min(dps[i-1][j], dps[i][j-1]);
int len = dps[i][j];
if(maze[i-len][j-len]=='x')
dps[i][j]++;
ans += dps[i][j]-1;
len = min(dpd[i-1][j-1], dpd[i-1][j+1]);
if(len == 0 || maze[i-1][j] != 'x')
dpd[i][j] = 1;
else{
if(maze[i-len-1][j] == 'x' && maze[i-len][j] == 'x')
dpd[i][j] = len+2;
else
dpd[i][j] = len;
}
ans += dpd[i][j]/2;
}
}
}
printf("%lld\n", ans);
}
return 0;
}
本文解决了一个关于从带有孔洞的方形纸张中切割不同大小风筝的问题。通过动态规划的方法,作者展示了如何计算出所有有效的风筝形状数量,包括正方形和菱形风筝。详细解释了算法实现过程,并提供了关键代码片段,帮助读者理解如何通过避免重复计算来优化算法效率。
3051

被折叠的 条评论
为什么被折叠?



