Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. public class Solution {
  2.     int min=Int32.MaxValue;
  3.     public int OpenLock(string[] deadends, string target) {
  4.         HashSet<string> dead=new HashSet<string>();
  5.         foreach (string i in deadends)
  6.             dead.Add(i);
  7.        
  8.         bfs("0000",target,dead);
  9.         return min==Int32.MaxValue?-1:min;
  10.     }
  11.    
  12.     void bfs(string source,string destination,HashSet<string> deadends)
  13.     {        
  14.        
  15.         if (deadends.Contains(source))
  16.             return;
  17.         Queue<string> q=new Queue<string>();
  18.         q.Enqueue(source);
  19.         Dictionary<string, int> visited = new Dictionary<string, int>();
  20.         visited.Add("0000", 0);
  21.        
  22.         while (q.Count>0)
  23.         {
  24.             source=q.Dequeue();
  25.             int depth = visited[source];
  26.             if (source==destination)
  27.             {
  28.                 min=depth;
  29.                 return;
  30.             }
  31.            
  32.  
  33.            
  34.             List<string> neighbours=GetNeighbours(source);
  35.             for (int i=0;i<neighbours.Count;i++)
  36.             {
  37.                 if (visited.ContainsKey(neighbours[i]))
  38.                     continue;
  39.                
  40.                 if (deadends.Contains(neighbours[i]))
  41.                     continue;
  42.                
  43.                 if (!visited.ContainsKey(neighbours[i]))
  44.                     visited.Add(neighbours[i],depth+1);
  45.                
  46.                 q.Enqueue(neighbours[i]);
  47.  
  48.             }
  49.            
  50.         }
  51.        
  52.     }
  53.    
  54.     List<string> GetNeighbours(string s)
  55.     {
  56.         List<string> neighbours=new List<string>();
  57.         for (int i=0;i<s.Length;i++)
  58.         {
  59.             StringBuilder sb=new StringBuilder(s);
  60.             if (s[i]=='0')
  61.             {
  62.                 sb[i]='1';
  63.                 neighbours.Add(sb.ToString());
  64.                    
  65.                 sb[i]='9';
  66.                 neighbours.Add(sb.ToString());                    
  67.             }
  68.             else if (s[i]=='9')
  69.             {
  70.                 sb[i]='8';
  71.                 neighbours.Add(sb.ToString());
  72.                    
  73.                 sb[i]='0';
  74.                 neighbours.Add(sb.ToString())  ;                  
  75.             }else
  76.             {
  77.                
  78.                 sb[i]=(char)(((int)s[i]) + 1);
  79.                 neighbours.Add(sb.ToString());
  80.                    
  81.                 sb[i]=(char)(((int)s[i]) - 1);
  82.                 neighbours.Add(sb.ToString());                    
  83.             }
  84.                            
  85.         }
  86.        
  87.         /*foreach(string i in neighbours)
  88.         {
  89.             Console.Write(i+" ");
  90.         }
  91.         Console.WriteLine();*/
  92.        
  93.         return neighbours;
  94.     }
  95.    
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement