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 10Sample Output 1:
4 0 11 10 21 10 35Sample Input 2:
4 13 -12 12 12 12 -12 -12 12 -12Sample Output 2:
0
————————————————————
自己做题的速度还是不行,不够快。
这个题目的测试点5是从小岛范围内可以直接跳到岸边……
测试点4是验证步数第一跳最小的情况。
和上一个easy版本的主要差别在于路径的保存,这里用到一个数组。
path[w]=v;表示,w的上一个节点为v,最后,利用一个堆栈可以直接得出路径,当然也可以递归。
这个数组,只要bfs一下就好了,上面一个版本为dfs。
对于第一跳最小的情况,需要进行排序,试了好几个方法,还是感觉自带的vector比较好用。
bool cmp(cro a, cro b)
{
return (a.x*a.x+a.y*a.y)<(b.x*b.x+b.y*b.y);
}
pGraph mysort(pGraph g)
{
vector<cro> v;
for(int i=0; i<=g->Num; i++)
{
v.push_back(g->croco[i]);
}
sort(v.begin(),v.end(),cmp);
for(int i=0; i<v.size(); i++)
{
g->croco[i]=v[i];
}
return g;
}
其他情况和上一个版本处理基本上相同。
总体代码如下。
#include <iostream>
#include <stdlib.h>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <stdio.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define DIA 15
//vector<cro> path;
int *path;
int *dist;
typedef struct crocodile
{
int x;
int y;
} cro;
typedef struct Graph
{
int Num;
cro *croco;
int *matrix;
bool *visited;
}*pGraph;
pGraph Creat(int num)
{
pGraph g=(pGraph)malloc(sizeof(struct Graph));
g->croco=(cro *)malloc(num*sizeof(struct crocodile));
g->Num=num;
int len=num*(num+1)/2;
g->matrix=(int *)malloc(len*(sizeof(int)));
memset(g->matrix,0,len*sizeof(int));
g->visited=(bool *)malloc(num*sizeof(bool));
memset(g->visited,false,num*sizeof(bool));
path = new int[num];
memset(path, -1, num*sizeof(int));
dist = new int[num];
memset(dist, -1, num*sizeof(int));
return g;
}
pGraph Insertg(pGraph g, int x, int y, int index)
{
g->croco[index].x=x;
g->croco[index].y=y;
return g;
}
pGraph linkg(pGraph g, int id1, int id2, int D)
{
if(id1<id2)
{
int tmp=id1;
id1=id2;
id2=tmp;
}
int dx=(g->croco[id1].x-g->croco[id2].x);
int dy=(g->croco[id1].y-g->croco[id2].y);
if(dx*dx+dy*dy <= D*D)
{
int idex=id1*(id1+1)/2 + id2;
g->matrix[idex]=1;
}
return g;
}
bool isSafe(pGraph g, int id, int D)
{
if(id==0)
{
D=D+DIA/2;
}
if(abs(g->croco[id].x)+D>=50 || abs(g->croco[id].y)+D>=50)
{
return true;
}
return false;
}
void printg(pGraph g, int v)
{
int step=1;
stack<int> s;
while(v != 0)
{
s.push(v);
step++;
v=path[v];
}
cout<<step<<endl;
while(!s.empty())
{
int t=s.top();
s.pop();
cout<<g->croco[t].x<<' '<<g->croco[t].y<<endl;
}
}
void bfs(pGraph g, int id, int D)
{
queue<int> q;
bool ans=false;
g->visited[id]=true;
q.push(0);
dist[0]=0;
while(!q.empty())
{
int v=q.front();q.pop();
int row,col,idx;
for(col=0; col<v; col++)
{
idx=v*(v+1)/2+col;
if(g->matrix[idx] && !g->visited[col])
{
g->visited[col]=true;
path[col]=v;
q.push(col);
if(isSafe(g,col,D))
{
printg(g,col);
return;
}
}
}
for(row=v; row<g->Num; row++)
{
idx=row*(row+1)/2+v;
if(g->matrix[idx] && !g->visited[row])
{
g->visited[row]=true;
path[row]=v;
q.push(row);
if(isSafe(g,row,D))
{
printg(g,row);
return;
}
}
}
}
cout<<0<<endl;
}
bool cmp(cro a, cro b)
{
return (a.x*a.x+a.y*a.y)<(b.x*b.x+b.y*b.y);
}
pGraph mysort(pGraph g)
{
vector<cro> v;
for(int i=0; i<=g->Num; i++)
{
v.push_back(g->croco[i]);
}
sort(v.begin(),v.end(),cmp);
for(int i=0; i<v.size(); i++)
{
g->croco[i]=v[i];
}
return g;
}
int main(int argc, char** argv) {
int N,D;
cin>>N>>D;
pGraph G=Creat(N+1);
Insertg(G,0,0,0);
for(int i=0; i<N; i++)
{
path[i]=dist[i]=-1;
}
for(int i=1; i<=N; i++)
{
int xx,yy;
cin>>xx>>yy;
Insertg(G,xx,yy,i);
}
for(int i=0; i<=N; i++)
{
for(int j=i+1; j<=N; j++)
{
if(i==0)
{
G=mysort(G);
}
i==0?linkg(G,i,j,D+DIA/2):linkg(G,i,j,D);
}
}
if(isSafe(G,0,D))
{
cout<<1<<endl;
return 0;
}
// for(int i=1; i<=N; i++)
// {
// memset(G->visited,false,(N+1)*(N+2)/2*sizeof(bool));
// memset(path,-1,N+1);
bfs(G,1,D);
// }
return 0;
}