/* * cities.cpp * * Created on: 10.01.2013 * Author: trifon */ #include using namespace std; const int MAX = 10; bool road[MAX][MAX] = { false }; bool visited[MAX] = { false }; int ncities; int path[MAX] = { 0 }; int step; void readRoads() { cin >> ncities; int from, to; do { cin >> from >> to; if (from >= 0 && to >= 0) road[from][to] = true; } while (from >= 0 || to >= 0); } void printRoads() { for(int i = 0; i < ncities; i++) { for(int j = 0; j < ncities; j++) cout << road[i][j] << ' '; cout << endl; } } bool findWay(int start, int end) { if (start == end) { path[step] = end; return true; } if (visited[start]) return false; // посещаваме start visited[start] = true; // добавяме start в пътя path[step] = start; step++; for(int i = 0; i < ncities; i++) if (road[start][i] && findWay(i, end)) return true; step--; return false; } int main() { readRoads(); printRoads(); int start, end; cin >> start >> end; step = 0; if (findWay(start, end)) cout << "Има"; else cout << "Няма"; cout << " път от " << start << " до "; cout << end << endl; cout << "Пътят е:" << endl; for(int i = 0; i <= step; i++) cout << path[i] << ' '; return 0; }