This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
Sample Input 1:
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
Sample Output 1:
4
0 11
10 21
10 35
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <stack>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
stack <int> st,path,temp;
int vis[200]={0};
int flag=0;
int n,m,step_count=0,min_count=9999999,min_size=9999999;
struct eyu
{
int x;
int y;
}eyu[200];
double dis(int x1,int y1,int x2,int y2)
{
double d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return d;
}
void dfs(int a)
{
if((abs(50-(eyu[a].x)<=m))||abs(50-(eyu[a].y)<=m))
{
flag=1;
if(step_count<=min_count)
{
min_count=step_count;
}
if(st.size()<min_size)
{
min_size=st.size();
while(!path.empty())
{
path.pop();
}
while(!temp.empty())
{
temp.pop();
}
while(!st.empty())
{
int s=st.top();
temp.push(s);
path.push(s);
st.pop();
}
while(!temp.empty())
{
int s=temp.top();
st.push(s);
temp.pop();
}
}
return ;
}
for(int i=1;i<=n;i++)
{
if((vis[i]!=1)&&(dis(eyu[a].x,eyu[a].y,eyu[i].x,eyu[i].y)<=m))
{
vis[i]=1;
st.push(i);
step_count++;
dfs(i);
step_count--;
st.pop();
vis[i]=0;
}
}
}
int main(int argc, char *argv[]) {
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>eyu[i].x>>eyu[i].y;
}
if(n==1)
{
cout<<"2"<<endl;
cout<<eyu[1].x<<" "<<eyu[1].y;
}
for(int i=1;i<=n;i++)
{
step_count=0;
if(dis(0,0,eyu[i].x,eyu[i].y)<=m)
{
while(!st.empty())
{
st.pop();
}
vis[i]=1;
st.push(i);
step_count++;
dfs(i);
}
}
if(flag==1)
{
cout<<min_count+1<<endl;
while(!path.empty())
{
int s=path.top();
cout<<eyu[s].x<<" "<<eyu[s].y<<endl;;
path.pop();
}
}else
{
cout<<"0";
}
return 0;
}