Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MAXN 205
  6. int n, ex, ey;
  7.  
  8. struct str
  9. {
  10.     int x, y;
  11.     string s;
  12. } arr[MAXN];
  13.  
  14. int cnt[MAXN];
  15.  
  16. bool ok(){
  17.     int x = 0, y = 0;
  18.     string dir = "R";
  19.     while (1){
  20.         int tx = INT_MAX, ty = INT_MAX;
  21.         int ind = 0;
  22.         string t = "";
  23.         if (x == ex && y == ey) return 1;
  24.         for (int i=1; i<=n + 1; i++){
  25.             if (dir == "R"){
  26.                 if (arr[i].x == x && arr[i].x < tx){
  27.                     tx = arr[i].x;
  28.                     ty = arr[i].y;
  29.                     ind = i;
  30.                     if (arr[i].s == "/"){
  31.                         t = "U";
  32.                     }
  33.                     else {
  34.                         t = "D";
  35.                     }
  36.                 }
  37.             }
  38.             else if (dir == "L"){
  39.                 if (arr[i].x == x && arr[i].x < tx){
  40.                     tx = arr[i].x;
  41.                     ty = arr[i].y;
  42.                     ind = i;
  43.                     if (arr[i].s == "/"){
  44.                         t = "D";
  45.                     }
  46.                     else{
  47.                         t = "U";
  48.                     }
  49.                 }
  50.             }
  51.             else if (dir == "U"){
  52.                 if (arr[i].y == y && arr[i].y < ty){
  53.                     tx = arr[i].x;
  54.                     ty = arr[i].y;
  55.                     ind = i;
  56.                     if (arr[i].s == "/"){
  57.                         t = "R";
  58.                     }
  59.                     else{
  60.                         t = "L";
  61.                     }
  62.                 }
  63.             }
  64.             else{
  65.                 if (arr[i].y == y && arr[i].y > ty){
  66.                     tx = arr[i].x;
  67.                     ty = arr[i].y;
  68.                     ind = i;
  69.                     if (arr[i].s == "/"){
  70.                         t = "L";
  71.                     }
  72.                     else {
  73.                         t = "R";
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         cnt[ind]++;
  79.         if (!ind ||cnt[ind] > 2) return 0;
  80.         dir = t;
  81.         if (t == "R"){
  82.             x = tx - 1;
  83.             y = ty;
  84.         }
  85.         else if (t == "L"){
  86.             x = tx - 1;
  87.             y = ty;
  88.         }
  89.         else if (t == "U"){
  90.             x = tx;
  91.             y = ty + 1;
  92.         }
  93.         else{
  94.             x = tx;
  95.             y = ty - 1;
  96.         }
  97.     }
  98. }
  99.  
  100. int main()
  101. {
  102.  
  103.     cin >> n >> ex >> ey;
  104.     for (int i=1; i<=n; i++){
  105.         cin >> arr[i].x >> arr[i].y >> arr[i].s;
  106.  
  107.     }
  108.     arr[n+1].x = ex;
  109.     arr[n+1].y = ey;
  110.     arr[n+1].s = "/";
  111.     if (ok()){
  112.         cout << 0 << "\n";
  113.         return 0;
  114.     }
  115.     for (int i=1; i<=n; i++){
  116.         string temp = arr[i].s;
  117.         if (arr[i].s == "/") arr[i].s = "\\";
  118.         else arr[i].s = "/";
  119.         memset(cnt, 0, sizeof cnt);
  120.         if (ok()){
  121.             cout << i << "\n";
  122.             return 0;
  123.         }
  124.         swap(temp, arr[i].s);
  125.     }
  126.     cout << -1 << "\n";
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement