In: Computer Science
The programming language has to be C
The user is going to provide you with a map of rivers and grassland. Each cell on the map will be either grassland or river. Your job is to decorate this map with forks, 4 way forks, and river bends when a river bends. You will process the user's map and modify the river cells if they depict a river bend.
You will use enums to model the river, its bends, and forks. You will pattern match to replace parts of the river with bends and forks. Out of bounds regions will be considered grasslands for pattern matching simplicity.
You will use enums in this program.
You will print the integers with 1 space padding and rows will be terminated by new lines:
10 10 10 1 2 3 4 5 6 7 8 9 10 10 10
Input and Output
The tiles that we use in the map are:
The user will input maps usually of 0 and 1 but they can include other tiles as well. Typically it will 0 and 1.
The user will input a map of
P2 6 7 10 0 0 0 0 0 0 0 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 0 0 0 0 0 1 0
Where 0 is grassland and 1 is river
The program will look for corners and replace them with the RiverBend pieces.
P2 6 7 10 0 0 0 0 0 0 0 5 1 1 3 0 0 1 0 0 1 0 0 6 1 1 10 1 0 1 0 0 1 0 0 4 1 1 7 0 0 0 0 0 1 0
The header format is described below. $WIDTH is the number of cells wide and $HEIGHT is the number of cells tall.
P2 $WIDTH $HEIGHT 10
The individual cells are whitespace seperated for input. Ignore whitespace and newlines for the cells.
The output format is strict, each row ends in a newline and there is 1 space padding for all integers cells printed.
Please review the q1a-test?-input.txt files for more examples
Patterns
0 0 0 0 0 0 0 0 0 -> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -> 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 -> 0 0 0 1 1 1 1 1 1
These examples are in cross form, this means that the corners can be anything (river or grassland). This is only 1 cell being changed, you are expected to apply these patterns to all cells.
1 1
1 1 0 -> 1 2 0
0 0
For example:
0 1 0 0 1 0
1 1 0 -> 1 2 0
0 0 0 0 0 0
0 0
1 1 0 -> 1 3 0
1 1
For example:
1 0 1 1 0 1
1 1 0 -> 1 3 0
1 1 1 1 1 1
1 1
0 1 1 -> 0 4 1
0 0
0 0
0 1 1 -> 0 5 1
1 1
1 1
0 1 1 -> 0 6 1
1 1
1 1
1 1 0 -> 1 7 0
1 1
1 1
1 1 1 -> 1 8 1
0 0
0 0
1 1 1 -> 1 9 1
1 1
1 1
1 1 1 -> 1 10 1
1 1
More details
Maximum supported dimension in width or height is 16000
The format you input and output is called plain PGM. PGM is portable grey map format so you can use some image programs to view it.
Invalid input (including unexpected EOF) should be aborted immediately with the message:
Invalid input!
Hints
Screenshot of the code



Below is the c code for the same. The code reads input from the file input.txt and writes output to console as well as file output.txt . Enum has been used to represent grassland,river,river-bends & forks.
#include<stdio.h>
#include<stdlib.h>
#define MAX_DIMENSION 16000// defining maximum dimension
//enum for tiles
enum tiles{Grassland,River,NorthWestRiverBend,SouthWestRiverBend,
NorthEastRiverBend,SouthEastRiverBend,
NorthEastSouthFork,NorthWestSouthFork,
WestNorthEastFork,WestSouthEastFork,FourWayFork};
//function for decorating map
void decorate_map(int **map,int width, int height){
//checking patterns in map
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
if(map[i][j]==0)
continue;
if(i>0&&j>0&&i<height-1&&j<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)
map[i][j]=FourWayFork;
else if(j>0&&i<height-1&&j<height-1&&map[i+1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)
map[i][j]=WestSouthEastFork;
else if(i>0&&j>0&&j<height-1&&map[i-1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)
map[i][j]=WestNorthEastFork;
else if(i>0&&j>0&&i<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j-1]==1)
map[i][j]=NorthWestSouthFork;
else if(i>0&&i<height-1&&j<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j+1]==1)
map[i][j]=NorthEastSouthFork;
else if(i<height-1&&j<height-1&&map[i+1][j]==1&&map[i][j+1]==1)
map[i][j]=SouthEastRiverBend;
else if(i>0&&j<height-1&&map[i-1][j]==1&&map[i][j+1]==1)
map[i][j]=NorthEastRiverBend;
else if(j>0&&i<height-1&&map[i+1][j]==1&&map[i][j-1]==1)
map[i][j]=SouthWestRiverBend;
else if(i>0&&j>0&&map[i-1][j]==1&&map[i][j-1]==1)
map[i][j]=NorthWestRiverBend;
}
}
}
int main(){
FILE *fp;
fp=fopen("input.txt","r");//input file
FILE *fp2;
fp2=fopen("output.txt","w");//output file
char P2[3];
if(!(fgets(P2,3,fp)))//inputting first line
printf("Invalid input!");
printf("%s\n",P2);
fprintf(fp2,"%s\n",P2);
int width,height;
if(!(fscanf(fp,"%d",&width)&&fscanf(fp,"%d",&height)))//inputting width and height
printf("Invalid input!");
printf("%d %d\n",width,height);
fprintf(fp2,"%d %d\n",width,height);
int _10_;
if(!(fscanf(fp,"%d",&_10_)))
printf("Invalid input!");
printf("%d\n",_10_);
fprintf(fp2,"%d\n",_10_);
//declaring map of max dimensions
int ** map;
map=(int **)malloc(sizeof(int*)*MAX_DIMENSION);
for(int i=0;i<MAX_DIMENSION;i++)
map[i]=(int *)malloc(sizeof(int)*MAX_DIMENSION);
//inputting map
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
if(!(fscanf(fp,"%d",&map[i][j])))
printf("Invalid input!");
//calling function to decorate map
decorate_map(map,width,height);
//outputting map
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
printf("%d ",map[i][j]);
fprintf(fp2,"%d ",map[i][j]);
}
printf("\n");
fprintf(fp2,"\n");
}
//freeing memory
for(int i=0;i<MAX_DIMENSION;i++)
free(map[i]);
free(map);
}