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

Sicily 1709:PropBot(BFS)

时间:2017-05-11 17:40:54      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:queue   ges   bsp   while   img   image   cin   nbsp   logs   

技术分享

 

#include<bits/stdc++.h>
using namespace std;
int TIME;
double X, Y;
double MIN;
double MIN_X, MIN_Y;
double SQRT = sqrt(0.5);
double DIR[8][2] = {{1,0}, {SQRT, -1*SQRT}, {0, -1}, {-1*SQRT, -1*SQRT}, 
                    {-1, 0}, {-1*SQRT, SQRT}, {0, 1}, {SQRT, SQRT}};
map<double, int> m[8];
struct Node{
    double x, y;
    int dir;
    int time;
}; 
double dist(Node n){
    return ((n.x - X)*(n.x - X) + (n.y - Y)*(n.y - Y));
}

void BFS(){
    Node n;
    n.x = n.y = 0;
    n.time = 0;
    n.dir = 0;
    queue<Node>q;
    if(n.time <= TIME){
        if(dist(n) < MIN){
            MIN = dist(n);
            MIN_X = n.x;
            MIN_Y = n.y;
        }
        q.push(n);
    } 
    
    while(!q.empty()){
        Node front = q.front();
        q.pop();
        double d = dist(front);
        Node t1 = front;
        t1.x += 10*DIR[t1.dir][0];
        t1.y += 10*DIR[t1.dir][1];
        t1.time++;
        if(t1.time <= TIME){
            if(dist(t1) < MIN){
                MIN = dist(t1);
                MIN_X = t1.x;
                MIN_Y = t1.y;
            }
            q.push(t1);
        }
        Node t2 = front;
        t2.dir = (++t2.dir) ;
        t2.time++;
        if(t2.time <= TIME && t2.dir < 8){
            q.push(t2);
        }
    }
}
int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> TIME >> X >> Y;
        MIN = 10000000;
        for(int i = 0; i < 8; i++){
            m[i].clear();
        }
        BFS();
        printf("%.6f\n", sqrt(MIN));
    }
}                        

 

Sicily 1709:PropBot(BFS)

标签:queue   ges   bsp   while   img   image   cin   nbsp   logs   

原文地址:http://www.cnblogs.com/Vincent-Bryan/p/6841871.html

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