In: Computer Science
(Written in C)The input below shows a grid dimension (first line), the following 2 are the positions of the start cell and end cell, and the rest is the positions of cells blocked. Keep in mind that the input is an txt file to be read through stdin (you can not type input multiple times). How do I read the input using scanf . Also, it is not allow to store input into array (because we can not assume the size of the grid with limited number).
10x10 [0,0] [9,9] 9 blocks
The expected output is like this:
grid has 10 rows and 10 columns. grid has 9 block(s). initial cell in the grid is [0,0]. goal cell in the grid is [9,9].
Help !! (thumbs up)
I have created a program .
In the program below , replace the path to the file with the file you have .
(Notice that the 2nd and 4th lines are swapped . But the result is same , i hope .)
If it helped , please consider liking .
#include<stdlib.h>
#include<stdio.h>
int main()
{
char
c,filename[]="/home/meliodas/Documents/CPP/Hackerrank/file.txt"; //
replace the file path here
char rows[2] ,columns[2],blocks[2],initial[10],goal[10];
FILE *fptr;
fptr=fopen(filename,"r"); // opening file
if (fptr==NULL)
{
printf("Cannot open ");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
printf("grid has ");
while (c!='x')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" rows");
c = fgetc(fptr);
printf("\ngrid has ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" columns");
c = fgetc(fptr);
printf("\ninitial cell in the grid is ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}
c = fgetc(fptr);
printf("\ngoal cell in the grid is ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}
c = fgetc(fptr);
printf("\ngrid has " );
while (c!=' ')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" block(s)");
fclose(fptr);
return 0;
}