Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //in addition to checking whether the cell is an obstacle, it adds the cell
  4. //to the list of obstacles if it is not (it would immediately become a
  5. //traveled cell, thus equiv. to an obstacle)
  6. char isOb (int x, int y, int ob[1000][2], int *nOb) {
  7.  
  8.     int i;
  9.     for (i = 0; i < *nOb; i++) {
  10.         if (x == ob[i][0] && y == ob[i][1]) {
  11.             return 1; //return true if the position being checked is in the array
  12.         }
  13.     }
  14.    
  15.     //add the position to the array otherwise
  16.     ob[*nOb][0] = x;
  17.     ob[*nOb][1] = y;
  18.     (*nOb)++;
  19.    
  20.     return 0; //return false if the position is not an obstacle
  21. }
  22.  
  23. int main () {
  24.    
  25.     int m, n, x, y, nOb; //nOb is the number of obstacles
  26.     int *nObp = &nOb; //using a pointer for number of obstacles so that the function can modify it as well
  27.     int ob[1000][2]; //array of obstacles (ob[i][0], ob[i][1]) = (xi, yi)
  28.     int i;
  29.     char cantMove;
  30.    
  31.     scanf("%d %d", &m, &n);
  32.     scanf("%d %d", &x, &y);
  33.     ob[0][0] = x;
  34.     ob[0][1] = y;
  35.     scanf("%d", &nOb);
  36.     nOb++; //one row reserved for initial position
  37.    
  38.     for (i = 1; i < nOb; i++)
  39.         scanf("%d %d", &ob[i][0], &ob[i][1]);
  40.  
  41.     do {
  42.         cantMove = 1; //assume that the robot is stuck and then check for possible moves
  43.        
  44.         //check if the robot can move in a particular direction, if so, move and set cantMove to false
  45.         if (!isOb(x, ((y+1)%n), ob, nObp)) { //the mod stuff is for handling wrapping
  46.             y += 1;
  47.             cantMove = 0;
  48.         }
  49.        
  50.         else if (!isOb(((x+1)%m), y, ob, nObp)) {
  51.             x += 1;
  52.             cantMove = 0;
  53.         }
  54.        
  55.         else if (!isOb(x, ((y+n-1)%n), ob, nObp)) {
  56.             y -= 1;
  57.             cantMove = 0;
  58.         }
  59.        
  60.         else if (!isOb(((x+m-1)%m), y, ob, nObp)) {
  61.             x -= 1;
  62.             cantMove = 0;
  63.         }
  64.  
  65.         //correcting for overflow
  66.         if (x == m)
  67.             x = 0;
  68.         else if (y == n)
  69.             y = 0;
  70.         else if (x == (0-1))
  71.             x = m-1;
  72.         else if (y == (0-1))
  73.             y = n-1;
  74.        
  75.     } while (cantMove == 0); //if the robot has moved this time, go through the process again
  76.    
  77.     printf("%d %d\n", x, y);
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement