码迷,mamicode.com
首页 > 其他好文 > 详细

统计一个方阵中在四个方向长度为D的连续子序列的和

时间:2018-04-04 10:31:05      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:方向   names   can   ons   for   return   +++   turn   std   

题目大意: 统计一个方阵中在四个方向长度为D的连续子序列的和

解题思路: 模拟

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 const int imax_n = 505;
  5 int a[imax_n][imax_n];
  6 int n, D;
  7 
  8 void solve()
  9 {
 10     int ans = 0;
 11     //hang
 12     for (int i = 0; i < n; ++i)
 13     {
 14         int tmp = 0;
 15         int j = 0;
 16         while (j < D)
 17         {
 18             tmp += a[i][j];
 19             ++j;
 20         }
 21         ans = max(ans, tmp);
 22         while (j < n)
 23         {
 24             tmp = tmp - a[i][j - D] + a[i][j];
 25             ++j;
 26             ans = max(ans, tmp);
 27         }
 28     }
 29     // printf("hang ans = %d\n", ans);
 30 
 31     for (int i = 0; i < n; ++i)
 32     {
 33         int tmp = 0;
 34         int j = 0;
 35         while (j < D)
 36         {
 37             tmp += a[j][i];
 38             ++j;
 39         }
 40         ans = max(ans, tmp);
 41         while (j < n)
 42         {
 43             tmp = tmp - a[j - D][i] + a[j][i];
 44             ++j;
 45             ans = max(ans, tmp);
 46         }
 47     }
 48     // printf("lei ans = %d\n", ans);
 49     // (i, 0) ++ ++
 50     for (int i = 0; i < n; ++i)
 51     {
 52         int j = 0;
 53         int tmp = 0;
 54         int k = i;
 55         while (k < n && j < D && j < n)
 56         {
 57             tmp += a[k][j];
 58             ++j;
 59             ++k;
 60         }
 61         if (j < D)
 62             continue;
 63         ans = max(ans, tmp);
 64         while (k < n && j < n)
 65         {
 66             tmp = tmp - a[k-D][j-D] + a[k][j];
 67             ans = max(ans, tmp);
 68             ++j;
 69             ++k;
 70         }
 71     }
 72     // printf("++++ans = %d\n", ans);
 73     //(0, i) ++ ++
 74     for (int i = 1; i < n; ++i)
 75     {
 76         int j = i;
 77         int k = 0;
 78         int tmp = 0;
 79         while (k < n && j < n && k < D)
 80         {
 81             tmp += a[k][j];
 82             ++k;
 83             ++j;
 84         }
 85         if (k < D)
 86             continue;
 87         ans = max(ans, tmp);
 88         while (k < n && j < n)
 89         {
 90             tmp = tmp - a[k - D ][j - D ] + a[k][j];
 91             ++j;
 92             ++k;
 93             ans = max(ans, tmp);
 94         }
 95     }
 96     // printf("++++ans = %d\n", ans);
 97 
 98     for (int i = 0; i < n; ++i)
 99     {
100         int j = 0;
101         int k = i;
102         int tmp = 0;
103         while (k >= 0 && j < n && j < D)
104         {
105             tmp += a[k][j];
106             ++j;
107             --k;
108         }
109         if (j < D)
110             continue;
111         ans = max(ans, tmp);
112         while (k >= 0 && j < n)
113         {
114             tmp = tmp - a[k + D][j - D ] + a[k][j];
115             ans = max(ans, tmp);
116             --k;
117             ++j;
118         }
119     }
120     // printf("--++ans = %d\n", ans);
121 
122     for (int i = 0; i < n; ++i)
123     {
124         int j = i;
125         int k = n-1;
126         int tmp = 0;
127         int step  = 0;
128         while (k >= 0 && j < n && step < D)
129         {
130             tmp += a[k][j];
131             ++j;
132             --k;
133             ++step;
134         }
135         if (step < D)
136             continue;
137         ans = max(ans, tmp);
138         while (k >= 0 && j < n)
139         {
140             tmp = tmp - a[k + D ][j - D ] + a[k][j];
141             ans = max(ans, tmp);
142             --k;
143             ++j;
144         }
145     }
146     // printf("--++ans = %d\n", ans);
147     printf("%d\n", ans);
148 }
149 
150 int main()
151 {
152 #ifndef ONLINE_JUDGE
153     freopen("test.txt", "r", stdin);
154 #endif // ONLINE_JUDGE
155     scanf("%d%d", &n, &D);
156     for (int i = 0;i < n; ++i)
157     {
158         for (int j = 0; j < n; ++j)
159         {
160             scanf("%d", &a[i][j]);
161         }
162     }
163     solve();
164     return 0;
165 }

 

统计一个方阵中在四个方向长度为D的连续子序列的和

标签:方向   names   can   ons   for   return   +++   turn   std   

原文地址:https://www.cnblogs.com/djingjing/p/8715162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!