2012年12月2日

UVa 10267 - Graphical Editor


#include <stdio.h>

char map[255][255];
int R, C, visit[255][255];

void fill(int r, int c, char color) {
  if (r > R || r <= 0 || c > C || c <= 0 || visit[r][c]) return;
  visit[r][c] = 1;
  char now = map[r][c];
  if (r != R && map[r + 1][c] == now) fill(r + 1, c, color);
  if (c != C && map[r][c + 1] == now) fill(r, c + 1, color);
  if (r > 1 && map[r - 1][c] == now) fill(r - 1, c, color);
  if (c > 1 && map[r][c - 1] == now) fill(r, c - 1, color);
  map[r][c] = color;
}

int max(int a, int b) {
  return a > b ? a : b;
}

int main() {
  char s[100], color[20];
  int r, c, r1, c1, r2, c2;
  while (gets(s)) {
    if (s[0] == 'X') return 0;
    if (s[0] == 'I') {
      sscanf(s, "%*s%d%d", &C, &R);
      for (r = 1; r <= R; r++) {
        for (c = 1; c <= C; c++)
          map[r][c] = 'O';
        map[r][c] = 0;
      }
    } else if (s[0] == 'C') {
      for (r = 1; r <= R; r++)
        for (c = 1; c <= C; c++)
          map[r][c] = 'O';
    } else if (s[0] == 'L') {
      sscanf(s, "%*s%d%d%s", &c, &r, color);
      map[r][c] = color[0];
    } else if (s[0] == 'V') {
      sscanf(s, "%*s%d%d%d%s", &c, &r1, &r2, color);
      for (r = r1 + r2 - max(r1, r2); r <= max(r1, r2); r++)
        map[r][c] = color[0];
    } else if (s[0] == 'H') {
      sscanf(s, "%*s%d%d%d%s", &c1, &c2, &r, color);
      for (c = c1 + c2 - max(c1, c2); c <= max(c1, c2); c++)
        map[r][c] = color[0];
    } else if (s[0] == 'K') {
      sscanf(s, "%*s%d%d%d%d%s", &c1, &r1, &c2, &r2, color);
      for (r = r1 + r2 - max(r1, r2); r <= max(r1, r2); r++)
        for (c = c1 + c2 - max(c1, c2); c <= max(c1, c2); c++)
          map[r][c] = color[0];
    } else if (s[0] == 'F') {
      sscanf(s, "%*s%d%d%s", &c, &r, color);
      for (r1 = 1; r1 <= R; r1++)
        for (c1 = 1; c1 <= C; c1++)
          visit[r1][c1] = 0;
      fill(r, c, color[0]);
    } else if (s[0] == 'S') {
      puts(s + 2);
      for (r = 1; r <= R; r++)
        puts(map[r] + 1);
    }
  }
}

沒有留言:

張貼留言