大家帮忙看一下,这道题目和最小生成树有什么关系?

idler 2001-10-09 08:28:27
Farmer John grazes his cows on a large, square field N (2 <= N <= 250) miles on a side (because, for some reason, his cows will only graze on precisely square land segments). Regrettably, the cows have ravaged some of the land (always in 1 mile square increments). FJ needs to map the remaining squares (at least 2x2 on a side) on which his cows can graze (in these larger squares, no 1x1 mile segments are ravaged).

Your task is to count up all the various square grazing areas within the supplied dataset and report the number of square grazing areas (of sizes >= 2x2) remaining. Of course, grazing areas may overlap for purposes of this report.

PROGRAM NAME: range
INPUT FORMAT
Line 1: N, the number of miles on each side of the field.
Line 2..N+1: N characters with no spaces. 0 represents "ravaged for that block; 1 represents "ready to eat".

SAMPLE INPUT (file range.in)
6
101111
001111
111111
001111
101101
111001

OUTPUT FORMAT
Potentially several lines with the size of the square and the number of such squares that exist. Order them in ascending order from smallest to largest size.

SAMPLE OUTPUT (file range.out)
2 10
3 4
4 1

其实就是给出一个01矩阵,找出其中所有全为1的正方形区域的面积以及个数。
USACO把它放在最小生成树的Section里面。
大概是我才疏学浅,但我实在看不出这和最小生成树有什么关系。
...全文
159 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
idler 2001-10-11
  • 打赏
  • 举报
回复
我也这么认为,而且它下面的一道题目"Calf Flac"也应该放在动规Section里
starfish 2001-10-11
  • 打赏
  • 举报
回复
答案里面也没有提到最小生成树呀,肯定是搞错了,应该放在动态规划的section里才对,不过这个动态规划比较简单。最小生成树应该是属于贪心算法。
idler 2001-10-10
  • 打赏
  • 举报
回复
再把答案页贴上。
不过我还是没有看出它和最小生成树有什么关系。

To count the squares, we first precompute the biggest square with lower right corner at any particular location. This is done by dynamic programming: the biggest square with lower right corner at (i, j) is the minimum of three numbers:

the number of consecutive uneaten grid units to the left
the number of consecutive uneaten grid units to the right
one plus the size of the biggest square with lower right corner at (i-1, j-1)
Once we've computed this information, counting squares is simple: go to each lower right corner and increment the counters for every square size between 2 and the biggest square ending at that corner.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXN 250

int goodsq[MAXN][MAXN];
int bigsq[MAXN][MAXN];
int tot[MAXN+1];

int
min(int a, int b)
{
return a < b ? a : b;
}

void
main(void)
{
FILE *fin, *fout;
int i, j, k, l, n, sz;

fin = fopen("range.in", "r");
fout = fopen("range.out", "w");
assert(fin != NULL && fout != NULL);

fscanf(fin, "%d\n", &n);

for(i=0; i<n; i++) {
for(j=0; j<n; j++)
goodsq[i][j] = (getc(fin) == '1');
assert(getc(fin) == '\n');
}

/* calculate size of biggest square with lower right corner (i,j) */
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
for(k=i; k>=0; k--)
if(goodsq[k][j] == 0)
break;

for(l=j; l>=0; l--)
if(goodsq[i][l] == 0)
break;

sz = min(i-k, j-l);
if(i > 0 && j > 0)
sz = min(sz, bigsq[i-1][j-1]+1);

bigsq[i][j] = sz;
}
}

/* now just count squares */
for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=2; k<=bigsq[i][j]; k++)
tot[k]++;

for(i=2; i<=n; i++)
if(tot[i])
fprintf(fout, "%d %d\n", i, tot[i]);

exit(0);
}

Greg Price writes:

The posted solution runs in cubic time, with quadratic storage. With a little more cleverness in the dynamic programming, the task can be accomplished with only quadratic time and linear storage, and the same amount of code and coding effort. Instead of running back along the rows and columns from each square, we use the biggest-square values immediately to the west and north, so that each non-ravaged square's biggest-square value is one more than the minimum of the values to the west, north, and northwest. This saves time, bringing us from cubic to quadratic time.

Another improvement, which saves space and perhaps cleans up the code marginally, is to keep track of the number of squares of a given size as we go along. This obviates the need to keep a quadratic-size matrix of biggest-square values, because we only need the most recent row for continuing the computation. As for "ravaged" values, we only use each one once, all in order; we can just read those as we need them.

#include <fstream.h>

ifstream fin("range.in");
ofstream fout("range.out");

const unsigned short maxn = 250 + 5;

unsigned short n;
char fieldpr;
unsigned short sq[maxn]; // biggest-square values
unsigned short sqpr;
unsigned short numsq[maxn]; // number of squares of each size

unsigned short
min3(unsigned short a, unsigned short b, unsigned short c)
{
if ((a <= b) && (a <= c))
return a;
else
return (b <= c) ? b : c;
}

void
main()
{
unsigned short r, c;
unsigned short i;
unsigned short tmp;

fin >> n;

for (c = 1; c <= n; c++)
sq[c] = 0;

for (i = 2; i <= n; i++)
numsq[i] = 0;

for (r = 1; r <= n; r++)
{
sqpr = 0;
sq[0] = 0;
for (c = 1; c <= n; c++)
{
fin >> fieldpr;
if (!(fieldpr - '0'))
{
sqpr = sq[c];
sq[c] = 0;
continue;
}

// Only three values needed.
tmp = 1 + min3(sq[c-1], sqpr, sq[c]);
sqpr = sq[c];
sq[c] = tmp;

// Only count maximal squares, for now.
if (sq[c] >= 2)
numsq[ sq[c] ]++;
}
}

// Count all squares, not just maximal.
for (i = n-1; i >= 2; i--)
numsq[i] += numsq[i+1];

for (i = 2; i <= n && numsq[i]; i++)
fout << i << ' ' << numsq[i] << endl;
}

第6行的right好像应该是upper,我写信过去问了。

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