spoj程序中的SIGSEGV错误

时间:2015-08-14 15:01:58

标签: c++ sigsegv

This是一个SPOJ问题,我似乎已经解决了但是当我运行它时出现错误SIGSEGV。即使我读了这个错误的内容(有点指的是数组中不存在的元素,如果我做对了),我也很难在code找到错误。

#include<iostream>
#include<climits>
using namespace std;
int results[214];
void Solution(int number)   // The real part of the program
{
    int r,c;   // rows and columns
    char ch;
    int var;   // current input value of cell
    int a[101][101]={0};   // a[i][j] is the sum of cells from the top left element 1,1 to the one with indexes i,j
    int max=INT_MIN;   // here the max numbers of bananas is kept
    cin>>r>>c;
    for(int i=1;i<=r;i++)
        for(int k=1;k<=c;k++)
            {
                cin>>ch;
                switch (ch)   // Transforms chars to numbers
                {
                    case 'B': var=1; break;
                    case 'T': var=-1; break;
                    default: var=0;
                }
                a[i][k]=a[i][k-1]+a[i-1][k]-a[i-1][k-1]+var;
            }
    int maxSide=r; 
    if(c<r) maxSide=c;   // just checking the smaller side
    for(int i=1;i<=maxSide;i++)   // The various lengths of the square area that is long i cells and
        for(int p=1;p<=r-i+1;p++)   // the top left corner has indexes p and q
            for(int q=1;q<=c-i+1;q++)
            {
                int x;   // current number of bananas
                x=a[p+r-1][q+r-1]-a[p-1][q+r-1]-a[p+r-1][q-1]+a[p-1][q-1];
                if(x>max) max=x;   // comparing if this is the maximum amount of bananas so far
            }
    results[number]=max;   // puts the max number of bananas for each case
}
int main()
{
    int T;   // number of cases
    cin>>T; 
    for(int i=1;i<=T;i++)
        Solution(i); 
    for(int i=1;i<=T;i++)
        cout<<"Case "<<i<<": "<<results[i]<<endl;   //outputs all the result of each case
    return 0;
}

0 个答案:

没有答案