8/19/2016
Desi gn and anal ysi s of al gor i thm s - C our se
X
[email protected] ▼
Courses » Design and analysis of algorithms
Announ Announceme cements nts
Forum Forum
Progress Progress
Mento Mentor r
Prisoner Escape Course Course outline Week 1: Introduction
Due on 2016-08-12, 23:59 IST
Prisoner Escape (Baltic Olympiad in Informatics, 2009)
Week 1: Analysis of algorithms Week 1 Quiz Week 2: Searching and sorting Week 2 Quiz
A group of war prisoners are trying to escape from a prison. They have thoroughly planned the escape from the prison itself, and after that they hope to find shelter in a nearby village. However, the village (marked as B, see picture below) and the prison (marked as A) are separated by a canyon which is also guarded by soldiers. These soldiers sit in their pickets and rarely walk; the range of view of each soldier is limited to exactly 100 meters. Thus, depending on the locations of soldiers, it may be possible to pass the canyon safely, keeping the distance to the closest soldier strictly larger than 100 meters at any moment.
Week 2 Programming Assignment Week 3: Graphs Week 3 Quiz Week 3 Programming Assignment Prisoner Escape
Week 4: Weighted graphs Week 4 Quiz Week 4 Programming Assignment Week 5: Data Structures: Union-Find and Heaps
You are to write a program which, given the width and the length of the canyon and the coordinates of every soldier in the canyon, and assuming that soldiers do not change their locations, determines whether prisoners can pass the canyon unnoticed.
Solution Hint The soldiers can be modelled as an undirected graph G. Let each soldier be represented by a vertex. Add an edge between two vertices if and only if the range of view of the corresponding soldiers overlaps. Add two additional vertices s and t, representing the northern and southern side of the canyon, respectively. Connect s and t to those vertices representing soldiers who range of view includes the respective side of the canyon. Use depth-firstsearch or breadth-first-search to check whether there is a path between s
https://onl i necour ses.nptel .ac.i n/noc16_cs22/pr ogassi gnm ent?nam e= 112
1/5
8/19/2016
Week 5: Divide and Conquer Week 5 Quiz Week 5 Programming Assignment
Design and analysis of algorithms - Course
an
n
.
Input format The first line contains three integers L, W, and N – the length and the width of the canyon, and the number of soldiers, respectively. Each of the following N lines contains a pair of integers Xi and Yi – the coordinates of ith soldier in the canyon (0 ≤ Xi ≤ L, 0 ≤ Yi ≤ W). The coordinates are given in meters, relative to the canyon: the southwestern corner of the canyon has coordinates (0, 0), and the northeastern corner of the canyon has coordinates (L,W), as seen in the picture above. Note that passing the canyon may start at coordinate (0,ys) for any 0 ≤ ys ≤ W and end at coordinate (L,ye) for any 0 &\le; ye ≤ W. Neither ys nor ye need to be integer.
Output format Output a single integer: 0 if the prisoners can escape, 1 if they cannot.
Test data 1≤W ≤ 50,000 1 ≤ L ≤ 50,000 1 ≤ N ≤ 250
Example Sample input 1 130 340 5 10 50 130 130 70 170 0 180 60 260
Sample output 1 1
Sample input 2 500 250 250 100 400
300 4 0 300 150 150
Sample output 2 0 Sample Test Cases Input 130 340 5 10 50 Test Case 1 https://onlinecourses.nptel.ac.in/noc16_cs22/progassignment?name=112
130 130 70 170
Output
1 2/5
8/19/2016
Design and analysis of algorithms - Course
0 180 60 260 500 300 6 100 0 100 150
Test Case 2
100 300
1
400 0 400 150 400 300 500 300 5 250 0
Test Case 3
250 150 250 300
1
100 150 400 150 500 300 4 250 0
Test Case 4
250 300
0
100 150 400 150 500 300 5 50 25
Test Case 5
450 25 100 275
0
250 120 400 275 500 300 6 250 0 100 300
Test Case 6
200 150
1
300 150 400 300 250 300
Due Date Exceeded. You scored 100.0/100. Your last recorded submission was :
https://onlinecourses.nptel.ac.in/noc16_cs22/progassignment?name=112
3/5
8/19/2016
Design and analysis of algorithms - Course
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
#include #include #include #include #include
using namespace std;
#define sq(x) ((x)*(x)) struct pt:pair { int i; pt(int x=0,int y=0):pair(x,y),i(‐1) { };
}
int main() { int L,W,n,r=100,d=2*r; cin>>L>>W>>n;
set
points;
typedef set::iterator iter; pt P;
list G[n+2]; //Prepare Graph with intelligence {using lgn based set} for(int i=1; i<=n; i++) { cin>>P.first>>P.second; P.i=i; pt Q(P.first‐d,P.second‐d),R(P.first+ d,P.second+d); iter l=points.lower_bound(Q),u=points.upper_bound(R); for(iter j=l; j!=u; ++j) // only those circles which can overl if(sq(P.first‐j‐>first)+sq(P.second‐j‐>second) <= sq(d)) G[i].push_back(j‐>i), G[j‐>i].push_back( i); // if P is touching y=0 if(P.second ‐ r <= 0) G[i].push_back(0), G[0].push_back(i); if(P.second + r >= W) // if P is touching y=W G[i].push_back(n+1), G[n+1].push_back(i);
}
points.insert(P);
//Do DFS/BFS to track int visited[n+2]; for(int i=0;i
deque q; q.push_back(0); // the y=0 line
while (!q.empty()) { int v=q.front(); q.pop_front(); if(visited[v]) continue ; visited[v]=1; if(v==n+1) //the y=L line { cout<<1<<"\n"; //there is an path from y=0 to y=L return 0; } for(list::iterator i=G[v].begin(); i != G[v].end(); ++i) q.push_back(*i);
}
}
cout<<0<<"\n"; return 0;
End
https://onlinecourses.nptel.ac.in/noc16_cs22/progassignment?name=112
4/5
8/19/2016
Design and analysis of algorithms - Course
© 2014 NPTEL - Privacy & Terms - Honor Code - FAQs A pro ject o f
0
In association with
Funded by
Powered by
https://onlinecourses.nptel.ac.in/noc16_cs22/progassignment?name=112
5/5