Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Monday, November 12, 2012

uva 118 - Mutant Flatworld Explorers


/*
Problem link
Algorithm:
 - Just go and take note of the grid where the robots fall.
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = 5000;
//----------------------------
struct robot {
 int x;
 int y;
 int cur;
};
//----------------------------
bool isok[60][60];
robot bob;
int cx,cy;
//----------------------------
bool isin(int x, int y) {
 if (x>=0 && y>=0 && x<=cx && y<=cy) return(true);
 return(false);
}
//----------------------------
void turn(int& cur, char dir) {
 if (dir=='L') 
  if (cur>1) cur = cur-1;
  else cur = 4;
 
 if (dir=='R')
  if (cur<4) cur = cur+1;
  else cur = 1;
}
//----------------------------
bool go(robot& bob, char dir) {
 if (dir!='F') turn(bob.cur,dir);
 else
 {
  switch (bob.cur)
  {
   case 1: {
    if (isok[bob.x][bob.y]) bob.y++;
    else if (isin(bob.x,bob.y+1)) bob.y++;
    if (!isin(bob.x,bob.y)) 
    {
     isok[bob.x][--bob.y] = false;
     return(false);
    }
    break;
   }
   case 2: {
    if (isok[bob.x][bob.y]) bob.x++;
    else if (isin(bob.x+1,bob.y)) bob.x++;
    if (!isin(bob.x,bob.y)) 
    {
     isok[--bob.x][bob.y] = false;
     return(false);
    }
    break;
   }
   case 3: {
    if (isok[bob.x][bob.y]) bob.y--;
    else if (isin(bob.x,bob.y-1)) bob.y--;
    if (!isin(bob.x,bob.y)) 
    {
     isok[bob.x][++bob.y] = false;
     return(false);
    }
    break;
   }
   case 4: {
    if (isok[bob.x][bob.y]) bob.x--;
    else if (isin(bob.x-1,bob.y)) bob.x--;
    if (!isin(bob.x,bob.y)) 
    {
     isok[++bob.x][bob.y] = false;
     return(false);
    }
    break;
   }
  }
 }
 return(true);
}
//----------------------------
int main() {
 memset(isok,true,sizeof(isok));
 cin >> cx >> cy;
 while (!cin.eof()) 
 {
  char c;
  string line;
  cin >> bob.x >> bob.y >> c;
  if (cin.eof()) break;
  switch (c)
  {
   case 'N': {
    bob.cur = 1;
    break;
   }
   case 'E': {
    bob.cur = 2;
    break;
   }
   case 'S': {
    bob.cur = 3;
    break;
   }
   case 'W': {
    bob.cur = 4;
    break;
   }
  }
  getline(cin,line);
  getline(cin,line);
  int i;
  for (i=0; i<line.length() && go(bob,line[i]); i++);
  char dir;
  switch (bob.cur)
  {
   case 1: {
    dir = 'N';
    break;
   }
   case 2: {
    dir = 'E';
    break;
   }
   case 3: {
    dir = 'S';
    break;
   }
   case 4: {
    dir = 'W';
    break;
   }
  }
  cout << bob.x << " " << bob.y << " " << dir;
  if (i<line.length()) cout << " LOST" << endl;
  else cout << endl;
 }
 return 0;
}
//----------------------------

No comments:

Post a Comment