10 / 10
Jun 2024

Hello,
I’m getting NZEC status for my solution to https://www.spoj.com/problems/WTPZ2001G/27.
May I ask for some hint or an example input that crashes my program?
Submission ID: 33115699
(One user mentioned in comments below the problem statement that the example input test case is malformed. I see that the format of the input test case has been fixed, not sure if this matters here)

  • created

    May '24
  • last reply

    Jun '24
  • 9

    replies

  • 271

    views

  • 3

    users

  • 3

    links

I think the only person who can provide a hint or correct the test data is Rafał Witkowski - the original problem setter. Looking at his submission history2, he has been active recently, so he may respond to the comment. (The site admins may also be able to access the test data, but they tend not to get involved.)

I merely tidied up the problem description input section to remove the HTML paragraph tags, since they may be interpreted as blank lines. I have no access to the actual test data sets though.

There have been no attempts since the problem was published in 2022, and now four different people attempting on the same day. Has this problem been set as some coursework by any chance?

I notice that there has been an AC submission. Is this person known to you? Perhaps you could ask if they took any special precautions to handle malformed input.

I submitted a test program to verify the input data format - it didn’t find any problems.

I checked for:

  • only digits and spaces on each line
  • the expected number of integers on each line
  • no multiple spaces between numbers
  • no leading or trailing spaces
  • no blank lines
  • number of data sets between 1 and 10
  • number of rows between 1 and 10
  • number of columns between 1 and 10
  • number of turns between 1 and 1000
  • coordinates of diamonds are within the board bounds

Thanks for reply, now I’m getting another kind of error, although I have no idea why were I getting RTE. I’ll contact Rafał Witkowski if I don’t come up wth an idea how to solve this problem.
This is not a coursework (or at least I’m not aware of it).
Publishing date of the problem is 2022, but until recently I haven’t noticed it in the problem set listing - I’ve solved a few SPOJ problems from the end of the list (https://www.spoj.com/problems/classical/sort=0,start=8000003) in the past few months and I don’t recall seeing the problem there until recently.
There are also a few other problems published by Rafał that have publishing date 2022, but I think they weren’t visible a few months ago.

The test cases are malformed in some way, although I am not quite sure how:

  • std::cin fails when reading with exceptions enabled std::cin.exceptions(std::ios_base::badbit | std::ios_base::failbit).
  • scanf("%d", ...) fails to read input as well

Perhaps the problem is handled differently by Free Pascal and C++?

Could it be a different line terminator character - carriage return or line feed or carriage return and line feed?

Could it be a missing line terminator on the final line of the test case?

Or something else along those lines that I haven’t thought of?

Could you write a similar program to mine to validate the input - read all input as strings and see what you can find. The only way I know to get info from the test data is to use asserts. Write a program that runs and exits cleanly, then add asserts until one of them fires.

And yet in Free Pascal, I read the full set.

This is the program I used to validate the input. What have I missed?

program Wtpz2001G;

// www.spoj.com/problems/WTPZ2001G
// someone believes bad input data - let's check it

{$ASSERTIONS ON}

uses
  SysUtils;

const
  MaxNumRows = 10;
  MaxNumColumns = 10;
  MaxNumTurns = 1000;

type
  StringArray = array of string;

function OnlyDigits(const s: string): boolean;
var
  i: longint;
begin
  OnlyDigits := false;
  for i := 1 to Length(s) do
    if (s[i] < '0') or (s[i] > '9') then exit;
  OnlyDigits := true;
end;

function OnlyDigitsAndSpaces(const s: string): boolean;
var
  i: longint;
begin
  OnlyDigitsAndSpaces := false;
  for i := 1 to Length(s) do begin
    if s[i] = ' ' then continue;
    if (s[i] < '0') or (s[i] > '9') then exit;
  end;
  OnlyDigitsAndSpaces := true;
end;

procedure Split(str: string; const delimiter: string; var fields: StringArray);
  procedure AddField(const f: string);
  var
    c: integer;
  begin
    c := Length(fields);
    SetLength(fields, c + 1);
    fields[c] := f;
  end;
var
  p: integer;
  f: string;
begin
  SetLength(fields, 0);
  p := Pos(delimiter, str);
  while p > 0 do begin
    f := Copy(str, 1, p - 1);
    AddField(f);
    Delete(str, 1, p + length(delimiter) - 1);
    p := Pos(delimiter, str);
  end;
  if str <> '' then
    AddField(str);
end;

var
  numTests, t, numRows, numColumns, numTurns, r, c, i: longint;
  s: string;
  fields: StringArray;
begin
  readln(s);
  Assert(not EOF);
  Assert(OnlyDigits(s));
  Split(s, ' ', fields);
  Assert(Length(fields) = 1);
  numTests := StrToInt(fields[0]);
  Assert((1 <= numTests) and (numTests <= 10));
  for t := 1 to numTests do begin
    readln(s);
    Assert(not EOF);
    Assert(Trim(s) = s);
    Assert(Trim(s) <> '');
    Assert(Pos('  ', s) = 0);
    Assert(OnlyDigitsAndSpaces(s));
    Split(s, ' ', fields);
    Assert(Length(fields) = 3);
    numRows := StrToInt(fields[0]);
    numColumns := StrToInt(fields[1]);
    numTurns := StrToInt(fields[2]);
    Assert((1 <= numRows) and (numRows <= MaxNumRows));
    Assert((1 <= numColumns) and (numColumns <= MaxNumColumns));
    Assert((1 <= numTurns) and (numTurns <= MaxNumTurns));

    for i := 1 to numTurns do begin
      readln(s);
      if (t < numTests) or (i < numTurns) then
        Assert(not EOF);
      Assert(Trim(s) = s);
      Assert(Trim(s) <> '');
      Assert(Pos('  ', s) = 0);
      Assert(OnlyDigitsAndSpaces(s));
      Split(s, ' ', fields);
      Assert(Length(fields) = 2);
      r := StrToInt(fields[0]);
      c := StrToInt(fields[1]);
      Assert((1 <= r) and (r <= numRows));
      Assert((1 <= c) and (c <= numColumns));
    end;
  end;
  writeln('Done');
end.

Do I understand correctly, that this is literally the program you submitted and from a wrong answer you conclude that the input is correct? This makes sense, but only if there is a single test input. I don’t think this is the true in this problem.

When I add an actual solution to your validation code it does indeed fail with NZEC.

No, the fact that it didn’t NZEC from an assertion failing shows that those tests all passed.