0

I just can't find the error with this Graphical Editor program, from the Programming-Challenges book. Here is the link to the problem.

All operations work and the test output works fine.

Here is my code:

#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <vector>

using namespace std;

int m;
int n;
string s;

vector< vector<char> > picture;

void reset() {
  for (int i = 0; i < picture.size(); ++i) {
    for (int j = 0; j < picture[i].size(); ++j) {
      picture[i][j] = 'O';
    }
  } 
}

struct point {
  int x;
  int y;
};

bool validate_x(int x) {
  return x >= 1 && x <= m;
}

bool validate_y(int y) {
  return y >= 1 && y <= n;
}

int main() {
  while (true) {
    cin >> s;
    if (s == "I") {
      cin >> m >> n;
      picture.resize(m);

      for (int i = 0; i < picture.size(); ++i) {
        picture[i].resize(n);
      }

      reset();
    } else if (s == "C") {
      picture.resize(m);

      for (int i = 0; i < picture.size(); ++i) {
        picture[i].resize(n);
      }

      reset();
    } else if (s == "L") {
      int x, y;
      cin >> x >> y;

      if (!validate_x(x) || !validate_y(y)) {
        continue;
      }

      char color;
      cin >> color;
      picture[x-1][y-1] = color;
    } else if (s == "V") {
      int x, y1, y2;
      char color;
      cin >> x >> y1 >> y2 >> color;

      if (!validate_x(x) || !validate_y(y1) || !validate_y(y2)) {
        continue;
      }

      for (int i = min(y1, y2); i < max(y1, y2) + 1; ++i) {
        picture[x-1][i-1] = color;
      }
    } else if (s == "H") {
      int x1, x2, y;
      char color;
      cin >> x1 >> x2 >> y >> color;

      if (!validate_y(y) || !validate_x(x1) || !validate_x(x2)) {
        continue;
      }

      for (int i = min(x1, x2); i < max(x1, x2) + 1; ++i) {
        picture[i-1][y-1] = color;
      }
    } else if (s == "K") {
      int x1, y1, x2, y2;
      char color;
      cin >> x1 >> y1 >> x2 >> y2 >> color;

      if (!validate_x(x1) || !validate_x(x2) || !validate_y(y1) || !validate_y(y2)) {
        continue;
      }

      for (int i = min(x1, x2); i < max(x1, x2) + 1; ++i) {
        for (int j = min(y1, y2); j < max(y1, y2) + 1; ++j) {
          if (i == x1 || i == x2 || j == y1 || j == y2) {
            picture[i-1][j-1] = color;
          }
        }
      }
    } else if (s == "F") {
      int x, y;
      char color;
      cin >> x >> y >> color;

      if (!validate_x(x) || !validate_y(y)) {
        continue;
      }

      char old_color = picture[x-1][y-1];

      if (old_color != color) {
        stack<point> stack;
        point p;
        p.x = x - 1;
        p.y = y - 1;

        stack.push(p);

        while (!stack.empty()) {
          point curr = stack.top();
          stack.pop();

          if (picture[curr.x][curr.y] == old_color) {
            picture[curr.x][curr.y] = color;
            if (curr.x > 0) {
              point left;
              left.x = curr.x - 1;
              left.y = curr.y;

              stack.push(left);
            }

            if (curr.x < m - 1) {
              point right;
              right.x = curr.x + 1;
              right.y = curr.y;

              stack.push(right);
            }

            if (curr.y > 0) {
              point over;
              over.x = curr.x;
              over.y = curr.y - 1;

              stack.push(over);
            }

            if (curr.y < n - 1) {
              point under;
              under.x = curr.x;
              under.y = curr.y + 1;

              stack.push(under);
            }
          }          
        }
      }
    } else if (s == "S") {
      string name;
      getline(cin, name);
      cout << name.erase(0, 1) << endl;
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
          cout << picture[j][i];
        }

        cout << endl;
      }
    } else if (s == "X") {
      return 0;
    }
  }
}

Test cases working:

I 5 6
2 3 A
one.bmp
2 3 J
3 3 J
2 3 4 W
3 4 2 Z
two.bmp

I 250 250
F 3 3 C
S superman
C
F 3 3 C
S superman
X
Johan S
  • 3,531
  • 6
  • 35
  • 63
  • "All operations work and the test output works fine." Then what is the problem? – Dark Falcon Jul 18 '13 at 18:08
  • @DarkFalcon: The UVa judge says "wrong answer" – Johan S Jul 18 '13 at 18:09
  • My advice is come up with new test data. These kinds of problems are notorious for hidden "gotchas" and edge-cases. Do you mind showing some of your test cases? – Pat Lillis Jul 18 '13 at 18:16
  • @PatLillis haven't written them down exactly but I can make up some for you, I'll update the answer 1 second. – Johan S Jul 18 '13 at 18:18
  • The global variables are making me nervous – doctorlove Jul 18 '13 at 18:48
  • @doctorlove Why are the globals making you nervous? – Johan S Jul 18 '13 at 18:55
  • "Globals are evil" is a common opinion among programmers. They occasionally cause code to operate in a surprising way. See discussion [here](http://c2.com/cgi/wiki?GlobalVariablesAreBad). – Kevin Jul 18 '13 at 18:59
  • @Kevin I know that globals are evil, but on the other hand this code is simpler with them, if you inspect the top performer's solutions on TopCoder or CodeForces you will see they use globals as well, because its programming-challenges, not e.g. An Android app meant for production usage. – Johan S Jul 18 '13 at 19:02
  • 2
    You can declare `s` in your main method without changing anything else. If you want to declare `m` and `n` and `picture` in your main, then you won't be able to use them in `reset`, `validate_x`, and `validate_y`. edit: In regards to "It's just a programming challenge", some would say that you should follow good coding practices regardless of how "serious" the project is. – Kevin Jul 18 '13 at 19:10

1 Answers1

2

Your K operation is incorrect.

I 5 5
K 1 1 4 4 R
S output.txt
output.txt
RRRRO
ROORO
ROORO
RRRRO
OOOOO
X

The rectangle should be filled. Ex:

RRRRO
RRRRO
RRRRO
RRRRO
OOOOO
Kevin
  • 74,910
  • 12
  • 133
  • 166