-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion65.c
56 lines (45 loc) · 801 Bytes
/
question65.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
/*
http://practice.geeksforgeeks.org/problems/shortest-path-from-1-to-n/0
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int findMin(int a, int b) {
return a < b ? a : b;
}
int numEdges(int n) {
int i,j;
int dp[n+1][n+1];
for(i=0;i<=n;i++){
dp[0][i] = 0;
dp[i][0] = 0;
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++) {
if(j-i-1 >= 0 && j-3*i >= 0){
dp[i][j] = 1 + findMin(dp[i][j-i-1],dp[i][j-3*i]);
}else if(j-3*i < 0 && j-i-1 >=0){
dp[i][j] = 1 + dp[i][j-i-1];
}else{
dp[i][j] = 200;
}
}
}
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
printf("%d\t", dp[i][j]);
}
printf("\n");
}
return dp[1][n];
}
int main() {
int cases;
int i;
scanf("%d",&cases);
for(i=0;i<cases;i++) {
int n;
scanf("%d",&n);
printf("%d\n", numEdges(n));
}
}