SRM 507(2-1000pt)

DIV2 1000pt

题意:在一个长度无限的数轴上移动一个方块,每次可以向左或者向右移动距离x,只要x为完全平方数。数轴上有一些坑,如果方块移动到坑上则方块会掉进坑中,不能再被移动。给整数s,e,和所有坑的位置hol[i],求最少多少步能够将方块从s点移动到e点,若不能从s移动到e,则返回-1。1 <= s, e, hol[i]  <= 100000,且他们互不相同。

   注意,若s = 1, e = 5, hol[0] = 3,则答案为-1。

解法:首先,从从s移动到e和从e移动到s的需要的步数是一样的,所以可以不妨设s < e。

   分为三种情况:存在一个i使得s < hol[i] < e,则答案一定为-1。

   若存在一个hol[i] < s和一个hol[j] > e,则将无限的数轴变为有限的,且1 <= hol[i] <= 100000,所以只需要一个建图用BFS做即可。

   

   然后先证明, 如果不存在hol[i] < s存在hol[j] > e,和不存在hol[i] < s且不存在hol[j] > e是等价的情况。这个很容易想明白,比如,从10转化到21,可以10 -> -15 -> 21,也可以10 ->46 -> 21。

   所以,剩下最后一种情况等价于,数轴上没有坑。设d = e - s,则无论如何,三步之内一定能从s走到e。

   若d为完全平方数,一步即可。

   若d = x*x + y*y,则两步即可。(枚举判断)

   若d为奇数,则d = 1 * d = (a+b) * (a-b),即a = (d+1)/2,b = (d-1)/2,则a*a - b*b = d,两步即可。

   若d为偶数。则如果d为4的倍数,同样可以求出a和b使得d=a*a-b*b,两步即可。若d不为4的倍数,则d = 1 + (d-1),三步即可。

   最后这种数轴上没有坑的情况,不能仅通过扩大有限数轴的范围然后用BFS的方法来做,因为范围会大到超时,而且也不容易判断具体范围要扩大到多大。

