-1

Codeforces Round 684(Div.2) Question C1:

https://codeforces.com/contest/1440/problem/C1

What is wrong in my solution and why is the above mentioned error occuring in the below commented line for each of solution. I am doing the in the specified number of operations and my output is also coming correct but each time i go to submit my solution it throws the error mentioned in the title . What is wrong in that line or in the entire for the given question?

My solution:

#include<iostream>
#include<bits/stdc++.h> 
#define ll long long int
 
using namespace std;
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int c=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>arr[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                c+=arr[i][j];   **//error occuring in this line**
            }
        }
        cout<<(c*3)<<endl;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(arr[i][j]==1&&(i!=n-1&&j!=m-1)){
                    cout<<(i+1)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+2)<<" "<<(i+2)<<" "<<(j+2)<<endl;
                    cout<<(i+1)<<" "<<(j+1)<<" "<<(i+2)<<" "<<(j+1)<<" "<<(i+2)<<" "<<(j+2)<<endl;
                    cout<<(i+2)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+2)<<endl;
                }
                else if(arr[i][j]==1&&(i==n-1||j==m-1)){
                    if(j==m-1&&i<n-1){
                       cout<<(i+2)<<" "<<(j)<<" "<<(i+1)<<" "<<(j)<<" "<<(i+1)<<" "<<(j+1)<<endl;
                       cout<<(i+2)<<" "<<(j)<<" "<<(i+2)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+1)<<endl;
                       cout<<(i+1)<<" "<<(j)<<" "<<(i+1)<<" "<<(j+1)<<" "<<(i+2)<<" "<<(j+1)<<endl;
                }
                    if(i==n-1&&j<m-1){
                        cout<<(i+1)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+2)<<" "<<(i)<<" "<<(j+2)<<endl;
                        cout<<(i+1)<<" "<<(j+1)<<" "<<(i)<<" "<<(j+1)<<" "<<(i)<<" "<<(j+2)<<endl;
                        cout<<(i)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+2)<<endl;
                    }
                    if(i==n-1&&j==m-1){
                        cout<<(i)<<" "<<(j)<<" "<<(i+1)<<" "<<(j)<<" "<<(i+1)<<" "<<(j+1)<<endl;
                        cout<<(i)<<" "<<(j)<<" "<<(i)<<" "<<(j+1)<<" "<<(i+1)<<" "<<(j+1)<<endl;
                        cout<<(i+1)<<" "<<(j)<<" "<<(i+1)<<" "<<(j+1)<<" "<<(i)<<" "<<(j+1)<<endl;
                    }
                }
            }
        }
    }
    return 0;
}
cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

0

Your reading of the input values is wrong. The problem description states, that

Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.

But you are reading the lines as follows:

for (int i = 0; i < n; i++) {
  for (int j = 0; j < m; j++) {
    cin >> arr[i][j];
  }
}

cin tokenizes the input at whitespaces (space, tab, linefeed, newline ...). Thus an input of 10001 is not read into 5 elements of your matrix as single bit, but into the first cell of the matrix as decimal 10001.

You must only read n lines and split each line into m bits, but you are reading n*m lines and don't split the lines into single bits. So at latest when you are summing up all cells of the matrix, you get out of range of an integer, because

  1. your cell values are way too big and
  2. you read way to many values, ie values from the next test cases

You could for instance try reading each line in as a string, and then just iterate over that string and set the cells of the matrix accordingly.

for (int i = 0; i < n; i++) {
  string line; cin >> line;
  for (int j = 0; j < m; j++)
    arr[i][j] = (line[j] - '0');
}
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • `"..but you are reading n*m lines and.."` -> `"but you are reading n lines and m whitespace delimited words in each line.."` Also, I suspect the lines have only a single string of `'0'` and `'1'`s so looping `j` times with `cin.get()` may be fine, or looping `i` times and reading with `getline` stopping after `m` characters. – David C. Rankin Nov 20 '20 at 14:54
  • @DavidC.Rankin in my understanding, `cin` reads until the next whitespace. So executing `cin` for `n*m` times reads `n*m` tokens. Whether each of them was given in a separate line or all of them are in the same line doesn't matter. And as the problem definition states "*Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.*". So not whitespaces in the lines. Thus my approach to read `n` whole lines with `cin` and iterate over the characters of each line with a loop to get the single bits. – derpirscher Nov 20 '20 at 17:14