码迷,mamicode.com
首页 > 其他好文 > 详细

05-2. Saving James Bond - Easy Version (PAT) - 图的遍历问题

时间:2015-02-12 21:25:40      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

 

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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No

题意:给出各个顶点的坐标,要求判断在给出的跳跃半径下,能否达到岸边位置
解题思路:根据给出的顶点坐标,建立图中各个顶点的连通关系(根据两个顶点的距离是否小于给出跳跃半径判断两个顶点是否连通),再从原点开始进行遍历,当存在能够到达岸边的顶点时,返回yes,否则返回no
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;

#define ISLANDDIAMETER 15

typedef struct{
    int x;
    int y;
}coord; 

typedef struct Graph{
    int *graphMatrix;
    bool *visited;
    int vertexNumber;
    coord *aryCoord;    //保存图中各个结点的坐标
}*pGraph, nGraph;

pGraph CreateGraph( int vertexNumber )
{
    pGraph pG;
    pG = ( pGraph )malloc( sizeof( nGraph ) );
    pG->vertexNumber = vertexNumber;
    int len = ( vertexNumber * ( vertexNumber + 1 ) / 2 );
    pG->graphMatrix = ( int* )malloc( len * sizeof( int ) );
    memset( pG->graphMatrix, 0, sizeof( int ) * len);
    pG->visited = ( bool* )malloc( vertexNumber * sizeof( bool ) );
    memset( pG->visited, false, sizeof( bool ) * vertexNumber );
    pG->aryCoord = ( coord * )malloc( vertexNumber * sizeof( coord ) );
    return pG;
}

pGraph InsertVertex( pGraph pG, int index, int xCoord, int yCoord )
{
    if ( index >= pG->vertexNumber )
    {
        cout << "VertexIndex Out of Range!" << endl;
        return pG;
    }
    pG->aryCoord[index].x = xCoord;
    pG->aryCoord[index].y = yCoord;
    return pG;
}

pGraph JudgeEdge( pGraph pG, int ver1, int ver2, int rad )
{
    if ( ver1 >= pG->vertexNumber || ver2 >= pG->vertexNumber )
    {
        cout << "Error Edge!" << endl;
        return pG;
    }
    int tmp;
    if ( ver1 < ver2 )
    {
        tmp = ver1;
        ver1 = ver2;
        ver2 = tmp;
    }
    //计算两个结点的距离的平方
    int lengthSquare;
    int xDiff;
    int yDiff;
    xDiff = pG->aryCoord[ver1].x - pG->aryCoord[ver2].x;
    yDiff = pG->aryCoord[ver1].y - pG->aryCoord[ver2].y;
    lengthSquare = xDiff * xDiff + yDiff * yDiff;
    if ( lengthSquare <= rad * rad )
    {
        int item = ver1 * ( ver1 + 1 ) / 2 + ver2;
        pG->graphMatrix[item] = 1;
    }
    return pG;
}

bool IsSafe( pGraph pG, int vertex, int rad )
{
    if ( vertex == 0 )
    {
        rad = rad + ISLANDDIAMETER / 2;
    }
    if ( abs( pG->aryCoord[vertex].x ) + rad >= 50 || abs( pG->aryCoord[vertex].y ) + rad  >= 50 )
    {
        return true;
    }
    return false;
}

bool DFS( pGraph pG, int vertex, int rad )
{
    bool answer = false;
    pG->visited[vertex] = true;
    if ( IsSafe( pG, vertex, rad) )
    {
        answer = true;
    }
    else
    {
        int    rows, cols;
        int materixIndex;
        for ( cols = 0; cols < vertex; cols++ )    //扫描行
        {
            materixIndex = vertex * ( vertex + 1 ) / 2 + cols;
            if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ cols ] == false )
            {
                answer = DFS( pG, cols, rad );
                if ( answer == true )
                {
                    break;
                }
            }
        }
        for ( rows = vertex; rows < pG->vertexNumber; rows++ )
        {
            materixIndex = rows * ( rows + 1 ) / 2 + vertex;
            if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ rows ] == false )
            {
                answer = DFS( pG, rows, rad );
                if ( answer == true )
                {
                    break;
                }
            }
        }
    }
    return answer;
}

int main()
{
    int num;
    int dis;
    cin >> num >> dis;
    int i;
    int j;
    pGraph pG;
    pG = CreateGraph( num + 1 );
    InsertVertex( pG, 0, 0, 0 );    //插入第一个结点(0, 0)
    int xCoord;
    int yCoord;
    for ( i = 1; i <= num; i++ )
    {
        cin >> xCoord >> yCoord;
        InsertVertex( pG, i, xCoord, yCoord );
    }
    //连接各个结点的边
    for ( i = 0; i <= num; i++ )
    {
        for ( j = i + 1; j <= num; j++ )
        {
            if ( i == 0 )
            {
                JudgeEdge( pG, i, j, dis + ISLANDDIAMETER / 2 );
            }
            else
            {
                JudgeEdge( pG, i, j, dis );
            }
        }
    }
    //进行深度搜索
    bool answer = false;
    answer = DFS( pG, 0, dis );
    if ( answer == true )
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}

 

05-2. Saving James Bond - Easy Version (PAT) - 图的遍历问题

标签:

原文地址:http://www.cnblogs.com/liangchao/p/4288867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!