zoukankan      html  css  js  c++  java
  • 07.图5 Saving James Bond

    07-图5 Saving James Bond - Hard Version (30分)

    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:
    0

    #include<iostream>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const double D = 15.0;
    double jump, cr[101][2];        //跳跃距离和鳄鱼坐标
    int n;
    bool cmp(int i, int j);
    int bfs(int& v, vector<int>& path);
    
    int main()
    {
    	int i, v = 0, flag = 0;
    	cin >> n >> jump;
    	vector<int>path(n + 1, -1), spath;    //创建一个动态数组并初始化为-1,和一个堆栈将路径倒出
    	cr[0][0] = 0; cr[0][1] = 0;       //从1开始存储坐标数据
    	for (i = 1; i <= n; i++)
    		cin >> cr[i][0] >> cr[i][1];
    	if (jump + D / 2 >= 50)  //直接可以跳上岸边
    		flag = 1;
    	else flag = bfs(v, path);
    	if (!flag)cout << "0";
    	else {
    		while (v) {
    			spath.push_back(v);
    			v = path[v];
    		}
    		cout << spath.size() + 1;
    		for (i = spath.size() - 1; i >= 0; i--)
    			cout << cr[spath[i]][0] << cr[spath[i]][1];
    	}
    	return 0;
    }
    //v是开始的结点 ,但是值最后成了尾结点(传的引用)
    int bfs(int& v, vector<int>& path) {
    	vector<int>dist(n + 1, -1), first(n + 1);
    	queue<int>q;
    	double firstdis;
    	int flag = 0, i;
    	dist[v] = 0;
    	for (i = 0; i < n + 1; i++)
    		first[i] = i;            
    	sort(first.begin(), first.end(), cmp);      //将第一条距离最短的结点放在开头
    	for (i = 1; i < n + 1; i++) {
    		v = first[i];                               //第一步类似于初始化
    		firstdis = sqrt(pow(cr[v][0], 2) + pow(cr[v][1], 2));
    		if (firstdis > D / 2 && firstdis <= jump + D / 2) {
    			q.push(v);
    			dist[v] = 1;
    			path[v] = 0;
    		}
    	}
    	while (!q.empty()) {
    		v = q.front();
    		q.pop();
    		if ((fabs(cr[v][0]) + jump >= 50 || fabs(cr[v][1]) + jump >= 50)) {
    			flag = 1;
    			break;
    		}
    		else {
    			for (i = 1; i < n + 1; i++) {
    				if (dist[i] == -1 && (sqrt(pow(cr[v][0] - cr[i][0], 2) + pow(cr[v][1] - cr[i][1], 2)) <= jump)) {
    					dist[i] = dist[v] + 1;
    					path[i] = v;
    					q.push(i);
    				}
    			}
    		}
    	}
    	return flag;
    }
    
    bool cmp(int i, int j) {
    	return pow(cr[i][0], 2) + pow(cr[i][1], 2) < pow(cr[j][0], 2) + pow(cr[j][1], 2);
    }
    

    加粗样式

  • 相关阅读:
    第七届湘计算机程序设计竞赛的学生 报道称,数字游戏
    python IDE
    字符串处理
    Ajax得知(两)—— 一个简单的Ajax示例
    九度OJ 1068 球半径和数量 (模拟)
    centos 之7zip
    svnclient本地化和异常处理
    java web.xml listener servlet 和filter加载顺序
    Jquery zTree实例
    探索Android中的Parcel机制(上)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109995.html
Copyright © 2011-2022 走看看