public override bool Equals(object obj) { KnownStates o = (KnownStates)obj; for (int i = 0; i < this.N; i++) { if (this.poss[i] != o.poss[i]) { return(false); } } return(true); }
public KnownStates Move(char dir, KnownStates good, char[,] map) { KnownStates afterMove = new KnownStates(); afterMove.N = this.N; afterMove.M = this.M; afterMove.poss = new long[N]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (this.isPoss(i, j)) { int i2 = i; int j2 = j; if (dir == 'D') { i2++; } if (dir == 'L') { j2--; } if (dir == 'R') { j2++; } if (map[i2, j2] == '#') { i2 = i; j2 = j; } if (!good.isPoss(i2, j2)) { return(null); } afterMove.AddPoss(i2, j2); } } } return(afterMove); }
/// <summary> /// Converts a given <see cref="KnownStates"/> to a snake case <see cref="string"/>. /// </summary> /// <param name="state">A <see cref="KnownStates"/>.</param> /// <returns> /// The state as a <see cref="string"/>. /// </returns> public static string ToStateString(this KnownStates state) { return(knownStatesCache.AsString(state)); }
private string SolveCave(int N, int M, int cavei, int cavej, char[,] map) { bool[,] poss = new bool[N, M]; poss[cavei, cavej] = true; Queue <int> oQ = new Queue <int>(); oQ.Enqueue(cavei); oQ.Enqueue(cavej); while (oQ.Count > 0) { int i = oQ.Dequeue(); int j = oQ.Dequeue(); int i2; int j2; // UP i2 = i - 1; j2 = j; if (!poss[i2, j2] && map[i2, j2] != '#') { poss[i2, j2] = true; oQ.Enqueue(i2); oQ.Enqueue(j2); } // LEFT i2 = i; j2 = j - 1; if (!poss[i2, j2] && map[i2, j2] != '#') { poss[i2, j2] = true; oQ.Enqueue(i2); oQ.Enqueue(j2); } // RIGHT i2 = i; j2 = j + 1; if (!poss[i2, j2] && map[i2, j2] != '#') { poss[i2, j2] = true; oQ.Enqueue(i2); oQ.Enqueue(j2); } } KnownStates good = new KnownStates(); good.N = N; good.M = M; good.poss = new long[N]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (poss[i, j]) { good.AddPoss(i, j); } } } Queue <KnownStates> q = new Queue <KnownStates>(); HashSet <KnownStates> visited = new HashSet <KnownStates>(); bool lucky = false; if (good.NumStates == 1) { lucky = true; } else { q.Enqueue(good); visited.Add(good); while (q.Count > 0) { KnownStates cur = q.Dequeue(); for (int i = 0; i < moves.Length; i++) { KnownStates next = cur.Move(moves[i], good, map); if (next != null && !visited.Contains(next)) { if (next.NumStates == 1) { lucky = true; break; } visited.Add(next); q.Enqueue(next); } } } } return(string.Format("{0} {1}", good.NumStates, lucky ? "Lucky" : "Unlucky")); }