topcoder srm 550

div1 250pt:

  题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面积是多少。

  解法:先随便设定一个起点,然后模拟机器人走的路线,先确定出来运动的大致范围,然后判断运动轨迹是否合法,也就是出了最后一步可以手动终止之外,看其他的时候,它转变方向是不是合法。。。。

  1 // BEGIN CUT HERE
  2 
  3 // END CUT HERE
  4 #line 5 "RotatingBot.cpp"
  5 #include<cstdio>
  6 #include<sstream>
  7 #include<cstring>
  8 #include<cstdlib>
  9 #include<ctime>
 10 #include<cmath>
 11 #include<cassert>
 12 #include<iostream>
 13 #include<string>
 14 #include<vector>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<stack>
 19 #include<algorithm>
 20 using namespace std;
 21 typedef long long ll;
 22 typedef pair<int,int> pii;
 23 int vis[1000][1000];
 24 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 25 class RotatingBot
 26 {
 27     public:
 28     int minArea(vector <int> moves){
 29     //$CARETPOSITION$
 30         memset(vis,-1,sizeof(vis));
 31         int cur_d = 0;
 32         int cur_x = 500,cur_y = 500;
 33         vis[cur_x][cur_y] = 0;
 34         for(int i = 0;i < moves.size();i++){
 35             int len = moves[i];
 36             while(len > 0){
 37                 cur_x += dir[cur_d][0];
 38                 cur_y += dir[cur_d][1];
 39                 if(vis[cur_x][cur_y] != -1)return -1;
 40                 vis[cur_x][cur_y] = i;
 41                 len --;
 42             }
 43             cur_d = (cur_d + 1) % 4;
 44         }
 45      //   cout << "done" <<endl;
 46         int left = 999,right = 0,top = 0,bottom = 999;
 47         for(int i = 0;i < 1000;i++)
 48             for(int j = 0;j < 1000;j++){
 49                 if(vis[i][j] != -1){
 50                     left = min(left,i);
 51                     right = max(right,i);
 52                     top = max(top,j);
 53                     bottom = min(bottom,j);
 54                 }
 55             }
 56     //    cout <<"left: "<<left<<" right: "<<right <<" top: "<<top<<" bottom:"<<bottom<<endl;
 57         cur_x = 500,cur_y = 500;cur_d = 0;
 58         for(int i = 0;i < moves.size();i++){
 59             int len = moves[i];
 60             while(len > 0){
 61                 cur_x += dir[cur_d][0];
 62                 cur_y += dir[cur_d][1];
 63                 len --;
 64             }
 65             if(i == moves.size() - 1)continue;
 66             int next_x = cur_x + dir[cur_d][0];
 67             int next_y = cur_y + dir[cur_d][1];
 68             if(next_x >= left && next_x <= right && next_y >= bottom && next_y <= top){
 69                 if(vis[next_x][next_y] == -1)return -1;
 70                 if(vis[next_x][next_y] > i)return -1;
 71             }
 72             cur_d = (cur_d + 1) % 4;
 73         }
 74         return (right - left + 1) * (top - bottom + 1);
 75 
 76     }
 77 
 78 // BEGIN CUT HERE
 79     public:
 80     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(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); if ((Case == -1) || (Case == 7)) test_case_7(); }
 81     private:
 82     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(); }
 83     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; } }
 84     void test_case_0() { int Arr0[] = {15}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 16; verify_case(0, Arg1, minArea(Arg0)); }
 85     void test_case_1() { int Arr0[] = {3,10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 44; verify_case(1, Arg1, minArea(Arg0)); }
 86     void test_case_2() { int Arr0[] = {1,1,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = -1; verify_case(2, Arg1, minArea(Arg0)); }
 87     void test_case_3() { int Arr0[] = {9,5,11,10,11,4,10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 132; verify_case(3, Arg1, minArea(Arg0)); }
 88     void test_case_4() { int Arr0[] = {12,1,27,14,27,12,26,11,25,10,24,9,23,8,22,7,21,6,20,5,19,4,18,3,17,2,16,1,15}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 420; verify_case(4, Arg1, minArea(Arg0)); }
 89     void test_case_5() { int Arr0[] = {8,6,6,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = -1; verify_case(5, Arg1, minArea(Arg0)); }
 90     void test_case_6() { int Arr0[] = {8,6,6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 63; verify_case(6, Arg1, minArea(Arg0)); }
 91     void test_case_7() { int Arr0[] = {5,4,5,3,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 30; verify_case(7, Arg1, minArea(Arg0)); }
 92 
 93 // END CUT HERE
 94 
 95 };
 96 // BEGIN CUT HERE
 97 int main(){
 98     RotatingBot ___test;
 99     ___test.run_test(-1);
100     return 0;
101 }
102 // END CUT HERE
250pt

 

div1 500pt:

  题意:两个人在一个无限大的格子里涂色,第一个人先在(0,0)涂色,然后接下来的每一轮,对于所有的点(x,y),如果(x-1,y-1)和(x-2,y)中有且只有一个点被涂色了,那么就把这个点也涂了,问经过t轮之后,某个区域的染色情况。

  解法:先在纸上模拟一下前几轮的情况,我们很容易发现,被涂色的区域就是{x>=0,y>=0并且x<=y}这部分区域,而且被涂色点的分布很像一个杨辉三角,其中被涂色的部分就是杨辉三角中奇数的点。。。于是问题就变成判断C(n,m)奇偶性的问题了,有个结论C(n,m)是奇数当且仅当n&m==n。。。

 1 // BEGIN CUT HERE
 2 
 3 // END CUT HERE
 4 #line 5 "CheckerExpansion.cpp"
 5 #include<cstdio>
 6 #include<sstream>
 7 #include<cstring>
 8 #include<cstdlib>
 9 #include<ctime>
10 #include<cmath>
11 #include<cassert>
12 #include<iostream>
13 #include<string>
14 #include<vector>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<stack>
19 #include<algorithm>
20 using namespace std;
21 typedef long long ll;
22 typedef pair<int,int> pii;
23 class CheckerExpansion
24 {
25     public:
26     vector <string> resultAfter(long long t, long long x0, long long y0, int w, int h){
27     //$CARETPOSITION$
28         vector<string> ret;
29       //  t = 5;x0 = 0;y0 = 0;w = 10;h = 10;
30         for(int i = 0;i < h;i++){
31             string tmp = "";
32             for(int j = 0;j < w;j++){
33                 char will;
34                 long long x = x0 + j;
35                 long long y = y0 + h - i - 1;
36         //        if(x == y)cout <<"x : " <<x<<" y:"<<y<<endl;
37                 if((x + y) % 2 == 0){
38                     if(x < y){
39                         will = '.';
40                     }else{
41                         long long row = (x + y) / 2;
42                         long long column = abs(x - row);
43                         if(row + 1> t){
44                             will = '.';
45                         }else if((row & column) == column){
46                             if(row % 2 == 0){
47                                 will = 'A';
48                             }else{
49                                 will = 'B';
50                             }
51                         }else{
52                             will = '.';
53                         }
54                     }
55                 }else{
56                     will = '.';
57                 }
58         //        if(x == y)cout<<will<<endl;
59                 tmp += will;
60             }
61             ret.push_back(tmp);
62         //    cout << tmp << endl;
63 
64         }
65         return ret;
66 
67 
68     }
69 
70 // BEGIN CUT HERE
71     public:
72     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(); }
73     private:
74     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(); }
75     void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
76     void test_case_0() { long long Arg0 = 1LL; long long Arg1 = 0LL; long long Arg2 = 0LL; int Arg3 = 4; int Arg4 = 4; string Arr5[] = {"....", "....", "....", "A..." }; vector <string> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[0]))); verify_case(0, Arg5, resultAfter(Arg0, Arg1, Arg2, Arg3, Arg4)); }
77     void test_case_1() { long long Arg0 = 5LL; long long Arg1 = 4LL; long long Arg2 = 1LL; int Arg3 = 3; int Arg4 = 4; string Arr5[] = {"A..", "...", "B..", ".B." }; vector <string> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[0]))); verify_case(1, Arg5, resultAfter(Arg0, Arg1, Arg2, Arg3, Arg4)); }
78     void test_case_2() { long long Arg0 = 1024LL; long long Arg1 = 1525LL; long long Arg2 = 512LL; int Arg3 = 20; int Arg4 = 2; string Arr5[] = {"B...B...B...........", ".B.A.B.A.B.........." }; vector <string> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[0]))); verify_case(2, Arg5, resultAfter(Arg0, Arg1, Arg2, Arg3, Arg4)); }
79     void test_case_3() { long long Arg0 = 53LL; long long Arg1 = 85LL; long long Arg2 = 6LL; int Arg3 = 5; int Arg4 = 14; string Arr5[] = {".....", ".....", "B....", ".B.A.", ".....", ".....", ".....", ".....", ".....", ".....", "B....", ".B...", "..B..", ".A.B." }; vector <string> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[0]))); verify_case(3, Arg5, resultAfter(Arg0, Arg1, Arg2, Arg3, Arg4)); }
80 
81 // END CUT HERE
82 
83 };
84 // BEGIN CUT HERE
85 int main(){
86     CheckerExpansion ___test;
87     ___test.run_test(-1);
88     return 0;
89 }
90 // END CUT HERE
500pt

 

 

posted @ 2014-03-05 15:22  silver__bullet  阅读(262)  评论(0编辑  收藏  举报