Simple simulation + DFS questions. Note that the pixel at Initialization is an uppercase letter 'O' rather than a number '0 '. In addition, ensure that x1 <x2, y1 <y2.
The Code is as follows:
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int m, n, vis[252][252];char a[252][252], c, cc;void change(int &x, int &y){ if(x > y) { int t = x; x = y; y = t; }}void DFS(int x, int y){ if(x < 1 || y < 1 || x > m || y > n || vis[y][x] || a[y][x] != cc) return ; a[y][x] = c; vis[y][x] = 1; DFS(x + 1, y); DFS(x - 1, y); DFS(x, y + 1); DFS(x, y - 1);}int main(){#ifdef test freopen("sample.txt", "r", stdin);#endif int x1, x2, y1, y2; char name[50], b[3], bb[3]; while(1) { scanf("%s", b); if(b[0] == 'X') break; else if(b[0] == 'I') { scanf("%d%d", &m, &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) a[i][j] = 'O'; } else if(b[0] == 'C') for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) a[i][j] = 'O'; else if(b[0] == 'L') { scanf("%d%d%s", &x1, &y1, bb); a[y1][x1] = bb[0]; } else if(b[0] == 'V') { scanf("%d%d%d%s", &x1, &y1, &y2, bb); change(y1, y2); for(int i = y1; i <= y2; i++) a[i][x1] = bb[0]; } else if(b[0] == 'H') { scanf("%d%d%d%s", &x1, &x2, &y1, bb); change(x1, x2); for(int i = x1; i <= x2; i++) a[y1][i] = bb[0]; } else if(b[0] == 'K') { scanf("%d%d%d%d%s", &x1, &y1, &x2, &y2, bb); change(x1, x2); change(y1, y2); for(int i = y1; i <= y2; i++) for(int j = x1; j <= x2; j++) a[i][j] = bb[0]; } else if(b[0] == 'F') { scanf("%d%d%s", &x1, &y1, bb); c = bb[0]; cc = a[y1][x1]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) vis[i][j] = 0; DFS(x1, y1); } else if(b[0] == 'S') { scanf("%s", name); puts(name); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) printf("%c", a[i][j]); printf("\n"); } } } return 0;}