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 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 |
struct Coord { {memset(this, NULL, sizeof(this));} : x(_x), y(_y) {} int x, y; friend bool { if (a.y==b.y) return a.x<b.x; else return a.y<b.y; } // for std::map friend bool { return a.x==b.x && a.y==b.y; } friend Coord { return Coord(a.x+b.x, a.y+b.y); } friend std::ostream& { std::stringstream s; s << "[" << a.x << "," << a.y << "]"; o << s.str(); return o; } }; struct LongestDistance { {memset(this, NULL, sizeof(this));} Coord a, b; int distance; }; struct Node { Node(bool _open=true, bool _visited=false, int _distance=0) : open(_open), visited(_visited), distance(_distance) {} bool open; bool visited; int distance; }; class Graph { public: { for { for m_map[Coord(i,j)] = Node(); } m_best.distance = 0; // the four basic directions dirs.push_back(Coord(0, -1)); dirs.push_back(Coord(1, 0)); dirs.push_back(Coord(0, 1)); dirs.push_back(Coord(-1, 0)); } {} void { if!=m_map.end()) m_map[c].open = flag; } void reset() { for(Map::iterator it = m_map.begin(); it != m_map.end(); ++it) { (*it).second.distance = INT_MAX; (*it).second.visited = false; } } void { reset(); Map::iterator current = m_map.find(start); (*current).second.distance = 0; std::list<Map::iterator> queue; queue.push_back(current); while!=0) { // find next node to compute current = *queue.begin(); for(std::list<Map::iterator>::iterator it = queue.begin(); it != queue.end(); ++it) { if.second.distance<(*current).second.distance) current = (*it); } queue.remove(current); // consider neighbours for; it != dirs.end(); ++it) // step in every direction { Map::iterator ni = m_map.find((*current).first+(*it)); if && !(*ni).second.visited) { Node* n = &(*ni).second; if// && !n->visited) { int dist = (*current).second.distance+1; if n->distance = dist; queue.push_back(ni); } } } //print_ascii(); //_getch(); (*current).second.visited = true; } // update m_best Map::iterator longest = m_map.end(); for; it != m_map.end(); ++it) { if.second.open && (*it).second.distance!=INT_MAX) if || (*it).second.distance>(*longest).second.distance) longest = it; } if.second.distance>m_best.distance) { m_best.a = start; m_best.b = (*longest).first; m_best.distance = (*longest).second.distance; } } void { m_best.distance = 0; for(Map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { if.second.open) dijkstra((*it).first); //print_ascii(); //_getch(); } } void print_ascii() { // marks the two best points with an X for(Map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { std::cout << ((*it).first.x==0?"\n":""); if.first==m_best.a || (*it).first==m_best.b) { std::cout << "X"; //std::cout << (*it).second.distance; } else { //if ((*it).second.visited) // std::cout << (*it).second.distance; // looks good until distance becomes two digit //else std::cout << ((*it).second.open?".":"#"); } } std::cout << std::endl; } const LongestDistance& { return m_best; } private: typedef std::map<Coord, Node> Map; Map m_map; LongestDistance m_best; std::list<Coord> dirs; }; int { int sizex = 15; int sizey = 15; Graph g(sizex, sizey); // make a open area enclosed with blocked squares // longest route should be a diagonal for { for g.set_open(Coord(x,y), !(x==0 || y==0 || x==sizex-1 || y==sizey-1)); } // run for one //g.dijkstra(Coord(1,1)); g.dijkstra_all(); g.print_ascii(); std::cout << g.best().a << " " << g.best().b << " " << g.best().distance << std::endl; return 1; } |