求矩阵中2×2子矩阵和的最大值

来源:互联网 发布:出租房网络掉线 编辑:程序博客网 时间:2024/05/18 00:24
#include <stdio.h>#include <stdlib.h>#include <string>#include <vector>#include <set>#include <iostream>#include <map>#include <string.h>using namespace std;void help(char* buf, vector<vector<int> >& vvec){    vector<int> vec;    for(int i = 0; i < strlen(buf); ++i)    {        if(buf[i] != ';')        {            if(buf[i] != ' ' )            {                string s = "";                s += buf[i];                int tmp = atoi(s.c_str());                vec.push_back(tmp);                //printf("%d\n", tmp);            }            if(i == strlen(buf) - 1)            {                vvec.push_back(vec);                vec.clear();            }        }else        {            vvec.push_back(vec);            vec.clear();        }    }}int main(int argc,char* argv[]){    int M, N;    char buf[100];    memset(buf, 0, 100);    gets(buf);    //printf("%s\n", buf);    vector<vector<int> > vvec;    //vector<int> vec;    help(buf, vvec);    //printf("***\n");    int m = vvec.size();    int n = vvec[0].size();    //printf("%d %d\n", m, n);    int sum = 0;    int maxi = 0;    for(int i = 0; i < m - 1; ++i)    {        for(int j = 0; j < n - 1; ++j)        {            sum = vvec[i][j] + vvec[i + 1][j] + vvec[i][j + 1] +vvec[i + 1][j + 1];            if(sum >= maxi)            {                maxi = sum;            }        }    }    printf("%d\n", maxi);    return 0;}
0 0