-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill_gates.c
74 lines (62 loc) · 1.13 KB
/
bill_gates.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cs50.h"
#define MAX 100
#define MAXX 1000000
int min (int tab[], int n)
{
int i;
int min = MAXX;
for (i = 0; i < n; i++)
{
if (min > tab[i] && tab[i] > 0)
min = tab[i];
}
if (min == MAXX)
min = -1;
return min;
}
int depth(int matrix[MAX][MAX], int size, int x, int y, int length)
{
if (length == size)
return -1;
int connection[MAX];
int j = 0;
if (matrix[x][y] == 1)
return 1;
else
{
int i;
for (i = 0; i < size; i++)
{
if (matrix[x][i] == 1)
connection[j++] = depth(matrix, size, i, y, length+1);
}
if (min (connection, j) == -1)
return -1;
else
return length+ min(connection, j);
}
}
int main(void)
{
int n = GetInt();
int matrix[MAX][MAX];
int i, j;
for (i = 0; i < n; i++)
{
j = 0;
string line = GetString();
string word = strtok(line, " ");
do
{
matrix[i][j++] = atoi(word);
}
while(word = strtok(NULL, " "));
}
int x = GetInt();
int y = GetInt();
printf("%d", depth(matrix, n, x, y, 1));
return 0;
}