wap 笔试题2015

来源:互联网 发布:直播软件 编辑:程序博客网 时间:2024/05/17 17:18
#include <iostream>#include <memory.h>#include <algorithm>//#pragma warning(disable:4996) vs2013using namespace std;const int MAX_LINE = 500;int a[MAX_LINE][MAX_LINE];int dp[MAX_LINE][MAX_LINE];//int a[4][4] =//{//-1, 4, 5, 1,//2, -1, 2, 4,//3, 3, -1, 3,//4, 2, 1, 2//};//int a[4][4] =//{//-1, 4, 5, 1,//2, -1, 2, 4,//3, 3, -1, -1,//4, 2, 1, 2//};/*test fun*/void print(int m,int n){for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){cout << dp[i][j] << " ";}cout << endl;}cout << endl;}//Init first Col of dpvoid InitDPOfFirstCol(int m){int colSum = 0;for (int i = 0; i < m; i++){if (a[i][0] == -1){dp[i][0] = -1;colSum = 0;}else{colSum += a[i][0];dp[i][0] = colSum;}}int colSumReverse = 0;for (int i = m - 1; i >= 0; i--){if (a[i][0] == -1){dp[i][0] = max(dp[i][0], -1);colSumReverse = 0;}else{colSumReverse += a[i][0];dp[i][0] = max(colSumReverse, dp[i][0]);}}}bool ShouldStop(int m,int colIndex){bool stop = true;for (int i = 0; i < m; i++){if (dp[i][colIndex] != -1){stop = false;break;}}return stop;}/***GetHigestScore of the game *@param m rows *@param n cols*@return HighestScore*/int GetHighestScore(int m,int n){memset(dp, -1, sizeof(dp));InitDPOfFirstCol(m);//print(m,n);//dp process form col 2 to nfor (int k = 1; k < n; k++){bool hasGoDown = false;bool hasGoUp = false;int downSum = 0;int upSum = 0;for (int j = 0; j < m; j++){if (dp[j][k - 1] == -1) {continue;}int downTemp = dp[j][k - 1];int t1 = j;for (; t1 < m; t1++){  //go downif (a[t1][k] == -1){break;}else{downTemp += a[t1][k];if (downTemp <= dp[t1][k]){break;}else{dp[t1][k] = downTemp;}}}if (t1 == m && !hasGoDown){  //go down int the bottomfor (int j = 0; j < m; j++){if (a[j][k] == -1)break;if (a[j][k] != -1){downSum += a[j][k];if (downSum <= dp[j][k]){break;}else{dp[j][k] = downSum;}}}hasGoDown = true;}int upTemp = dp[j][k - 1];int t2 = j;for (; t2 >= 0; t2--){  //go up if (a[t2][k] == -1){break;}else{upTemp += a[t2][k];if (upTemp <= dp[t2][k]){//break;}else{dp[t2][k] = upTemp;}}}if (t2 == 0 && !hasGoUp){  //go up in the topfor (int j = m; j >= 0; j--){if (a[j][k] == -1)break;if (a[j][k] != -1){upSum += a[j][k];if (upSum <= dp[j][k]){//break;}else{dp[j][k] = upSum;}}}hasGoUp = true;}}if (ShouldStop(m,k)){return -1;}//print(m,n);}//get the result from last colint res = -1;for (int i = 0; i < m; i++){res = max(dp[i][n - 1],res);}return res;}int main(){int m, n;cin >> m >> n;for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){cin >> a[i][j];}}int res = GetHighestScore(n,m);  cout << res << endl;return 0;}

0 0
原创粉丝点击