2013-07-18 73 views
0

我只是從編程挑戰書中找不到這個圖形編輯器程序的錯誤。這裏是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 
+0

「所有操作都能正常工作,測試輸出正常工作。」那麼問題是什麼? –

+0

@DarkFalcon:UVa法官說「錯誤的答案」 –

+0

我的建議是拿出新的測試數據。這類問題因隱藏的「陷阱」和邊緣案例而臭名昭着。你介意展示一些你的測試用例嗎? –

回答

2

說明您的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 
+0

判決:接受!在這裏和在UVA!謝謝! –