我只是在编程挑战书中找不到这个图形编辑器程序的错误这是problem的链接。
所有操作正常,测试输出正常。
这是我的代码:

#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;
    }
  }
}

测试用例工作:
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

最佳答案

你的K操作不正确。

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

应该填充矩形前任:
RRRRO
RRRRO
RRRRO
RRRRO
OOOOO

07-27 14:02