tag:math, BFS, good

  1 // BEGIN CUT HERE
  2 /*
  3  * Author:  plum rain
  4  * score :
  5  */
  6 /*
  7 
  8  */
  9 // END CUT HERE
 10 #line 11 "CubeRoll.cpp"
 11 #include <sstream>
 12 #include <stdexcept>
 13 #include <functional>
 14 #include <iomanip>
 15 #include <numeric>
 16 #include <fstream>
 17 #include <cctype>
 18 #include <iostream>
 19 #include <cstdio>
 20 #include <vector>
 21 #include <cstring>
 22 #include <cmath>
 23 #include <algorithm>
 24 #include <cstdlib>
 25 #include <set>
 26 #include <queue>
 27 #include <bitset>
 28 #include <list>
 29 #include <string>
 30 #include <utility>
 31 #include <map>
 32 #include <ctime>
 33 #include <stack>
 34 
 35 using namespace std;
 36 
 37 #define CLR(x) memset(x, 0, sizeof(x))
 38 #define CLR1(x) memset(x, -1, sizeof(x))
 39 #define PB push_back
 40 #define SZ(v) ((int)(v).size())
 41 #define zero(x) (((x)>0?(x):-(x))<eps)
 42 #define out(x) cout<<#x<<":"<<(x)<<endl
 43 #define tst(a) cout<<#a<<endl
 44 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 45 
 46 typedef vector<int> VI;
 47 typedef vector<string> VS;
 48 typedef vector<double> VD;
 49 typedef long long int64;
 50 typedef pair<int, int> pii;
 51 
 52 const double eps = 1e-8;
 53 const double PI = atan(1.0)*4;
 54 const int maxint = 2139062143;
 55 const int maxx = 320;
 56 
 57 bool v[100005];
 58 pii an[100005];
 59 
 60 bool ok(int a)
 61 {
 62     int x = sqrt(a + 0.0);
 63     if ((x-1)*(x-1) == a) return 1;
 64     if (x * x == a) return 1;
 65     if ((x+1)*(x+1) == a) return 1;
 66     return 0;
 67 }
 68 
 69 int BFS (int l, int r, int s, int e)
 70 {
 71     CLR (v);
 72     v[s] = 1;
 73     int il = 0, ir = 0;
 74     pii t; t.second = 1;
 75     for (int i = 1; i < maxx; ++ i){
 76         if (s + i*i < r){
 77             v[s+i*i] = 1;
 78             t.first = s + i*i; an[ir++] = t;
 79             continue;
 80         }
 81         else break;
 82     }
 83     for (int i = 1; i < maxx; ++ i){
 84         if (s - i*i > l){
 85             v[s - i*i] = 1;
 86             t.first = s - i*i; an[ir++] = t;
 87         }
 88         else break;
 89     }
 90 
 91     while (il < ir){
 92         pii tmp = an[il++];
 93         if (tmp.first == e) return tmp.second;
 94 
 95         pii t1 = tmp; ++ t1.second;
 96         for (int i = 1; i < maxx; ++ i){
 97             if (!v[tmp.first+i*i] && tmp.first + i*i < r){
 98                 t1.first = tmp.first + i*i; an[ir++] = t1;
 99                 v[t1.first] = 1;
100             }
101             if (tmp.first + i*i >= r) break;
102         }
103         for (int i = 1; i < maxx; ++ i){
104             if (!v[tmp.first-i*i] && tmp.first - i*i > l){
105                 t1.first = tmp.first - i*i; an[ir++] = t1;
106                 v[t1.first] = 1;
107             }
108             if (tmp.first - i*i <= l) break;
109         }
110     }
111     return -1;
112 }
113 
114 int gao(int d)
115 {
116     for (int i = 1; i < maxx; ++ i)
117         for (int j = 1; j < maxx; ++ j)
118             if (i*i + j*j == d) return 2;
119     return 3;
120 }
121 
122 class CubeRoll
123 {
124     public:
125         int getMinimumSteps(int s, int e, vector <int> hol){
126             if (s > e) swap(s, e);
127             bool tt = 0;
128             int n = hol.size(), l = 0, r = 100005;
129             for (int i = 0; i < n; ++ i){
130                 if (hol[i] < s) l = max(l, hol[i]);
131                 if (hol[i] > e) r = min(r, hol[i]);
132                 if (s < hol[i] && hol[i] < e) tt = 1;
133             }
134             
135             if (tt) return -1;
136             if (l && r != 100005) return BFS(l, r, s, e);
137 
138             int d = e - s;
139             if (ok(d)) return 1;
140             if (d & 1 || !(d % 4)) return 2;
141             return gao(d);
142         }
143         
144 // BEGIN CUT HERE
145     public:
146     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
147     private:
148     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
149     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
150     void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arr2[] = {3}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = -1; verify_case(0, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
151     void test_case_1() { int Arg0 = 36; int Arg1 = 72; int Arr2[] = {300, 100, 200, 400}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; verify_case(1, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
152     void test_case_2() { int Arg0 = 10; int Arg1 = 21; int Arr2[] = {38,45}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(2, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
153     void test_case_3() { int Arg0 = 98765; int Arg1 = 4963; int Arr2[] = {10,20,40,30}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(3, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
154     void test_case_4() { int Arg0 = 68332; int Arg1 = 825; int Arr2[] = {99726,371,67,89210}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(4, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
155 
156 // END CUT HERE
157 
158 };
159 
160 // BEGIN CUT HERE
161 int main()
162 {
163 //    freopen( "a.out" , "w" , stdout );    
164     CubeRoll ___test;
165     ___test.run_test(-1);
166        return 0;
167 }
168 // END CUT HERE
View Code

 

 

 

 

 

 

posted @ 2013-12-13 12:26  Plumrain  阅读(221)  评论(0编辑  收藏  举报