Python——【USACO 3.3.4】——Home on the Range

Home on the Range

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  

"""
ID : mcdonne1
LANG : PYTHON
TASK : range
"""
fin = open ('range.in', 'r')
fout = open ('range.out', 'w')
cnt = [0 for i in range (252)]
g = [[0 for i in range (252)] for j in range (252)]
r = fin.readlines()
n = int(r[0])
for i in range (n) :
	for j in range (n) :
		g[i][j] = int(r[i+1][j])
for i in range (n)[::-1] :
	for j in range (n)[::-1] :
		if g[i][j] != 0 :
			g[i][j] = 1 + min (g[i+1][j], g[i][j+1], g[i+1][j+1])
for i in range (n) :
	for j in range (n) :
		if g[i][j] >= 2 :
			for k in range(2, g[i][j] + 1) :
				cnt[k] += 1
for i in range (2, n + 1) :
	if cnt[i] > 0 :
		fout.write (str(i) + ' ' + str(cnt[i]) + '\n')
fin.close()
fout.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值