|
| 1 | +#include <vector> |
| 2 | +#include <iostream> |
| 3 | +#include <algorithm> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | + |
| 7 | +// you can also use includes, for example: |
| 8 | +// #include <algorithm> |
| 9 | +#define ui unsigned int |
| 10 | +#define inside(x,L,R) (L<x&&x<R) |
| 11 | +const int dir[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}}; |
| 12 | +inline bool cross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){ |
| 13 | + if (x1 > x2) swap(x1, x2); |
| 14 | + if (y1 > y2) swap(y1, y2); |
| 15 | + if (x3 > x4) swap(x3, x4); |
| 16 | + if (y3 > y4) swap(y3, y4); |
| 17 | + |
| 18 | + if (x1 == x2){ |
| 19 | + if (x3 == x4){ |
| 20 | + if (x1 == x3){ |
| 21 | + return !(y3 >= y2 || y1 >= y4); |
| 22 | + }else return false; |
| 23 | + }else{ |
| 24 | + return inside(y3,y1,y2) && inside(x1,x3,x4); |
| 25 | + } |
| 26 | + }else{ |
| 27 | + if (x3 == x4){ |
| 28 | + return inside(x3,x1,x2) && inside(y1,y3,y4); |
| 29 | + }else{ |
| 30 | + if (y1 == y3){ |
| 31 | + return !(x3 >= x2 || x1 >= x4); |
| 32 | + }else return false; |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +int solution(const vector<int> &A) { |
| 37 | + int x1 = 0, y1 = 0, x2, y2, d = 0; |
| 38 | + for (ui i = 0; i < A.size(); i++){ |
| 39 | + |
| 40 | + |
| 41 | + x2 = x1 + dir[d][0] * A[i]; |
| 42 | + y2 = y1 + dir[d][1] * A[i]; |
| 43 | + |
| 44 | + |
| 45 | + int x3 = 0, y3 = 0, x4, y4, d1 = 0; |
| 46 | + for (ui j = 0; j < i; j++){ |
| 47 | + x4 = x3 + dir[d1][0] * A[j]; |
| 48 | + y4 = y3 + dir[d1][1] * A[j]; |
| 49 | + if (cross(x1, y1, x2, y2, x3, y3, x4, y4)) return i+1; |
| 50 | + if (++d1 == 4) d1 = 0; |
| 51 | + x3 = x4; |
| 52 | + y3 = y4; |
| 53 | + } |
| 54 | + if (++d == 4) d = 0; |
| 55 | + x1 = x2; |
| 56 | + y1 = y2; |
| 57 | + } |
| 58 | + return -1; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +int main(){ |
| 64 | + int a[] = {1,3,2,5,4,4,6,3,2}; |
| 65 | + vector<int> A(a, a+sizeof(a)/4); |
| 66 | + cout << solution(A) << endl; |
| 67 | +} |
0 commit comments