-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_in_a_grid.cpp
79 lines (72 loc) · 1.74 KB
/
robot_in_a_grid.cpp
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
#include <iostream>
#include <array>
#include <bitset>
#include <chrono>
// N = number of rows
// M = number of columns
template<std::size_t N, std::size_t M>
using BitGrid = std::array<std::bitset<M>, N>;
template<std::size_t N, std::size_t M>
static std::size_t numPaths1(std::size_t i, std::size_t j, BitGrid<N, M>& grid)
{
if(i >= M || j >= N || grid[j][i])
return 0;
grid[j][i] = true;
if((i + 1) == M && (j + 1) == N)
{
grid[j][i] = false;
return 1;
}
auto paths = numPaths1<N, M>(i + 1, j, grid) + numPaths1<N, M>(i, j + 1, grid);
grid[j][i] = false;
return paths;
}
template<std::size_t N, std::size_t M>
static std::size_t numPaths1()
{
BitGrid<N, M> grid;
return numPaths1<N, M>(0, 0, grid);
}
template<std::size_t N, std::size_t M>
struct Solution1
{
std::size_t operator()()
{
return numPaths1<N, M>();
}
};
template<template<std::size_t, std::size_t> typename Sol, std::size_t N, std::size_t M>
static void runNumPaths()
{
std::cout << "Input: { Rows = " << N << ", Columns = " << M << " }\n";
auto start = std::chrono::steady_clock::now();
std::size_t paths = Sol<N, M> { }();
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(end - start).count();
std::cout << "Output: " << paths << " paths\n";
std::cout << "Time taken: " << elapsed << " ms\n";
}
template<std::size_t N, std::size_t M>
static void run()
{
static std::size_t runCount = 0;
++runCount;
std::cout << "------ RUN: " << runCount << " ---------\n";
runNumPaths<Solution1, N, M>();
}
int main()
{
run<1, 1>();
run<0, 0>();
run<0, 1>();
run<2, 2>();
run<1, 2>();
run<2, 1>();
run<3, 3>();
run<4, 5>();
run<6, 6>();
run<7, 8>();
run<10, 10>();
run<12, 12>();
return 0;
}