Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.
My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.
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! struct pos_t { int x; int y; int distance; }; void { printf("Maze (%ix%i):\n", w, h); int x,y; for { for { int c = map[x * w + y]; if { 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 { if { paths[i][i] = 0; // left if paths[i][i-1] = 1; // right if paths[i][i+1] = 1; // top if paths[i][i-w] = 1; // bottom if paths[i][i+w] = 1; } } // the actual algorithm for { for { for { if { if { 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 { for { if { 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 { 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 { // we know there are borders, so just leave them out for { // 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) { pos = myQueue.front(); myQueue.pop(); if { 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 { 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 { int w,h; scanf("%i %i\n", &w, &h); int *map = (int*)malloc(w * h * sizeof(int)); int x,y; // read maze for { for { map[y * w + x] = getc(stdin); } // get line break getc(stdin); } if == 0 || strcmp(argv[1], "-FloydWarshall") == 0)) { floydWarshall(w, h, map); } else if == 0 || strcmp(argv[1], "-HosamAly") == 0)) { hosamAly(w, h, map); } else { puts("No algorithm specified."); } free(map); return 0; } |