首页 > 其他 > 详细

Topcoder SRM646 div1 600 bfs+剪枝

时间:2015-01-17 08:48:10      阅读:574      评论:0      收藏:0      [点我收藏+]

因为只有47个blocks,所以现在小范围内,即在-50 <= x <= 50,-50 <= y <= 50内进行bfs,之后尽量让点向右走,记录最大值。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime>
#include <iomanip>

#pragma comment(linker,"/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007

using namespace std;

struct P
{
    int x,y,k;
};

queue<P> q;

map<pair<int,int>,int> M;

class TheGridDivOne
{
public :
    int find(vector <int> x, vector <int> y, int k)
    {
        q.push((P){0,0,0});

        M.insert( pair<pair<int,int>,int> (pair<int,int>(0,0),0));

        P f,s;

        int i , n;
        n = x.size();

        int Max = 0;

        map<pair<int,int>,int>::iterator it;

        while(q.empty() == false)
        {
            f = q.front();
            q.pop();

            if(k-f.k+f.x <= Max)
                continue;

            Max = max(Max,f.x);

            it = M.find(pair<int,int>(f.x,f.y));

      

            if(it->second < f.k)
                continue;

            int U = Mod,D = -Mod,L = -Mod,R = Mod;
            s = f;
            for(i = 0; i < n; ++i)
            {
                if(x[i] == s.x && y[i] > s.y)
                    U = min(U,y[i]);
                if(x[i] == s.x && y[i] < s.y)
                    D = max(D,y[i]);

                if(y[i] == s.y && x[i] > s.x)
                    R = min(R,x[i]);
                if(y[i] == s.y && x[i] < s.x)
                    L = max(L,x[i]);
            }

            s.y = f.y,s.x = min(f.x+k-f.k,R-1),s.k = f.k+s.x-f.x;

            it = M.find(pair<int,int>(s.x,s.y));

            if(it == M.end())
            {
                q.push(s);
                M.insert( pair<pair<int,int>,int> (pair<int,int>(s.x,s.y),s.k));
            }
            else if(it->second > s.k)
            {
                q.push(s);
                it->second = s.k;
            }



            for(i = f.x-1; i > max(f.x-2,L); --i)
            {
                s.x = i,s.y = f.y,s.k = f.k + f.x-i;

                it = M.find( pair<int,int>(s.x,s.y) );

                if(s.k > k)
                    break;

                if(it == M.end())
                {
                    M.insert( pair<pair<int,int>,int> (pair<int,int>(s.x,s.y),s.k));
                    q.push(s);
                }
                else if(it->second > s.k)
                {
                    it->second = s.k;
                    q.push(s);
                }
            }

            for(i = f.y+1; i < min(f.y+2,U); ++i)
            {
                s.x = f.x,s.y = i,s.k = f.k + i-f.y;

                if(s.k > k)
                    break;

                it = M.find( pair<int,int>(s.x,s.y));

                if(it == M.end())
                {
                    M.insert( pair<pair<int,int>,int> (pair<int,int>(s.x,s.y),s.k));
                    q.push(s);
                }
                else if(it->second > s.k)
                {
                    it->second = s.k;
                    q.push(s);
                }
            }

            for(i = f.y-1; i > max(f.y-2,D); --i)
            {
                s.x = f.x,s.y = i,s.k = f.k + f.y-i;

                if(s.k > k)
                    break;

                it = M.find( pair<int,int>(s.x,s.y));

                if(it == M.end())
                {
                    M.insert( pair<pair<int,int>,int> (pair<int,int>(s.x,s.y),s.k));
                    q.push(s);
                }
                else if(it->second > s.k)
                {
                    it->second = s.k;
                    q.push(s);
                }
            }

            if(f.k <= 50)
            {
                for(i = f.x+1; i < min(f.x+2,R); ++i)
                {
                    s.x = i,s.y = f.y,s.k = f.k + i-f.x;

                    it = M.find( pair<int,int>(s.x,s.y) );

                    if(s.k > k)
                        break;

                    if(it == M.end())
                    {
                        M.insert( pair<pair<int,int>,int> (pair<int,int>(s.x,s.y),s.k));
                        q.push(s);
                    }
                    else if(it->second > s.k)
                    {
                        it->second = s.k;
                        q.push(s);
                    }
                }
            }

        }
        return Max;
    }
};


Topcoder SRM646 div1 600 bfs+剪枝

原文:http://blog.csdn.net/zmx354/article/details/42803147

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!