50170. Draw a Rectangle

I'm a slow walker, but I never walk backwards.

Task Description

Given a two dimensional matrix, we want to fill all elements in a rectangle in the matrix with a given value. The first subtask is that you will be given two pointers to the upper left and the lower right corners of the rectangle. You will also have the numbers of rows and columns of the 2D matrix, and the value to fill the rectangle with. The second task is similar to the first one, except that now the pointers point to the upper right and lower left corner. The prototypes of these two functions are as follows. We assume that the layout of a 2D matrix uses contiguous memory in row-major order, as described in the textbook.

void fill_rectangle_neg(int *upper_left, int *bottom_right, int n_row, int n_col, int fill);
void fill_rectangle_pos(int *upper_right, int *bottom_left, int n_row, int n_col, int fill);

You can use the following main.c to test your function. Note that in each subtask we will only test one function. In main.c, there is a variable type. If it is 0 then the main program will test fill_rectangle_neg. If it is 1 then we will test fill_rectangle_pos. Click here to download main.c and fill_rectangle.h.

#include <stdio.h>
#include <string.h>
#include "fill_rectangle.h"
int main(){
        int row, col, type, row1, col1, row2, col2, fill;
        scanf("%d%d%d", &row, &col, &type);
        int arr[row][col];
        memset(&(arr[0][0]), 0, sizeof(int) * row * col);
        while(scanf("%d%d%d%d%d", &row1, &col1, &row2, &col2, &fill) != EOF){
                if(type == 0)
                        fill_rectangle_neg(&(arr[row1][col1]), &(arr[row2][col2]), row, col, fill);
                else
                        fill_rectangle_pos(&(arr[row1][col1]), &(arr[row2][col2]), row, col, fill);
        }
        for(int i = 0; i < row; i++)
                for(int j = 0; j < col; j++)
                        printf("%d%c", arr[i][j], j == col - 1 ? '\n' : ' ');
        return 0;
}

Input Format (for main.c)

The first line contains three integers: the numbers of rows and columns of this 2D array, and type number for the function we will test. Next, there are several lines until EOF. Each line contains five integers row1, col1, row2, col2, and fill, which represent the row and column of the first element, the row and column of the second element, and the value we fill rectangle with, respectively. If type is 0 (upper left and lower right), then row1 $\leq$ row2 and col1 $\leq$ col2, otherwise row1 $\leq$ row2 and col1 $\geq$ col2.

Output Format (for main.c)

Output the final 2D array.

Subtasks

  • 50 points: type is 0 so we will only test your fill_rectangle_neg.
  • 50 points: type is 1 so we will only test your fill_rectangle_pos.

Sample Input 1

5 5 0
0 1 4 2 2
2 0 3 3 7

Sample Output 1

0 2 2 0 0
0 2 2 0 0
7 7 7 7 0
7 7 7 7 0
0 2 2 0 0

Sample Input 2

5 5 1
0 2 4 1 2
2 3 3 0 7

Sample Output 2

0 2 2 0 0
0 2 2 0 0
7 7 7 7 0
7 7 7 7 0
0 2 2 0 0

Strong Sample

Here are strong test data for you. strong_0 is for fill_rectangle_neg, and strong_1 is for fill_rectangle_pos.

Compile

gcc -std=c99 -O2 -c fill_rectangle.c
gcc -std=c99 -O2 -c main.c
gcc -std=c99 -O2 main.o fill_rectangle.o

Discussion