sicily 1563 GECKO

Description

During the rainy season, one of the walls in the house is infested with mosquitoes. The wall is covered by h × w square tiles, where there are h rows of tiles from top to bottom, and w columns of tiles from left to right. Each tile has 1 to 1000 mosquitoes resting on it.

A gecko wants to eat as many mosquitoes as possible, subject to the following restrictions. It starts by choosing any tile in the top row, and eats the mosquitoes in that tile. Then, it moves to a tile in the next lower row, eats the mosquitoes on the tile, and so on until it reaches the floor. When it moves from one tile to a tile in the next lower row, it can only move vertically down or diagonally to the left or right .

Given the values of h and w, and the number of mosquitoes resting on each tile, write a program to compute the maximum possible number of mosquitoes the gecko can eat in one single trip from the top to the bottom of the wall.

Input

The first line has two integers. The first integer h (1 <= h <= 500) is the number of rows of tiles on the wall. The second integer w (1 <= w <= 500)is the number of columns of tiles on the wall.  Next, there are h lines of inputs. The ith line specifies the number of mosquitoes in each tile of the top ith row. Each line has w integers, where each integer m (1 <= m <= 1000) is the number mosquitoes on that tile. The integers are  separated by a space character.

Output

The output file contains a single integer, which is the maximum possible number of mosquitoes the gecko can eat in one single trip from the top to the bottom of the wall.

Sample Input

6 5
3 1 7 4 2
2 1 3 1 1
1 2 2 1 8
2 2 1 5 3
2 1 4 4 4
5 7 2 5 1

Sample Output

 

32

分析:

本体是典型的DP类型题,每一层是一段状态,选优递推即可,相对简单而且直白。注意最后要求的是一层中的最优。当然也可以从后向前推,更省空间,这里不再优化。

代码:

 1 // Problem#: 1563
 2 // Submission#: 1907365
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 using namespace std;
 8 
 9 #define MAX 500
10 
11 int wall[MAX][MAX];
12 int move[3][2] = {{-1, -1}, {-1, 0}, {-1, 1}};
13 int dp[MAX][MAX] = {0};
14 int h, w;
15 
16 inline bool judge(int x, int y) {
17     return x >= 0 && x < h && y >= 0 && y < w;
18 }
19 
20 inline int max(int x, int y) {
21     int a, b, m;
22     m = 0;
23     for (int i = 0; i < 3; ++i) {
24         a = x + move[i][0];
25         b = y + move[i][1];
26         if (judge(a, b) && dp[a][b] > m) m = dp[a][b];
27     }
28     return m;
29 }
30 
31 int main() {
32     cin >> h >> w;
33     int re = 0;
34     for (int i = 0; i < h; ++i)
35         for (int j = 0; j < w; ++j)
36             cin >> wall[i][j];
37     for (int i = 0; i < w; ++i)
38         dp[0][i] = wall[0][i];
39     for (int i = 1; i < h; ++i) {
40         for (int j = 0; j < w; ++j) {
41             dp[i][j] = max(i, j) + wall[i][j];
42         }
43     }
44     for (int i = 0; i < w; ++i)
45         if (dp[h - 1][i] > re) re = dp[h - 1][i];
46     cout << re << endl;
47     return 0;
48 }

 

posted @ 2013-01-25 16:06  Cielsk  阅读(199)  评论(0编辑  收藏  举报