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
// compile using g++ -O2 -o maze -Wall maze.cpp
// mazes _must_ have a border around them!

#include <cstdio>
#include <cstring>
#include <queue>

struct pos_t {
	int x;
	int y;
	int distance;
};

void printMap(int w, int h, int *map) {
	printf("Maze (%ix%i):\n", w, h);
	int x,y;
	for (x = 0; x < h; x++) {
		for (y = 0; y < w; y++) {
			int c = map[x * w + y];
			if (c < 0) {
				printf("%i", -c);
			} else {
				putc(c, stdout);
			}
		}
		putc('\n', stdout);
	}
}

void floydWarshall(int w, int h, int *map) {
	const int verticesCount = w*h;
	unsigned short paths[verticesCount][verticesCount];
	
	// fill the array, all edges are initially infinty
	memset(paths, 0xFF, sizeof(short) * verticesCount * verticesCount);
		
	int i,j,k;
	
	// set all i->i edges to cost 0
	// set all i->adjecent(i) to cost 1 or leave them at infinity if there's an obstacle
	for (i = 0; i < verticesCount; i++) {
		if (map[i] == '.') {
			paths[i][i] = 0;
			// left
			if (i > 0 && map[i-1] == '.')
				paths[i][i-1] = 1;
				
			// right
			if (i < verticesCount && map[i+1] == '.')
				paths[i][i+1] = 1;
				
			// top
			if (i > w && map[i-w] == '.')
				paths[i][i-w] = 1;
				
			// bottom
			if (i + w < verticesCount && map[i+w] == '.')
				paths[i][i+w] = 1;
		}
	}
	
	// the actual algorithm
	for (k = 0; k < verticesCount; k++) {
		for (i = 0; i < verticesCount; i++) {
			for (j = 0; j < verticesCount; j++) {
				if (paths[i][k] != 0xFFFF && paths[k][j] != 0xFFFF) {
					if (paths[i][k] + paths[k][j] < paths[i][j]) {
						paths[i][j] = paths[i][k] + paths[k][j];
					}
				}
			}
		}
	}
	
	puts("Fartest Distance is (Floyd Warshall):");
	// find greatest distance
	int distance = 0;
	int endi = 0;
	int endj = 0;
	for (i = 0; i < verticesCount; i++) {
		for (j = 0; j < verticesCount; j++) {
			if (paths[i][j] != 0xFFFF && paths[i][j] > distance) {
				distance = paths[i][j];
				endi = i;
				endj = j;
			}
		}
	}
	printf("(%i, %i) -> (%i, %i): %i\n", endi % w, endi / w, endj % w, endj / w, distance);
	map[endi] = 'S';
	map[endj] = 'E';
	printMap(w,h,map);
}

void hosamAly(int w, int h, int *map) {
	const size_t mapSize = w * h * sizeof(int);
	
	int x,y;
	pos_t fartestStart = {0,0,0};
	pos_t fartestEnd = {0,0,0};
		
	int *mapCopy = (int*)malloc(mapSize);
	std::queue<pos_t> myQueue;
	for (x = 1; x < w - 1; x++) { // we know there are borders, so just leave them out
		for (y = 1; y < h - 1; y++) {
			// flood fill the map and find the fartest point.
			memcpy(mapCopy, map, mapSize);
			pos_t startPos = {x, y, 0};
			myQueue.push(startPos);
			pos_t pos;
			while (!myQueue.empty()) {
				pos = myQueue.front();
				myQueue.pop();
				if (mapCopy[w * pos.y + pos.x] == '.') {
					mapCopy[w * pos.y + pos.x] = pos.distance;
				
					pos_t leftPos = {pos.x + 1, pos.y, pos.distance - 1};
					pos_t rightPos = {pos.x - 1, pos.y, pos.distance - 1};
					pos_t topPos = {pos.x, pos.y - 1, pos.distance - 1};
					pos_t bottomPos = {pos.x, pos.y + 1, pos.distance - 1};
					myQueue.push(leftPos);
					myQueue.push(rightPos);
					myQueue.push(topPos);
					myQueue.push(bottomPos);
					
					// is it farter than the old point?
					if (pos.distance < fartestEnd.distance) {
						fartestStart = startPos;
						fartestEnd = pos;
					}
				}
			}
		}
	}
	
	puts("Fartest Distance is (Hosam Aly):");
	printf("(%i, %i) -> (%i, %i): %i\n", fartestStart.x, fartestStart.y, fartestEnd.x, fartestEnd.y, -fartestEnd.distance);
	
	map[fartestStart.x + fartestStart.y * w] = 'S';
	map[fartestEnd.x + fartestEnd.y * w] = 'E';
	printMap(w,h,map);
	
	free(mapCopy);
}

int main(int argc, char *argv[]) {	
	int w,h;
	scanf("%i %i\n", &w, &h);
	
	int *map = (int*)malloc(w * h * sizeof(int));
	
	int x,y;
	// read maze
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			map[y * w + x] = getc(stdin);
		}
		// get line break
		getc(stdin);
	}
		
	if (argc > 1 && (strcmp(argv[1], "-FW") == 0 || strcmp(argv[1], "-FloydWarshall") == 0)) {
		floydWarshall(w, h, map);
	} else if (argc > 1 && (strcmp(argv[1], "-HA") == 0 || strcmp(argv[1], "-HosamAly") == 0)) {
		hosamAly(w, h, map);
	} else {
		puts("No algorithm specified.");
	}
	
	free(map);
	
	return 0;
}