forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-there- a-way.c
145 lines (96 loc) · 2.8 KB
/
is-there- a-way.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*[40 Points]
----------------------------------------------------------------------
Automated Grading Scheme:
Visible: 2 each for all visible test cases
Hidden: 6 each for all hidden test cases
Manually Grading Scheme:
Note: Give 0 marks for test-case component if there is any form of hard-coding. Eg: printf("0");
Penalty :
-20% each for using any library other than stdio.h
-25% for using global variables
Note: Use of recursion is must. Otherwise, 0 marks are rewarded.
----------------------------------------------------------------------
You are given an N x N grid. You need to find if there exists a path from the cell (x_1, y_1) to the cell (x_2, y_2).
If you are at (x, y) and the value at this cell of the grid is a then you can either move to (x+a, y) or to (x, y+a), provided that you stay inside the grid.
Input
First line of the input contains the number N.
On each of the next N, lines there will be N entries where the jth entry on the ith line cooresponds to the value at the cell (i, j) of the grid. All these entries will be positive.
The next line contains 4 integers, x_1, y_1, x_2 and y_2. All these entries are greater than or equal to 1 and less than or equal to N.
Output
If there exists a path from (x_1, y_1) to (x_2, y_2) print YES, otherwise print NO.
Note:
The numbering of the cells starts from (1,1) and goes on to (N, N).
Examples:
Input 1
2
1 2
1 100
1 1 2 2
Output 1
YES
Explanation 1
From (1, 1) go to (1 + 1, 1) (as a = 1 for (1, 1)) and from (2, 1) go to (2, 1 + 1) (as a = 1 for (2, 1)).
Input 2
4
3 2 1 4
3 2 1 3
3 2 1 2
3 2 1 1
1 4 4 4
Output 2
NO
Explanation 2
We can move from (1, 4) to (1 + 4, 4) or (1, 4 + 4) and both these points take us out of the grid, so it is not possible to reach (4, 4) from (1, 4).*/
// solution:
#include <stdio.h>
void recursion(int n, int mat[n][n], int i, int j, int p, int q, int *flag)
{
if (i == p && j == q)
{
*flag = *flag + 1;
return;
}
if (i > n - 1 || j > n - 1 || i > p || j > q)
{
return;
}
if (i == n - 1)
{
recursion(n, mat, i, j + mat[i][j], p, q, flag);
}
else if (j == n - 1)
{
recursion(n, mat, i + mat[i][j], j, p, q, flag);
}
else
{
recursion(n, mat, i + mat[i][j], j, p, q, flag);
recursion(n, mat, i, j + mat[i][j], p, q, flag);
}
}
int main()
{
int n;
scanf("%d", &n);
int mat[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &mat[i][j]);
}
}
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
int flag = 0;
recursion(n, mat, x1 - 1, y1 - 1, x2 - 1, y2 - 1, &flag);
if (flag == 0)
{
printf("NO");
}
else
{
printf("YES");
}
return 0;
}