标签:
这道题我们可以采用广度优先搜索的办法,在我看来实现起来要比深搜更好一些
核心算法写为了bfs()函数 = =
#include <iostream> #include <queue> using namespace std; typedef struct{ int x; int y; }location; int n, dis; location *crocodile; bool *map; queue<location> path; bool bfs(); int main() { /** 初始化 **/ cin >> n >> dis; crocodile = (location *)malloc(n*sizeof(location)); map = (bool *)malloc(n*sizeof(bool)); for (int i = 0; i < n; i++){ cin >> crocodile[i].x >> crocodile[i].y; map[i] = false; } /** 初始化 **/ /** 第一跳 **/ for (int i = 0; i < n; i++){ if ((15 + 2 * dis)*(15 + 2 * dis) >= (crocodile[i].x * crocodile[i].x)+(crocodile[i].y*crocodile[i].y)){ path.push(crocodile[i]); map[i] = true; } } /** 第一跳 **/ /** 宽搜啦 **/ bool flag = bfs(); if(flag == true){ cout << "Yes" << endl; } else{ cout << "No" << endl; } return 0; } bool bfs() { while (path.empty() != true){ int x = path.front().x, y = path.front().y; if (x <= -50 + dis || x >= 50 - dis||y <= -50 + dis || y >= 50 - dis){ return true; } else{ path.pop(); for (int i = 0; i < n; i++){ if (map[i]==false){ int x1 = crocodile[i].x, y1 = crocodile[i].y; if ((x - x1)*(x - x1) + (y - y1)*(y - y1) <= (dis*dis)){ map[i] = true; path.push(crocodile[i]); } } } } } return false; }
Saving James Bond - Easy Version (25)
标签:
原文地址:http://www.cnblogs.com/zhouyiji/p/4564816.html