-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMoves.c
More file actions
56 lines (56 loc) · 933 Bytes
/
MatrixMoves.c
File metadata and controls
56 lines (56 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Move through the matrix for a range of queries in four directions
Code:
#include<stdio.h>
#include<string.h>
#define MAX 100
int main()
{
int sq_mat_size,mat[MAX][MAX],ind1,ind2,queries,location=0;
char direction[MAX];
scanf("%d",&sq_mat_size);
for(ind1=1;ind1<=sq_mat_size;ind1++)
{
for(ind2=1;ind2<=sq_mat_size;ind2++)
scanf("%d",&mat[ind1][ind2]);
}
scanf("%d",&queries);
ind1=ind2=1;
while(queries--)
{
scanf("%s",direction);
if(strcmp(direction,"RIGHT")==0)
{
if(ind2+1<=sq_mat_size)
{
ind2++;
location=mat[ind1][ind2];
}
}
else if(strcmp(direction,"LEFT")==0)
{
if(ind2-1>=1)
{
ind2--;
location=mat[ind1][ind2];
}
}
else if(strcmp(direction,"UP")==0)
{
if(ind1-1>=1)
{
ind1--;
location=mat[ind1][ind2];
}
}
else
{
if(ind1+1<=sq_mat_size)
{
ind1++;
location=mat[ind1][ind2];
}
}
}
printf("%d",location);
return 0;
}