1. 程式人生 > >資料結構-圖-C語言-PTA-Saving James Bond - Easy Version

資料結構-圖-C語言-PTA-Saving James Bond - Easy Version

Data Structures and Algorithms (English)

7-10 Saving James Bond - Easy Version

以下為自己寫的版本,難過的是測試點2無法通過
#include <stdio.h>
#include <math.h>

#define MaxSize 100
#define MinLength 42.5

bool answer = false;
int N;//代表有幾條鱷魚
int D;//代表跳躍距離

typedef struct Point *pPoint;
struct Point {
	int
x; int y; bool isVisited; }; /* *@program 判斷是否能跳到鱷魚上,Complete */ bool isEdge(Point pointA, Point pointB, int D) { int x = fabs(pointA.x - pointB.x); int y = fabs(pointB.y - pointA.y); if (x * x + y * y <= D * D && x + y != 0) return true; return false; } /* *@program 判斷是否能跳到岸邊,Complete */
bool isSafe(Point point, int D) { if (point.x + D >= 50 || point.x - D <= -50) return true; if (point.y + D >= 50 || point.y - D <= -50) return true; return false; } /* *@program 判斷在島上是否能跳到鱷魚上,Complete */ bool firstJump(Point pointA, pPoint pointB, int D) { int x = fabs(pointA.x - pointB-
>x); int y = fabs(pointB->y - pointA.y); if (x * x + y * y <= (D + 7.5) * (D + 7.5)) { pointB->isVisited = true; return true; } return false; } /* *@program 深度遍歷圖 */ bool DFS(Point point, Point points[], int D) { for (int i = 1; i <= N; i++) { if (points[i].isVisited) continue; if (isEdge(point, points[i], D)) { points[i].isVisited = true; if (isSafe(points[i], D)) answer = true; answer = DFS(points[i], points, D); if(answer==true) break; } } return answer; } int main() { /*initialization Start*/ if (scanf("%d %d", &N, &D)); bool isEscaped = false; Point points[MaxSize]; points[0].x = 0; points[0].y = 0; points[0].isVisited = true; for (int i = 1; i <= N; i += 1) { scanf("%d %d", &points[i].x, &points[i].y); points[i].isVisited = false; } /*initialization End*/ /* 可以直接從孤島上提到岸上 直接輸出 */ if (D >= MinLength) { printf("Yes"); return 0; } for (int i = 1; i <= N; i += 1) { if (firstJump(points[0], &points[i], D)) { isEscaped = DFS(points[i], points, D); if (isEscaped) { printf("Yes"); break; } } } if (!isEscaped) printf("No"); return 0; }

標準答案

#include "math.h"
using namespace std;
int n, m;
#define MINLEN 42.5
struct Pointer {
    int x;
    int y;
}p[101];
bool answer = false; /* 記錄007能否安全逃生~~ */
bool visited[101] = {false}; /* 判斷當前點是否被訪問過 */

bool isSave(int x) { /* 判斷從當前點能否跳到岸上 */
    if ((p[x].x - m <= -50) || (p[x].x + m >= 50) || (p[x].y - m <= -50) || (p[x].y + m >= 50))
        return true;
    return false;
}

bool jump(int x, int y) { /* 判斷2個點距離是否在跳躍能力內 */
        int p1 = pow(p[x].x - p[y].x, 2);
        int p2 = pow(p[x].y - p[y].y, 2);
        int r = m * m;
        if (p1 + p2 <= r)
            return true;
        return false;
}

bool firstJump(int x) {  /* 當007處於孤島時 第一次可以選擇跳的鱷魚 因為第一次判斷能否跳躍的計算方法與後面dfs不相同 所以要單獨寫 */
    int p1 = pow(p[x].x , 2);
    int p2 = pow(p[x].y , 2);
    int r = (m+7.5) * (m+7.5);
    if (p1 + p2 <= r) {
        return true;
    }
    return false;
}
bool dfs(int x) { /* 深搜 */
    visited[x] = true;
    if (isSave(x)) answer = true;
    for (int i = 0; i < n; i++) {
    	/* 沒訪問過 並且在跳躍能力之內 */
        if (!visited[i] && jump(x, i)) {
            answer = dfs(i);
            if (answer == true) break;
        }
    }
    return answer;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
    	scanf("%d %d", &p[i].x, &p[i].y);
    }
    /* 可以直接從孤島上提到岸上 直接輸出 */
    if (m >= MINLEN) { printf("Yes");return 0;}
    /* 如果第一次能夠跳的 並且之前沒有訪問過的節點 則深搜該節點 */
    for (int i = 0; i < n; i++) {
        if (firstJump(i) && !visited[i]) { 
            if (dfs(i)) break;
        }
     }
     if (answer == true) printf("Yes");
     else printf("No");
     return 0;
}