Island Path
Given a collection of island than can (or not) be connected eachother by bridges (north, east, south and west bridge). Return "PATH" if there is a path that connects island A and B. Return "NOT PATH" otherwise.
Also, return this path.
Input Format
Each value of this matrix will have a decimal number.
This decimal number has the following representantion in hexadecimal: North = 0x1
East = 0x2
South = 0x4
East = 0x8
Begin = 0xA
End = 0x10
Constraints
The matrix will be squared and it´s size will be: 4 <= size <= 8.
Output Format
Return the path by printing each island´s position.
Sample Input 1
14 0 0 0
05 0 0 0
05 0 0 0
17 0 0 0
Sample Output 1
PATH
(0,0)
(1,0)
(2,0)
(3,0)
My solution:
C#:
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IslandPath { class Program { public static int beginX, beginY, endX, endY; public struct Node { public Boolean North, East, South, West, Begin, End; public List<Node> Neighbours; public int posX, posY; public int parentX, parentY; public Node(int x) { North = false; East = false; South = false; West = false; Begin = false; End = false; Neighbours = new List<Node>(); posX = 0; posY = 0; parentX = 0; parentY = 0; } } static public Boolean isNorth(int value) { //remove begin value if exist if (value > 10 && value < 16) value -= 10; else if (value > 16) //remove end value if exist { value -= 16; } int result = value & 1; //if the rest of value is 1, then it has North value if (result == 1) return true; else return false; } static public Boolean isEast(int value) { //remove begin value if exist if (value > 10 && value < 16) value -= 10; else if (value > 16) //remove end value if exist { value -= 16; } int result = value & 2; //if the rest of value is 1, then it has East value if (result == 2) return true; else return false; } static public Boolean isSouth(int value) { //remove begin value if exist if (value > 10 && value < 16) value -= 10; else if (value > 16) //remove end value if exist { value -= 16; } int result = value & 4; //if the rest of value is 1, then it has South value if (result == 4) return true; else return false; } static public Boolean isWest(int value) { //remove begin value if exist if (value > 10 && value < 16) value -= 10; else if (value > 16) //remove end value if exist { value -= 16; } int result = value & 8; //if the rest of value is 1, then it has North value if (result == 8) return true; else return false; } static public Boolean isBegin(int value) { if (value > 10 && value < 16) return true; else return false; } static public Boolean isEnd(int value) { if (value > 16) return true; else return false; } static public Node[][] CreateGraph(int[][] matrix, int numColumns) { Node[][] matrixNode = new Node[numColumns][]; for (int i=0;i<numColumns ; i++) { matrixNode[i] = new Node[numColumns]; for (int j = 0; j < numColumns; j++) { matrixNode[i][j].Neighbours = new List<Node>(); matrixNode[i][j].posX = i; matrixNode[i][j].posY = j; if (isNorth(matrix[i][j])) { matrixNode[i][j].North = true; } if (isEast(matrix[i][j])) { matrixNode[i][j].East = true; } if (isSouth(matrix[i][j])) { matrixNode[i][j].South = true; } if (isWest(matrix[i][j])) { matrixNode[i][j].West = true; } if (isBegin(matrix[i][j])) { beginX = i; beginY = j; matrixNode[i][j].Begin = true; } if (isEnd(matrix[i][j])) { endX = i; endY = j; matrixNode[i][j].End = true; } } } return matrixNode; } static void fillNeighbour(ref Node[][] matrixNode, int numColumns) { for (int i = 0; i < numColumns; i++) { for (int j = 0; j < numColumns; j++) { if(matrixNode[i][j].North==true && i>0 && matrixNode[i-1][j].South==true) { matrixNode[i][j].Neighbours.Add(matrixNode[i - 1][j]); } if (matrixNode[i][j].East == true && j < numColumns - 1 && matrixNode[i][j + 1].East == true) { matrixNode[i][j].Neighbours.Add(matrixNode[i][j+1]); } if (matrixNode[i][j].South == true && i < numColumns - 1 && matrixNode[i + 1][j].North == true) { matrixNode[i][j].Neighbours.Add(matrixNode[i + 1][j]); } if (matrixNode[i][j].West == true && j > 0 && matrixNode[i][j-1].East == true) { matrixNode[i][j].Neighbours.Add(matrixNode[i][j-1]); } } } } static Node SetParentsPos(Node item, Node element) { item.parentX = element.posX; item.parentY = element.posY; return item; } static void Main(string[] args) { //read first line int[] firstLine = Array.ConvertAll(Console.ReadLine().Split(' '), lineTemp => Convert.ToInt32(lineTemp)); int numColumns = firstLine.Length; int[][] matrix = new int[numColumns][]; matrix[0] = new int[numColumns]; //copying firstLine to matrix first line for (int i = 0; i < numColumns; i++) { matrix[0][i] = firstLine[i]; } //reading the rest of the matrix for (int i = 1; i < numColumns; i++) { matrix[i] = Array.ConvertAll(Console.ReadLine().Split(' '), lineTemp => Convert.ToInt32(lineTemp)); } Node[][] matrixNode = CreateGraph(matrix, numColumns); //fill neighbour data of each node fillNeighbour(ref matrixNode, numColumns); //Apply Breadth-first search Queue <Node> queue = new Queue<Node>(); matrixNode[beginX][beginY].status = 1; //visiting first node queue.Enqueue(matrixNode[beginX][beginY]); //enqueue Begin Node Stack<Node> parentsStack = new Stack<Node>(); Node NeighbourWithParent = SetParentsPos(matrixNode[beginX][beginY], matrixNode[beginX][beginY]); parentsStack.Push(NeighbourWithParent); while (queue.Count > 0) { var element = queue.Dequeue(); if (element.Neighbours.Count()>0) { foreach (var Neighbour in element.Neighbours) { //at this point, neighbour dont have their parent position, so we have to set it NeighbourWithParent = SetParentsPos(Neighbour, element); parentsStack.Push(NeighbourWithParent); queue.Enqueue(Neighbour); //if found end element if (Neighbour.End) { int nextNodeX = -1; int nextNodeY = -1; //print path while (parentsStack.Count > 0) { Node item = parentsStack.Pop(); //verify if is correct to print if (nextNodeX == -1 || (item.posX == nextNodeX && item.posY == nextNodeY)) { Console.WriteLine("(" + item.posX + "," + item.posY + ")"); nextNodeX = item.parentX; nextNodeY = item.parentY; } } Console.WriteLine("PATH"); Console.ReadKey(); return; } } } else //visit Node if (element.End) { Console.WriteLine("PATH"); Console.ReadKey(); return; } } Console.WriteLine("NOT PATH"); Console.ReadKey(); } } } |