#include #include #include #include using namespace std; template class matrix { protected: int rows, cols; T * data; public: matrix(int r, int c) { rows = r; cols = c; data = new T[r * c]; } ~matrix() { delete [] data; } int numrows() { return rows; } int numcols() { return cols; } T & at(int r, int c) { assert(r >= 0 && r < rows); assert(c >= 0 && c < cols); return data[r * cols + c]; } }; template class queue { protected: T * data; int size, num, first, last; public: queue() { size = 4; data = new T[size]; num = 0; first = 0; last = -1; } bool is_empty() { return size == 0; } void do_to_contents(string intro, void f(T item)) { cout << intro; int pos = first; for (int i = 0; i < num; i += 1) { f(data[pos]); pos += 1; if (pos == size) pos = 0; } cout << "\n"; } void print_everything_and_do_to_contents(string intro, void f(T item)) { cout << intro << "size = " << size << ", num = " << num << ", first = " << first << ", last = " << last << "\n"; do_to_contents(" ", f); } void enlarge() { int newsize = size * 2; T * newdata = new T [newsize]; for (int dest = 0; dest < num; dest += 1) { newdata[dest] = data[first]; first += 1; if (first == size) first = 0; } delete data; data = newdata; size = newsize; first = 0; last = num - 1; } void add(T value) { if (num == size) enlarge(); if (last + 1 == size) last = -1; data[last + 1] = value; last += 1; num += 1; } T remove() { T value = data[first]; first += 1; // num += 1; here is the careless mistake, it should obviously be: num -= 1; if (first == size) first = 0; return value; } }; template class searchable_vector: public vector { public: bool contains(T value) { for (int i = 0; i < vector::size(); i += 1) if (vector::at(i) == value) return true; return false; } void print() { for (int i = 0; i < vector::size(); i += 1) cout << vector::at(i) << " "; cout << "\n"; } }; struct state { int location; searchable_vector path; state(int loc, searchable_vector p): location(loc), path(p) { } }; void printer(state * ps) { cout << ps->location << " "; } int main(int argc, char * argv[]) { assert(argc == 2); ifstream fin(argv[1]); if (fin.fail()) { cerr << "Can't read " << argv[1] << "\n"; exit(1); } int N; fin >> N; matrix map(N, N); for (int r = 0; r < N; r += 1) for (int c = 0; c < N; c += 1) fin >> map.at(r, c); fin.close(); for (int r = 0; r < N; r += 1) { for (int c = 0; c < N; c += 1) cout << map.at(r, c) << " "; cout << "\n"; } int from, to; cout << "start point and destination? "; cin >> from >> to; queue Q; searchable_vector p0; p0.push_back(from); Q.add(new state(from, p0)); while (! Q.is_empty()) { Q.print_everything_and_do_to_contents(" ", printer); state * s = Q.remove(); int here = s->location; searchable_vector path = s->path; cout << "here = " << here << "\n"; if (here == to) { cout << "Found it\nPath = "; path.print(); break; } for (int next = 0; next < N; next += 1) if (map.at(here, next) == 1 && ! path.contains(next)) { path.push_back(next); Q.add(new state(next, path)); path.pop_back(); } delete s; } }