USACO home on the range C 语言实现
来源:互联网 发布:ubuntu第一次怎么进入 编辑:程序博客网 时间:2024/05/18 00:13
题目大意:
农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛。(因为一些原因,他的奶牛只在正方形的牧场上吃草。)遗憾的是,他的奶牛已经毁坏一些土地。( 一些1平方英里的正方形)
农民约翰需要统计那些可以放牧奶牛的正方形牧场(至少是2×2的,在这些较大的正方形中没有一个点是被破坏的,也就是说,所有的点都是“1”)。
你的工作要在被供应的数据组里面统计所有不同的正方形放牧区域(>=2×2)的个数。当然,放牧区域可能是重叠。
PROGRAM NAME: range
INPUT FORMAT:
(file range.in)
第 1 行:N,牧区的边长。
第 2 到 n+1行:N个没有空格分开的字符。
0 表示 “那一个区段被毁坏了”;1 表示 ” 准备好被吃”。
OUTPUT FORMAT:
(file range.out)
输出那些存在的正方形的边长和个数,一种一行。
SAMPLE INPUT
SAMPLE INPUT (file range.in)
6
101111
001111
111111
001111
101101
111001
SAMPLE OUTPUT (file range.out)
2 10
3 4
4 1
这道题可以动态规划
二维的动态规划。
使用一个二维数组map,首先用input数据进行初始化。
同时,在dp的填表的过程中,不断改变map的值。
状态定义:map[i][j]为以(i,j)为左上角顶点的正方形的最大边长。
状态转移方程: map[i][j]=min{ map[i+1][j] , map[i][j+1] , map[i+1][j+1] } + 1;
map[i+1][j] ,map[i][j+1] , map[i+1][j+1]分别为 向下、向右、向右下一格。如果3个中有一个出现0,及不可扩充,那么map[i][j]的值将为1.
从最右下向上,依次扩充即可。
/*ID: abc18711LANG: CTASK: range*/#include <stdio.h>#include <string.h>#define MAXN 251int map[MAXN][MAXN];int squares[MAXN]; // squares[2]=1 represents 2*2's square has 1 int n;int min(int a, int b, int c){int d = a;if(d > b) d = b;if(d > c) d = c;return d;}int dp(){int i, j;int k;for(i=n-1; i>0; i--)for(j=n-1; j>0; j--){if(map[i][j] > 0)map[i][j] = min(map[i+1][j], map[i][j+1], map[i+1][j+1]) + 1;if(map[i][j] > 1)for(k=2; k<=map[i][j]; k++)squares[k]++;}}int main(){int i, j;char ch;FILE *fin = fopen("range.in", "r");FILE *fout = fopen("range.out", "w");memset(squares, 0, sizeof(squares));fscanf(fin, "%d", &n);for(i=1; i<=n; i++)for(j=1; j<=n; j++){ch = 10;while(ch==10 || ch==13)fscanf(fin, "%c", &ch);map[i][j] = ch -48;}dp();for(i=2; i<=n; i++){if(squares[i])fprintf(fout,"%d %d\n", i, squares[i]);}fclose(fin);fclose(fout);return 0;}
- USACO home on the range C 语言实现
- usaco Home on the Range
- USACO Home on the Range
- USACO Home on the Range
- usaco Home on the Range
- USACO 3.3 Home on the Range (range)
- usaco Home on the Range java ---DP
- usaco 3.3 Home on the Range
- USACO Home on the Range 解题报告
- USACO Section 3.3 Home on the Range
- USACO--3.3Home on the Range+DP
- usaco Chapter 3 section 3.3 Home on the Range
- USACO Section 3.3 Home on the Range - 优化的BFS..
- USACO section3.3 Home on the Range(压缩+枚举)
- USACO-Section 3.3-PROB Home on the Range
- Home on the Range
- USACO3.3.4 Home on the Range (range)
- USACO3.3.4 Home on the Range (range)
- 详解大端模式和小端模式
- 最后那3分——ER模型到关系模型的转换
- 17.1.1.1 Setting the Replication Master Configuration
- Android进程级别与如何防止服务进程被回收
- OC之非正式协议总结
- USACO home on the range C 语言实现
- Unity3d中Shader的基本属性以及定义
- 字符串哈希到整数函数,算法
- Java实现字符串中的连续数字的反转
- windows下tomcat+solr+ZooKeeper集群搭建
- Android开发是sqlite数据库使用adb shell在cmd下查看
- JSON字符串解析(得到属性的值)
- HDU 4035 Maze
- 海量数据前N大