usaco range java
来源:互联网 发布:vb .net boolean 判断 编辑:程序博客网 时间:2024/06/05 03:48
/*ID: daniel.20LANG: JAVATASK: range*/import java.util.*;import java.io.*;public class range { static int[][] table; static int num; static int[] result; static int[][] start; static void search(int a,int b){ for(int i=start[a][b];i<num;i++){ if(a+i>=num||b+i>=num) return; int flag = 1; for(int j=0;j<=i;j++){ if(table[a+j][b+i]!=1||table[a+i][b+j]!=1){ flag = 0; break; } } if(flag==1) { result[i+1]++; for(int k=i-1;k>=1;k--){ int update = i-k+1; for(int j=0;j<=k;j++){ if(start[a+j][b+k]<update){ start[a+j][b+k] = update; result[update]++; } if(start[a+k][b+j]<update){ start[a+k][b+j] = update; result[update]++; } } } } else break; } } static void work() throws IOException{ PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("range.out"))); for(int i=0;i<num;i++){ for(int j=0;j<num;j++){ if(table[i][j]!=1) continue; search(i,j); } } for(int i=0;i<251;i++){ if(result[i]!=0) { out.println(i+" "+result[i]); } } out.close(); } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new FileReader("range.in")); num = Integer.parseInt(reader.readLine()); table = new int[num][num]; result = new int[251]; start = new int[num][num]; for(int i=0;i<num;i++){ Arrays.fill(start[i], 1); String s = reader.readLine(); for(int j=0;j<num;j++){ table[i][j] = s.charAt(j)-'0'; } } work(); }}
题目思路不是很难,就是对每个点一层层递推.
开始就想到了有重复计算的部分,是可以优化的.
比如说一个点推到第三层,那么我们有一个3×3的方框,其实呢我们还有3个新的2×2的方框.
所以result[3]+1, result[2]+3. 并且有3个点下次从第三层开始看,第二层被计算过了。
开始懒了,没写优化的部分,最后一个case就是卡你的,不写优化刚好过不去.
写了速度就唰一下过去了。。。
Test 1: TEST OK [0.065 secs, 274688 KB] Test 2: TEST OK [0.065 secs, 274688 KB] Test 3: TEST OK [0.065 secs, 274688 KB] Test 4: TEST OK [0.065 secs, 274688 KB] Test 5: TEST OK [0.101 secs, 275712 KB] Test 6: TEST OK [0.245 secs, 276736 KB] Test 7: TEST OK [0.317 secs, 276736 KB]这题可能是这个section最简单的....
- usaco range java
- USACO--range
- usaco--range
- usaco Home on the Range java ---DP
- USACO/range 3.3.4
- USACO 3.3 Home on the Range (range)
- usaco Home on the Range
- USACO Home on the Range
- USACO Home on the Range
- usaco 3.3 range 2008.11.7
- usaco Home on the Range
- Java Range
- USACO算法序列十七——range
- 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
- 多线程
- windows程序逆向工具汇总
- myeclipse10中导入的jquery文件报错(出现红叉叉,提示语法错误)
- [转] C语言的谜题
- 正则表达式
- usaco range java
- syncback 30天xx
- Java连接MySQL数据库并查询结果
- CString,string,char*的综合比较及相互转换
- 毕业两年~
- 博士生女友自白:在读博士生是弱势群体
- javascript:利用Object.create()方法创建对象
- 进程间通信——System V信号量
- android从服务器下载文件(php+apache+win7+MySql)