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
|
#include <iostream>
using namespace std;
void findRoutes(int row, int col);
int subway[12][12] = {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}};
char stationLetters[13] = "ABCDEFGHIJKL"; //station letters
char order[25] = "A"; //set up for route order, up to 25 conservatively
int routes = 0, i = 1;
main()
{
findRoutes(0, 0); //start with first row, first column
cout << endl << routes << " different routes can be taken, after " << routes
<<" days the user will have to repeate routes." << endl;
return 0;
}
void findRoutes(int row, int col)
{
while (col < 12)
{
if (subway[row][col] == 0)
{
if (row == 11)
{
routes++;
cout << routes << ". " << order << endl;
return;
}
col++;
if (row != 11 && col == 12)
{
return;
}
}
else if (subway[row][col] == 1)
{
order[i] = stationLetters[col];
i++;
subway[row][col] = 0;
subway[col][row] = 0;
findRoutes(col, 0);
order[i] = 0;
i--;
subway[row][col] = 1;
subway[col][row] = 1;
col++;
if (row != 11 && col == 12)
{
return;
}
}
}
}
|