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
#include <iostream>
#include <map>
#include <list>
#include <sstream>
#include <conio.h>

struct Coord
{
	Coord() {memset(this, NULL, sizeof(this));}
	Coord(int _x, int _y) : x(_x), y(_y) {}
	int x, y;

	friend bool operator<(const Coord& a, const Coord& b) 
	{ if (a.y==b.y) return a.x<b.x; else return a.y<b.y; } // for std::map

	friend bool operator==(const Coord& a, const Coord& b) 
	{ return a.x==b.x && a.y==b.y; }

	friend Coord operator+(const Coord& a, const Coord& b) 
	{ return Coord(a.x+b.x, a.y+b.y); }

	friend std::ostream& operator<<(std::ostream& o, const Coord& a)
	{ std::stringstream s; s << "[" << a.x << "," << a.y << "]"; o << s.str(); return o; }
};

struct LongestDistance
{
	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:
	Graph(int sx, int sy)
	{
		for (int i = 0; i<sx; ++i)
		{
			for (int j = 0; j<sy; ++j)
				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));
	}
	~Graph() {}

	void set_open(const Coord& c, bool flag)
	{
		if (m_map.find(c)!=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 dijkstra(const Coord& start)
	{
		reset();

		Map::iterator current = m_map.find(start);
		(*current).second.distance = 0;

		std::list<Map::iterator> queue;
		queue.push_back(current);

		while (queue.size()!=0)
		{
			// find next node to compute
			current = *queue.begin();
			for(std::list<Map::iterator>::iterator it = queue.begin(); it != queue.end(); ++it)
			{
			    if ((**it).second.distance<(*current).second.distance)
					current = (*it);
			}
			queue.remove(current);

			// consider neighbours
			for (std::list<Coord>::const_iterator it = dirs.begin(); it != dirs.end(); ++it) // step in every direction
			{
			    Map::iterator ni = m_map.find((*current).first+(*it));
				if (ni!=m_map.end() && !(*ni).second.visited)
				{
					Node* n = &(*ni).second;
					if (n->open)// && !n->visited)
					{
						int dist = (*current).second.distance+1;
						if (dist<n->distance)
							n->distance = dist;
						queue.push_back(ni);
					}
				}
			}
			//print_ascii();
			//_getch();
			(*current).second.visited = true;
		}

		// update m_best
		Map::iterator longest = m_map.end();
		for (Map::iterator it = m_map.begin(); it != m_map.end(); ++it)
		{
			if ((*it).second.open && (*it).second.distance!=INT_MAX)
				if (longest==m_map.end() || (*it).second.distance>(*longest).second.distance)
					longest = it;
		}
		if ((*longest).second.distance>m_best.distance)
		{
			m_best.a = start;
			m_best.b = (*longest).first;
			m_best.distance = (*longest).second.distance;
		}
	}

	void dijkstra_all()
	{
		m_best.distance = 0;
		for(Map::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
		{
		    if ((*it).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 ((*it).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&	best()	{ return m_best; }

private:
	typedef std::map<Coord, Node>	Map;
	Map								m_map;
	LongestDistance					m_best;
	std::list<Coord>				dirs;
};

int main()
{
	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 (int y = 0; y<=sizey; ++y)
	{
		for (int x = 0; x<=sizex; ++x)
			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;
}