Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

4T-Shirt's blog

By 4T-Shirt, 13 years ago, In English
Hi! This is a problem on SPOJ: http://www.spoj.pl/problems/ACS/
Could someone please tell me why my program is always "runtime error ".
Here is my code:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
const int rowMax = 1234;
const int colMax = 5678;
int mat[rowMax+1][colMax+1];
char ch[100];
char c;
int a,b;
int main()
{
    int cnt = 0;
    for (int i=1;i<=colMax;i++)    mat[0][i] = i;
    for (int i=1;i<=rowMax;i++) mat[i][0] = i;
    for (int i=1;i<=rowMax;i++)
        for (int j=1;j<=colMax;j++)
        {
            mat[i][j] = cnt + 1;
            cnt ++;
        }
        while (gets(ch))
        {
            if (strlen(ch)==0)
            {
            }
            else if (ch[0]=='R')
            {
                sscanf(ch,"%c %d %d",&c,&a,&b);
                mat[a][0] = b;mat[b][0] = a;
            }
            else if (ch[0] == 'C')
            {
                sscanf(ch,"%c %d %d",&c,&a,&b);
                mat[0][a] = b;mat[0][b] = a;
            }
            else if (ch[0] == 'Q')
            {
                sscanf(ch,"%c %d %d",&c,&a,&b);
                int row = 1,col=1;
                while (mat[row][0] != a )    row++;
                while (mat[0][col] != b )    col++;
                printf("%d\n",mat[row][col]);
            }
            else if (ch[0] == 'W')
            {
                sscanf(ch,"%c %d",&c,&a);
                int row = (a-1)/colMax + 1;
                int col = a - (row-1)*colMax;
                printf("%d %d\n",mat[row][0],mat[0][col]);
            }
        }
        return 0;
}


Tags re, spoj
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Could you yourself debug your program? Most common mistake of all people who write here "I have C++ program which works wrong or gives runtime error" is "array index out of bounds".

If you want to master programming - you could not avoid learning how to search for your own mistakes in your own works.
  • 13 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Submit this and meditate a little.

    (UPD: I mean that with this check you will be getting non-zero return code instead of run-time error, which shows that check is working and your program's logic is not correct. You can also simply comment out all this branch which works on 'Q' and see that you are getting WA - which also shows that you have mistake in this block, but not as precisely.)

    By the way I think it is very wrong idea each time to search throw an array to find required number. You should just keep array of current numbers of lines which were swapped.
                else if (ch[0] == 'Q')
                {
                    sscanf(ch,"%c %d %d",&c,&a,&b);
                    int row = 1,col=1;
                    while (mat[row][0] != a ) {
                        if (row > rowMax) {
                            printf("I am an idiotic bug\n");
                            return 1;
                        } // if
                        row++;
                    } // while
                    while (mat[0][col] != b ) {
                        if (col > colMax) {
                            printf("I am an wonderful feature\n");
                            return 2;
                        } // if
                        col++;
                    } // while
                    printf("%d\n",mat[row][col]);
                }