1
+ #include < iostream>
2
+ #include < fstream>
3
+ #include < iterator>
4
+ #include < memory>
5
+ #include < vector>
6
+ #include < string>
7
+ #include < set>
8
+ #include < map>
9
+ #include < algorithm>
10
+
11
+
12
+ using field_t = std::vector< std::vector< uint64_t > >;
13
+ using point_t = std::pair< uint64_t , uint64_t >;
14
+ using visited_t = std::map< point_t , uint64_t >;
15
+ using front_t = std::set< point_t >;
16
+
17
+
18
+ inline bool in_visited ( point_t const & p, visited_t const & visited )
19
+ {
20
+ return visited.find ( p ) != visited.end ();
21
+ }
22
+
23
+ inline visited_t nexts ( uint64_t const x, uint64_t const y, field_t const & field )
24
+ {
25
+ visited_t n;
26
+
27
+ if ( x != 0 )
28
+ n.emplace ( std::make_pair ( x - 1 , y ), field[ x - 1 ][ y ] );
29
+ if ( y != 0 )
30
+ n.emplace ( std::make_pair ( x, y - 1 ), field[ x ][ y - 1 ] );
31
+ if ( x != field.size () - 1 )
32
+ n.emplace ( std::make_pair ( x + 1 , y ), field[ x + 1 ][ y ] );
33
+ if ( y != field[ 0 ].size () - 1 )
34
+ n.emplace ( std::make_pair ( x, y + 1 ), field[ x ][ y + 1 ] );
35
+
36
+ return n;
37
+ }
38
+
39
+ inline visited_t nexts ( point_t const & p, field_t const & field )
40
+ {
41
+ return nexts ( p.first , p.second , field );
42
+ }
43
+
44
+ inline uint64_t incl ( uint64_t x )
45
+ {
46
+ ++x;
47
+ if ( x == 10 )
48
+ x = 1 ;
49
+ return x;
50
+ }
51
+
52
+
53
+ int main ( int argc, char * argv[] )
54
+ {
55
+ if ( argc < 2 )
56
+ return 0 ;
57
+
58
+ field_t field;
59
+
60
+ std::ifstream ifs ( argv[ 1 ] );
61
+ for ( std::string s; std::getline ( ifs, s ); )
62
+ {
63
+ std::vector< uint64_t > v;
64
+ std::transform (
65
+ s.begin (), s.end (),
66
+ std::back_inserter< std::vector< uint64_t > >( v ),
67
+ []( char const c ) -> uint64_t
68
+ {
69
+ return c - ' 0' ;
70
+ }
71
+ );
72
+ size_t const sz ( v.size () );
73
+ for ( size_t i ( 1 ); i != 5 ; ++i )
74
+ for ( size_t j ( 0 ); j != sz; ++j )
75
+ v.push_back ( incl ( v[ sz * ( i - 1 ) + j ] ) );
76
+
77
+ field.push_back ( std::move ( v ) );
78
+ }
79
+
80
+ size_t const sz ( field.size () );
81
+ for ( size_t i ( 1 ); i != 5 ; ++i )
82
+ for ( size_t j ( 0 ); j != sz; ++j )
83
+ {
84
+ std::vector v ( field[ sz * ( i - 1 ) + j ] );
85
+ std::for_each ( v.begin (), v.end (), []( uint64_t & x ) { x = incl ( x ); } );
86
+ field.push_back ( std::move ( v ) );
87
+ }
88
+
89
+ point_t const target{ field.size () - 1 , field[ 0 ].size () - 1 };
90
+
91
+ visited_t weights;
92
+ weights.emplace ( std::make_pair ( 0 , 0 ), 0 );
93
+
94
+ front_t front;
95
+ front.emplace ( std::make_pair ( 0 , 0 ) );
96
+
97
+ while ( !front.empty () )
98
+ {
99
+ point_t const point ( front.extract ( front.begin () ).value () );
100
+ for ( auto const & [ p, w ] : nexts ( point, field ) )
101
+ {
102
+ if ( !in_visited ( p, weights ) || ( weights[ point ] + w < weights[ p ] ) )
103
+ {
104
+ weights[ p ] = weights[ point ] + w;
105
+ front.emplace ( p );
106
+ }
107
+ }
108
+ }
109
+
110
+ std::cout << " result = " << weights[ target ] << std::endl;
111
+
112
+ return 0 ;
113
+ }
0 commit comments