Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //in addition to checking whether the cell is an obstacle, it adds the cell
- //to the list of obstacles if it is not (it would immediately become a
- //traveled cell, thus equiv. to an obstacle)
- char isOb (int x, int y, int ob[1000][2], int *nOb) {
- int i;
- for (i = 0; i < *nOb; i++) {
- if (x == ob[i][0] && y == ob[i][1]) {
- return 1; //return true if the position being checked is in the array
- }
- }
- //add the position to the array otherwise
- ob[*nOb][0] = x;
- ob[*nOb][1] = y;
- (*nOb)++;
- return 0; //return false if the position is not an obstacle
- }
- int main () {
- int m, n, x, y, nOb; //nOb is the number of obstacles
- int *nObp = &nOb; //using a pointer for number of obstacles so that the function can modify it as well
- int ob[1000][2]; //array of obstacles (ob[i][0], ob[i][1]) = (xi, yi)
- int i;
- char cantMove;
- scanf("%d %d", &m, &n);
- scanf("%d %d", &x, &y);
- ob[0][0] = x;
- ob[0][1] = y;
- scanf("%d", &nOb);
- nOb++; //one row reserved for initial position
- for (i = 1; i < nOb; i++)
- scanf("%d %d", &ob[i][0], &ob[i][1]);
- do {
- cantMove = 1; //assume that the robot is stuck and then check for possible moves
- //check if the robot can move in a particular direction, if so, move and set cantMove to false
- if (!isOb(x, ((y+1)%n), ob, nObp)) { //the mod stuff is for handling wrapping
- y += 1;
- cantMove = 0;
- }
- else if (!isOb(((x+1)%m), y, ob, nObp)) {
- x += 1;
- cantMove = 0;
- }
- else if (!isOb(x, ((y+n-1)%n), ob, nObp)) {
- y -= 1;
- cantMove = 0;
- }
- else if (!isOb(((x+m-1)%m), y, ob, nObp)) {
- x -= 1;
- cantMove = 0;
- }
- //correcting for overflow
- if (x == m)
- x = 0;
- else if (y == n)
- y = 0;
- else if (x == (0-1))
- x = m-1;
- else if (y == (0-1))
- y = n-1;
- } while (cantMove == 0); //if the robot has moved this time, go through the process again
- printf("%d %d\n", x, y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement