USACO 3.3 Home on the Range

对每个正方形右下角进行动态规划,很简单。

TASK: range
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3328 KB]
   Test 2: TEST OK [0.000 secs, 3328 KB]
   Test 3: TEST OK [0.000 secs, 3328 KB]
   Test 4: TEST OK [0.000 secs, 3328 KB]
   Test 5: TEST OK [0.000 secs, 3328 KB]
   Test 6: TEST OK [0.027 secs, 3328 KB]
   Test 7: TEST OK [0.027 secs, 3328 KB]

All tests OK.

1 /*
2 PROG: range
3 ID: jiafeim1
4 LANG: C++
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10  int rec[252][252];
11 int n;
12 char tempStr[252][252]={0};
13 int res[252]={0};
14 int main()
15 {
16 FILE *fin = fopen("range.in", "r");
17 FILE *fout = fopen("range.out", "w");
18
19 fscanf(fin,"%d",&n);
20
21 for(int i = 0 ;i<n;++i)
22 {
23 fscanf(fin,"%s",&(tempStr[i][0]));
24 }
25
26 for(int i = 0 ;i<n;++i)
27 for(int x=i;x<n;++x)
28 for(int y=i;y<n;++y)
29 {
30 if(tempStr[x][y]=='1' && rec[x][y]>=i &&(i==0 || rec[x-1][y-1]>=i))
31 {
32 if(tempStr[x-i][y]=='1' && tempStr[x][y-i]=='1')
33 ++rec[x][y];
34 }
35 }
36 for(int x=0;x<n;++x)
37 for(int y=0;y<n;++y)
38 {
39 if(rec[x][y]>=2)
40 {
41 for(int i = 2;i<=rec[x][y];++i)
42 {
43 ++res[i];
44 }
45 }
46 }
47 for(int i = 2;i<=n;++i)
48 {
49 if(res[i]!=0)
50 {
51 fprintf(fout,"%d %d\n",i,res[i]);
52 }
53 }
54 fclose(fin);
55 fclose(fout);
56 return 0;
57 }
posted @ 2011-05-28 22:39  幻魇  阅读(218)  评论(0编辑  收藏  举报