Hi there.
I have passed all the test cases in the forum and still got wrong answers.
I don't know where to go from here....
Code: Select all
/*
new GE
*/
#include <stdio.h>
#include <stdlib.h>
void create(int mCol, int nRow, char*** matrix);
void clear(int mCol, int nRow, char** matrix);
void colorXY(int x, int y, char c, char** matrix);
void fill(int mCol, int nRow, int x, int y, char** matrix, char oldc, char newc);
void printMatrix(int mCol, int nRow, char** matrix);
void swap(int *a, int *b);
int main() {
char** matrix;
size_t size;
int mCol = 0;
int nRow = 0;
int quit = 0;
char* command = NULL;
char ch;
while (!quit && getline(&command, &size, stdin)!=EOF) {
switch(command[0]) {
case 'I' : {
sscanf(command, "%c %d %d", &ch, &mCol, &nRow);
create(mCol, nRow, &matrix);
clear(mCol, nRow, matrix);
break;
}
case 'C' : {
clear(mCol, nRow, matrix);
break;
}
case 'L' : {
int x, y;
char c;
sscanf(command, "%c %d %d %c", &ch, &x, &y, &c);
colorXY(x, y, c, matrix);
break;
}
case 'V' : {
int x, y1, y2;
char c;
sscanf(command, "%c %d %d %d %c", &ch, &x, &y1, &y2, &c);
if (y1>y2) swap(&y1, &y2);
int i;
for (i=y1;i<=y2;i++) {
colorXY(x, i, c, matrix);
}
break;
}
case 'H' : {
int x1, x2, y;
char c;
sscanf(command, "%c %d %d %d %c", &ch, &x1, &x2, &y, &c);
if (x1>x2) swap(&x1, &x2);
int i;
for (i=x1;i<=x2;i++) {
colorXY(i, y, c, matrix);
}
break;
}
case 'K' : {
int x1, x2, y1, y2;
char c;
sscanf(command, "%c %d %d %d %d %c", &ch, &x1, &y1, &x2, &y2, &c);
if (x1>x2) swap(&x1, &x2);
if (y1>y2) swap(&y1, &y2);
int i, j;
for (i=x1;i<=x2;i++) {
for (j=y1;j<=y2;j++) {
colorXY(i, j, c, matrix);
}
}
break;
}
case 'F' : {
int x, y;
char c;
sscanf(command, "%c %d %d %c", &ch, &x, &y, &c);
fill(mCol, nRow, x, y, matrix, matrix[x-1][y-1], c);
break;
}
case 'S' : {
char name[20];
sscanf(command, "%c %s", &ch, name);
printf("%s\n", name);
printMatrix(mCol, nRow, matrix);
break;
}
}
}
return 0;
}
void create(int mCol, int nRow, char*** matrix) {
*matrix = (char **)malloc(mCol * sizeof(char *));
int i;
for (i=0;i<mCol;i++) {
(*matrix)[i] = (char *)malloc(nRow * sizeof(char));
}
}
void clear(int mCol, int nRow, char** matrix) {
int i,j;
for (i=0;i<mCol;i++) {
for (j=0;j<nRow;j++) {
matrix[i][j] = 'O';
}
}
}
void colorXY(int x, int y, char c, char** matrix) {
matrix[x-1][y-1] = c;
}
void fill(int mCol, int nRow, int x, int y, char** matrix, char oldc, char newc) {
if (oldc==newc) return;
if (x<1 || x>mCol || y<1 || y>nRow) return;
colorXY(x, y, newc, matrix);
/* share up side */
if (y>1 && matrix[x-1][y-2]==oldc) {
//colorXY(x, y, newc, matrix);
fill(mCol, nRow, x, y-1, matrix, oldc, newc);
}
/* share bottom side */
if (y<nRow && matrix[x-1][y]==oldc) {
//colorXY(x, y, newc, matrix);
fill(mCol, nRow, x, y+1, matrix, oldc, newc);
}
/* share left side */
if (x>1 && matrix[x-2][y-1]==oldc) {
//colorXY(x, y, newc, matrix);
fill(mCol, nRow, x-1, y, matrix, oldc, newc);
}
/* share right side */
if (x<mCol && matrix[x][y-1]==oldc) {
//colorXY(x, y, newc, matrix);
fill(mCol, nRow, x+1, y, matrix, oldc, newc);
}
}
void printMatrix(int mCol, int nRow, char** matrix) {
int i,j;
for (i=0;i<nRow;i++) {
for (j=0;j<mCol;j++) {
printf("%c", matrix[j][i]);
}
printf("\n");
}
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}