1votos

Número de caminos en una cuadrícula en C#

por josejuan hace 6 años

Al contrario que en Haskell (que en estructuras inmutables es trivial) paralelizar mutable es difícil, no he encontrado una forma eficiente de mantener el uso de las CPU al 100% durante todo el proceso (hay subcaminos mucho más complejos que otros). De todos modos, se confirma que la estrategia de eliminación de lazos cerrados mejora (para n=7 en un 50%) la directa (eg. sólo fronteras).

Dada una cuadrícula de NxN nodos, ¿cuantos caminos diferentes hay (sin pasar dos veces por el mismo nodos) al ir de una esquina a su opuesta diagonal?

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* 
** 8 cores reales ** 
 
$mono __paths.exe 6 4 
maxN = 6, maxSpan = 4 
n = 2, c = 2, t = 00:00:00,031 
n = 3, c = 12, t = 00:00:00,000 
n = 4, c = 184, t = 00:00:00,000 
n = 5, c = 8512, t = 00:00:00,000 
n = 6, c = 1262816, t = 00:00:00,046 
 
$mono __paths.exe 7 13 
maxN = 7, maxSpan = 13 
... 
n = 7, c = 575780564, t = 00:00:17,316 
*/ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
 
namespace __paths { 
 
    enum State { 
        Up, 
        Down, 
        Left, 
        Right, 
        Empty, 
        Objetive 
 
    class Grid { 
        private int ss, s, x, y; 
        private State [,] g; 
        public Grid(int size) { 
            ss = size; 
            s = size + 2; 
            g = new State [s, s]; 
        public void Reset() { 
            for(var x = 0; x < s; x++) 
                for(var y = 0; y < s; y++) 
                    g [x, y] = State.Empty; 
            for(var c = 0; c < s; c++) { 
                g [0, c] = g [s - 1, c] = State.Up; 
                g [c, 0] = g [c, s - 1] = State.Left; 
            g [1, 1] = State.Up; 
            g [s - 2, s - 2] = State.Objetive; 
            this.x = this.y = 1; 
        public Grid Clone() { 
            var g_ = new Grid(s - 2); 
            for(var x = 0; x < s; x++) 
                for(var y = 0; y < s; y++) 
                    g_.g [x, y] = (State) ((int) g [x, y]); 
            g_.x = this.x; 
            g_.y = this.y; 
            return g_; 
        public State ChkUp() { 
            return g [x, y - 1]; 
        public State ChkDown() { 
            return g [x, y + 1]; 
        public State ChkLeft() { 
            return g [x - 1, y]; 
        public State ChkRight() { 
            return g [x + 1, y]; 
 
        public void GoUp() { 
            g [x, --y] = State.Up; 
        public void BaUp() { 
            g [x, y++] = State.Empty; 
        public void GoDown() { 
            g [x, ++y] = State.Down; 
        public void BaDown() { 
            g [x, y--] = State.Empty; 
        public void GoLeft() { 
            g [--x, y] = State.Left; 
        public void BaLeft() { 
            g [x++, y] = State.Empty; 
        public void GoRight() { 
            g [++x, y] = State.Right; 
        public void BaRight() { 
            g [x--, y] = State.Empty; 
         
        // genera una serie de caminos iniciales 
        public IEnumerable<Grid> Span(int level, ref int startCount) { 
            var l = new List<Grid>(); 
            if(level == 0) 
                l.Add(this); 
            else { 
                State s; 
                if((s = ChkUp()) == State.Empty) { 
                    var g_ = Clone(); 
                    g_.GoUp(); 
                    l.AddRange(g_.Span(level - 1, ref startCount)); 
                } else 
                    if(s == State.Objetive) 
                        startCount++; 
                if((s = ChkDown()) == State.Empty) { 
                    var g_ = Clone(); 
                    g_.GoDown(); 
                    l.AddRange(g_.Span(level - 1, ref startCount)); 
                } else 
                    if(s == State.Objetive) 
                        startCount++; 
                if((s = ChkRight()) == State.Empty) { 
                    var g_ = Clone(); 
                    g_.GoRight(); 
                    l.AddRange(g_.Span(level - 1, ref startCount)); 
                } else 
                    if(s == State.Objetive) 
                        startCount++; 
                if((s = ChkLeft()) == State.Empty) { 
                    var g_ = Clone(); 
                    g_.GoLeft(); 
                    l.AddRange(g_.Span(level - 1, ref startCount)); 
                } else 
                    if(s == State.Objetive) 
                        startCount++; 
            return l; 
        public int DoLeft() { 
            var w = ChkLeft(); 
            if(w == State.Objetive) 
                return 1; 
            if(w == State.Empty) { 
                GoLeft(); 
                var k = CountL(); 
                BaLeft(); 
                return k; 
            return 0; 
        public int DoRight() { 
            var w = ChkRight(); 
            if(w == State.Objetive) 
                return 1; 
            if(w == State.Empty) { 
                GoRight(); 
                var k = CountR(); 
                BaRight(); 
                return k; 
            return 0; 
        public int DoUp() { 
            var w = ChkUp(); 
            if(w == State.Objetive) 
                return 1; 
            if(w == State.Empty) { 
                GoUp(); 
                var k = CountU(); 
                BaUp(); 
                return k; 
            return 0; 
        public int DoDown() { 
            var w = ChkDown(); 
            if(w == State.Objetive) 
                return 1; 
            if(w == State.Empty) { 
                GoDown(); 
                var k = CountD(); 
                BaDown(); 
                return k; 
            return 0; 
        public int CountLR(State z) { 
            var k = 0; 
            if(z != State.Left) 
                k += DoLeft(); 
            if(z != State.Right) 
                k += DoRight(); 
            return k; 
        public int CountUD(State z) { 
            var k = 0; 
            if(z != State.Up) 
                k += DoUp(); 
            if(z != State.Down) 
                k += DoDown(); 
            return k; 
        public int CountD() { 
            return DoDown() + CountLR(ChkDown()); 
        public int CountU() { 
            return DoUp() + CountLR(ChkUp()); 
        public int CountL() { 
            return DoLeft() + CountUD(ChkLeft()); 
        public int CountR() { 
            return DoRight() + CountUD(ChkRight()); 
 
        public int Count() { 
            switch(g [x, y]) { 
                case State.Up: 
                    return CountU(); 
                case State.Down: 
                    return CountD(); 
                case State.Left: 
                    return CountL(); 
                case State.Right: 
                    return CountR(); 
            throw new Exception(); 
 
    class Program { 
 
        static void Main(string [] args) { 
 
            var maxN = int.Parse(args [0]); 
            var maxSpan = int.Parse(args [1]); 
 
            Console.WriteLine("maxN = {0}, maxSpan = {1}", maxN, maxSpan); 
 
            for(var n = 2; n <= maxN; n++) { 
                var t0 = DateTime.UtcNow; 
                var g = new Grid(n); 
                g.Reset(); 
                g.GoDown(); 
                int startCount = 0; 
                var r = 2 * g.Span(maxSpan, ref startCount).AsParallel().Sum(h => h.Count()); 
                var t1 = DateTime.UtcNow; 
                Console.WriteLine("n = {0}, c = {1}, t = {2:hh\\:mm\\:ss\\,fff}", n, r + 2 * startCount, t1 - t0 ); 
 
 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.