diff --git a/algorithms/DP/Travelling Salesman/DP_TSP.cpp b/algorithms/DP/Travelling Salesman/DP_TSP.cpp new file mode 100755 index 0000000..a81d393 --- /dev/null +++ b/algorithms/DP/Travelling Salesman/DP_TSP.cpp @@ -0,0 +1,43 @@ +// Collecting Beepers +// DP TSP + +#include +#include +#include +#include +using namespace std; + +int i, j, TC, xsize, ysize, n, x[11], y[11], dist[11][11], memo[11][1 << 11]; // Karel + max 10 beepers + +int tsp(int pos, int bitmask) { // bitmask stores the visited coordinates + if (bitmask == (1 << (n + 1)) - 1) + return dist[pos][0]; // return trip to close the loop + if (memo[pos][bitmask] != -1) + return memo[pos][bitmask]; + + int ans = 2000000000; + for (int nxt = 0; nxt <= n; nxt++) // O(n) here + if (nxt != pos && !(bitmask & (1 << nxt))) // if coordinate nxt is not visited yet + ans = min(ans, dist[pos][nxt] + tsp(nxt, bitmask | (1 << nxt))); + return memo[pos][bitmask] = ans; +} + +int main() { + scanf("%d", &TC); + while (TC--) { + scanf("%d %d", &xsize, &ysize); // these two values are not used + scanf("%d %d", &x[0], &y[0]); + scanf("%d", &n); + for (i = 1; i <= n; i++) // karel's position is at index 0 + scanf("%d %d", &x[i], &y[i]); + + for (i = 0; i <= n; i++) // build distance table + for (j = 0; j <= n; j++) + dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]); // Manhattan distance + + memset(memo, -1, sizeof memo); + printf("The shortest path has length %d\n", tsp(0, 1)); // DP-TSP + } + + return 0; +} diff --git a/algorithms/DP/Travelling Salesman/README.md b/algorithms/DP/Travelling Salesman/README.md new file mode 100644 index 0000000..b493f5a --- /dev/null +++ b/algorithms/DP/Travelling Salesman/README.md @@ -0,0 +1,44 @@ +**Note** +Rename this file to README.md +When viewing on Github, click the Raw button to view the unrendered file. + +# TRAVELLING SALESMAN PROBLEM + +Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point. + +# SAMPLE QUESTION + +Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set ofinteger coordinates (x and y). Your job is to design a program that will help Karel pick up a numberof beepers that are placed in her world. To do so you must direct Karel to the position where eachbeeper is located. Your job is to write a computer program that nds the length of the shortest paththat will get Karel from her starting position, to each of the beepers, and return back again to thestarting position. + +Karel can only move along the x and y axis, never diagonally. Moving from one position (i,j) to an adjacent position (i; j+ 1), (i; j -1), (i - 1 , j ) , or (i+ 1; j) has a cost of one.You can assume that Karel's world is never larger than 20*20 squares and that there will neverbe more than 10 beepers to pick up. Each coordinate will be given as a pair (x; y) where each valuewill be in the range 1 through the size of that particular direction of the coordinate system + +### Input Format + +First there will be a line containing the number of scenarios you are asked to help Karel in. For eachscenario there will rst be a line containing the size of the world. This will be given as two integersx-size andy-size). Next there will be one line containing two numbers giving the starting position ofKarel. On the next line there will be one number giving the number of beepers. For each beeper therewill be a line containing two numbers giving the coordinates of each beeper + +### Output Format + +The output will be one line per scenario, giving the minimum distance that Karel has to move to getfrom her starting position to each of the beepers and back again to the starting position. + +### Sample Input + +``` +1 +10 10 +1 1 +4 +2 3 +5 5 +9 4 +6 5 +``` + +### Sample Output + +``` +The shortest path has length 24 +``` + +### Implemented in: + +- [C++](algorithms_with_git/algorithms/DP/'Travelling Salesman'/DP_TSP.cpp)