keymasterviriya1150

UVA 11831

Dec 1st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<utility>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int  cheakDirection(int value){
  10.     if(value == 9)
  11.         value = 0; // N
  12.     else if(value == 8)
  13.         value = 1; // O
  14.     else if(value == 7)
  15.         value = 2; // S
  16.     else if(value == 6)
  17.         value = 3; // L
  18.     return  value;
  19. }
  20. pair<int,int> pointStart(int arr[100][100],int n ,int m){
  21.     pair<int,int> start;
  22.     for (int i = 0; i < n; i++) {
  23.         for (int j = 0; j < m; j++){
  24.             if( arr[i][j] ==  9 ||
  25.                 arr[i][j] ==  7 ||
  26.                 arr[i][j] ==  8 ||
  27.                 arr[i][j] ==  6)
  28.             start = make_pair(i,j);
  29.         }
  30.     }
  31.     return start;
  32. }
  33. int robot(char path[],int arr[100][100],int x,int y,int n,int m){
  34.     int dir = cheakDirection(arr[x][y]);
  35.     cout << "dir=" << dir << endl;
  36.     int count = 0;
  37.     for (int  i = 0; path[i]; i++) {
  38.         char move = path[i];
  39.  
  40.         if (move == 'D')
  41.           dir = (dir + 1) % 4;
  42.         else if (move == 'E')
  43.           dir = (4 + dir - 1) % 4;
  44.          
  45.         else if(move == 'F'){
  46.             // N
  47.              if (dir == 0){
  48.                  printf("S0\n");
  49.                  x--;
  50.                  if(x < 0)
  51.                     x = 0;
  52.                  if(arr[x][y] == 1){
  53.                     printf("if1\n");
  54.                     x++;
  55.                  }
  56.                  else if(arr[x][y] == 2){
  57.                      printf("if2\n");
  58.                      count++;
  59.                  }
  60.                    
  61.              }
  62.              // O
  63.              else if (dir == 1 ) {
  64.                  printf("S1\n");
  65.                  y++;
  66.                  if(y > m)
  67.                     y = m-1;
  68.                  if(arr[x][y] == 1){
  69.                     printf("if1\n");
  70.                     y--;                    
  71.                  }
  72.                    
  73.                  else if(arr[x][y] == 2){
  74.                     printf("if2\n");
  75.                     count++;  
  76.                  }
  77.              }
  78.              // S
  79.              else if (dir == 2 ){
  80.                  printf("S2\n");
  81.                  x++;
  82.                    if(x> n)
  83.                     x = n-1;
  84.                  if(arr[x][y] == 1){
  85.                     printf("if1\n");
  86.                     x--;  
  87.                  }
  88.                  else if(arr[x][y] == 2){
  89.                     printf("if2\n");
  90.                     count++;  
  91.                  }
  92.              }
  93.              // L
  94.              else if(dir == 3 ){
  95.                  printf("S3\n");
  96.                  y--;
  97.                  if(y < 0)
  98.                     y = 0;
  99.                  if(arr[x][y] == 1){
  100.                     printf("if1\n");
  101.                     y++;                
  102.                  }
  103.                  else if(arr[x][y] == 2){
  104.                     printf("if2\n");
  105.                     count++;  
  106.                  }
  107.              }
  108.          }
  109.          printf("x=%d y=%d count=%d\n",x,y,count);
  110.     }
  111.     return count;
  112. }
  113. void out(int arr[100][100],int n,int m){
  114.     for(int i=0;i<n;i++){
  115.         for(int j=0;j<m;j++){
  116.             cout << arr[i][j] <<" ";
  117.         }
  118.         cout << endl;
  119.     }
  120. }
  121. int main()
  122. {
  123.     int n,m,s;
  124.     char mes;
  125.     while(cin >> n >> m >> s ){
  126.        
  127.         if (n==0 && m==0 && s==0) break;
  128.        
  129.         int screen[100][100];
  130.        
  131.         for (int i = 0; i < n; i++) {
  132.             for (int j = 0; j < m; j++) {
  133.                 cin >> mes;
  134.                 if(mes == '.')
  135.                     screen[i][j] = 0;
  136.                 else if (mes == '#')
  137.                     screen[i][j] = 1;
  138.                 else if (mes == '*')
  139.                     screen[i][j] = 2;
  140.                    
  141.                 else if (mes == 'N')
  142.                     screen[i][j] = 9;
  143.                 else if (mes == 'S')
  144.                     screen[i][j] = 8;
  145.                 else if (mes == 'L')
  146.                     screen[i][j] = 7;
  147.                 else if (mes == 'O')
  148.                     screen[i][j] = 6;
  149.             }
  150.         }
  151.          out(screen,n,m);
  152.          char path[]="FDFFFFFFEEFFFFFFEFDF";
  153.          
  154.      
  155.          pair<int,int> p = pointStart(screen,n,m);
  156.          printf("start =%d %d",p.first,p.second);
  157.          cout <<"robot "<< robot(path,screen,p.first,p.second,n,m) << endl;
  158.          //out(screen,n,m);
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment