-
Notifications
You must be signed in to change notification settings - Fork 1
/
DownRight.c
105 lines (91 loc) · 1.71 KB
/
DownRight.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
/* solve using C
How many way to reach the end point from start.
You can only move right and down
start
.- + - + -.
| | | |
.- + - + -.
| | | |
.- + - + -.
| | | |
.- + - + -.
end
*/
#include <stdio.h>
#define BITS_PER_BYTE 8
int solve_numeric(int nrow, int ncol);
int print_path (unsigned int num, int nrow, int ncol);
int bit_match(unsigned int num, int n);
char *sym = "DR";
int main(int argc, int *argv[])
{
int nrow, ncol;
if (argc != 3)
{
printf ("\r Usage:\n %s <nrow> <ncol>\r\n", argv[0]);
return -1;
}
nrow = atoi(argv[1]);
ncol = atoi(argv[2]);
if (!(1<<(nrow+ncol+1)))
{
printf ("\r Try dimension below %d\r\n", (sizeof(int)*BITS_PER_BYTE)-1);
return -1;
}
solve_numeric(nrow, ncol);
return 0;
}
int solve_numeric(int nrow, int ncol)
{
unsigned int min = 0, max, i;
int nsol = 0;
for (i = 0; i < ncol; i++)
{
min <<= 1;
min |= 0x01;
}
max = min;
max <<= nrow;
for (i = min; i <= max; i++)
{
if(bit_match(i, ncol))
{
nsol++;
print_path(i, nrow, ncol);
}
}
printf ("\r %d solutions found\r\n", nsol);
return 0;
}
int solve_graph(int nrow, int ncol)
{
/* ToDo: Complete this function
Someday gotta construct a tree here and find the ans
*/
return 0;
}
/* Checks if exact number of 1s are present */
int bit_match(unsigned int num, int n)
{
unsigned int nbit = 0;
while (num)
{
nbit += num & 0x01;
num >>= 1;
}
return (nbit == n);
}
/* Prints the path */
int print_path (unsigned int num, int nrow, int ncol)
{
char temp_str[sizeof(int)*BITS_PER_BYTE+1];
int i = 0;
for (i = 0; num || (i < (nrow+ncol)); i++)
{
temp_str[i] = sym[num & 0x01];
num >>= 1;
}
temp_str[i] = '\0';
printf ("\r %s\r\n", temp_str);
return 0;
}