-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25DMaze.cs
More file actions
205 lines (184 loc) · 8.16 KB
/
25DMaze.cs
File metadata and controls
205 lines (184 loc) · 8.16 KB
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
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution
{
const bool OVER = true;
const bool UNDER = false;
const bool HORIZONTAL = true;
const bool VERTICAL = false;
static void Main(string[] args)
{
string[] inputs;
inputs = Console.ReadLine().Split(' ');
int starty = int.Parse(inputs[0]);
int startx = int.Parse(inputs[1]);
inputs = Console.ReadLine().Split(' ');
int endy = int.Parse(inputs[0]);
int endx = int.Parse(inputs[1]);
inputs = Console.ReadLine().Split(' ');
int h = int.Parse(inputs[0]);
int w = int.Parse(inputs[1]);
var maze = new Tile[h, w];
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; ++j)
maze[i, j] = (Tile)Console.Read();
Console.ReadLine();
}
var goodMaze = MakeGoodMaze(maze);
Point start = goodMaze[starty, startx, 0];
Point end = goodMaze[endy, endx, 0];
int answer = FindLengthOfShortestPath(maze, goodMaze, start, end);
PrintPathToError(end);
Console.WriteLine(answer);
Console.Read();
}
enum Tile
{
Floor = '.',
ShortWall = '+', HighWall = '#',
VerticalSlope = '|', HorizontalSlope = '-',
Bridge = 'X', Tunnel = 'O'
}
class Point
{
public int x, y, length;
public bool onTop;
public Point parent;
internal Point(int X, int Y, bool OnTop)
{
x = X;
y = Y;
onTop = OnTop;
length = int.MaxValue;
parent = null;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", x, y, onTop ? "top" : "bottom");
}
}
static Point[,,] MakeGoodMaze(Tile[,] maze)
{
var goodMaze = new Point[maze.GetLength(0), maze.GetLength(1), 2];
for (int y = 0; y < maze.GetLength(0); ++y)
for (int x = 0; x < maze.GetLength(1); ++x)
{
if (maze[y, x] == Tile.Floor || maze[y, x] == Tile.VerticalSlope || maze[y, x] == Tile.HorizontalSlope ||
maze[y, x] == Tile.Bridge || maze[y, x] == Tile.Tunnel)
{
goodMaze[y, x, 0] = new Point(x, y, UNDER);
}
if (maze[y, x] == Tile.ShortWall || maze[y, x] == Tile.VerticalSlope || maze[y, x] == Tile.HorizontalSlope ||
maze[y, x] == Tile.Bridge)
{
goodMaze[y, x, 1] = new Point(x, y, OVER);
}
}
return goodMaze;
}
static void UpdateLengthAndEnqueue(Point current, Point next, Queue<Point> toVisit)
{
if (current.length + 1 < next.length)
{
next.length = current.length + 1;
next.parent = current;
}
toVisit.Enqueue(next);
}
static bool CanGoOver(Tile next, bool onTop, bool horisontal, Tile current)
{
bool res;
if ((current == Tile.VerticalSlope && horisontal) || (current == Tile.HorizontalSlope && !horisontal)) res = false;
else
if (next == Tile.Floor) res = false;
else if (next == Tile.ShortWall) res = onTop;
else if (next == Tile.VerticalSlope) res = horisontal ? false : !onTop;
else if (next == Tile.HorizontalSlope) res = horisontal ? !onTop : false;
else if (next == Tile.HighWall) res = false;
else if (next == Tile.Bridge) res = onTop;
else if (next == Tile.Tunnel) res = false;
else throw new ArgumentException();
Console.Error.WriteLine($"Over) ({current} -> {next}) => {res}");
return res;
}
static bool CanGoUnder(Tile next, bool onTop, bool horisontal, Tile current)
{
bool res;
if ((current == Tile.VerticalSlope && horisontal) || (current == Tile.HorizontalSlope && !horisontal)) res = false;
else
if (next == Tile.Floor) res = !onTop;
else if (next == Tile.ShortWall) res = false;
else if (next == Tile.VerticalSlope) res = horisontal ? false : onTop;
else if (next == Tile.HorizontalSlope) res = horisontal ? onTop : false;
else if (next == Tile.HighWall) res = false;
else if (next == Tile.Bridge) res = !onTop;
else if (next == Tile.Tunnel) res = !onTop;
else throw new ArgumentException();
Console.Error.WriteLine($"Under) ({current} -> {next}) => {res}");
return res;
}
static bool CanGoLeft(Tile[,] maze, Point point, bool over)
{
Console.Error.Write($"\t=> ({point.x - 1}, {point.y}) (Left");
Tile next = maze[point.y, point.x - 1];
Tile current = maze[point.y, point.x];
return over ? CanGoOver(next, point.onTop, HORIZONTAL, current) : CanGoUnder(next, point.onTop, HORIZONTAL, current);
}
static bool CanGoTop(Tile[,] maze, Point point, bool over)
{
Console.Error.Write($"\t=> ({point.x}, {point.y - 1}) (Top");
Tile next = maze[point.y - 1, point.x];
Tile current = maze[point.y, point.x];
return over ? CanGoOver(next, point.onTop, VERTICAL, current) : CanGoUnder(next, point.onTop, VERTICAL, current);
}
static bool CanGoRight(Tile[,] maze, Point point, bool over)
{
Console.Error.Write($"\t=> ({point.x + 1}, {point.y}) (Right");
Tile next = maze[point.y, point.x + 1];
Tile current = maze[point.y, point.x];
return over ? CanGoOver(next, point.onTop, HORIZONTAL, current) : CanGoUnder(next, point.onTop, HORIZONTAL, current);
}
static bool CanGoBottom(Tile[,] maze, Point point, bool over)
{
Console.Error.Write($"\t=> ({point.x}, {point.y + 1}) (Bottom");
Tile next = maze[point.y + 1, point.x];
Tile current = maze[point.y, point.x];
return over ? CanGoOver(next, point.onTop, VERTICAL, current) : CanGoUnder(next, point.onTop, VERTICAL, current);
}
static int FindLengthOfShortestPath(Tile[,] tileMaze, Point[,,] maze, Point start, Point end)
{
var visited = new HashSet<Point>();
var toVisit = new Queue<Point>();
toVisit.Enqueue(start);
start.length = 0;
while (toVisit.Count != 0)
{
var current = toVisit.Dequeue();
visited.Add(current);
Console.Error.WriteLine(current);
if (!visited.Contains(maze[current.y, current.x - 1, 1]) && CanGoLeft(tileMaze, current, OVER)) UpdateLengthAndEnqueue(current, maze[current.y, current.x - 1, 1], toVisit);
if (!visited.Contains(maze[current.y, current.x - 1, 0]) && CanGoLeft(tileMaze, current, UNDER)) UpdateLengthAndEnqueue(current, maze[current.y, current.x - 1, 0], toVisit);
if (!visited.Contains(maze[current.y - 1, current.x, 1]) && CanGoTop(tileMaze, current, OVER)) UpdateLengthAndEnqueue(current, maze[current.y - 1, current.x, 1], toVisit);
if (!visited.Contains(maze[current.y - 1, current.x, 0]) && CanGoTop(tileMaze, current, UNDER)) UpdateLengthAndEnqueue(current, maze[current.y - 1, current.x, 0], toVisit);
if (!visited.Contains(maze[current.y, current.x + 1, 1]) && CanGoRight(tileMaze, current, OVER)) UpdateLengthAndEnqueue(current, maze[current.y, current.x + 1, 1], toVisit);
if (!visited.Contains(maze[current.y, current.x + 1, 0]) && CanGoRight(tileMaze, current, UNDER)) UpdateLengthAndEnqueue(current, maze[current.y, current.x + 1, 0], toVisit);
if (!visited.Contains(maze[current.y + 1, current.x, 1]) && CanGoBottom(tileMaze, current, OVER)) UpdateLengthAndEnqueue(current, maze[current.y + 1, current.x, 1], toVisit);
if (!visited.Contains(maze[current.y + 1, current.x, 0]) && CanGoBottom(tileMaze, current, UNDER)) UpdateLengthAndEnqueue(current, maze[current.y + 1, current.x, 0], toVisit);
}
return end.length;
}
static void PrintPathToError(Point end)
{
if (end.parent != null) PrintPathToError(end.parent);
Console.Error.WriteLine(end);
}
}