Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- char turn(char x,char y){
- //recibe posicion actual del robot (N,S,E,W) y la instruccion (left o right) devuelve la nueva posicion
- switch(x){
- case 'E':
- if(y=='R'){
- return 'S';
- }else if(y=='L'){
- return 'N';
- }
- break;
- case 'W':
- if(y=='R'){
- return 'N';
- }else if(y=='L'){
- return 'S';
- }
- break;
- case 'N':
- if(y=='R'){
- return 'E';
- }else if(y=='L'){
- return 'L';
- }
- break;
- case 'S':
- if(y=='R'){
- return 'W';
- }else if(y=='L'){
- return 'E';
- }
- break;
- }
- }
- bool wasHere(char *lost,int x, int y){
- //busca las coordenadas dadas en el arreglo de posiciones de los perdidos
- for(int i=0; lost[i]!='\0'; i=i+2){
- if(lost[i]==x && lost[i+1]==y){
- return true;
- }
- }
- return false;
- }
- int main(int argc, char *argv[]) {
- int limx,limy,actx,acty, tope=0,isLost;
- char dir, aux;
- char mov[100], lost[100];
- scanf("%i%i",&limx,&limy); //recibe coordenadas de la esquina superior derecha
- while(scanf("%i%i%c",&actx,&acty,&dir)!=EOF){ //pide input mientras reciba: coordenada inicial en x ,y, posicion actual
- isLost=0;
- fflush(stdin);
- scanf("%s",mov); //lee instrucciones
- for(int i=0;mov[i]!='\0';i++){
- if(mov[i]=='R' || mov[i]=='L'){
- dir=turn(dir,mov[i]); //transforma la direccion
- }else if(mov[i]=='F'){ //si el movimiento es avanzar
- switch (dir){
- case 'N':
- if(acty+1 <0 || acty+1>limy ){ //si el sig. movimiento rebasa los limites esta perdido
- isLost=1;
- lost[tope++]=actx; //guarda coordenada x en arreglo
- lost[tope++]=acty; //guarda coordenada y en arreglo
- }
- if(!wasHere(lost,actx,acty)){ //valida: si la posicion actual no ha sido ocupada por un robot perdido, avanza
- acty++;
- }
- break;
- case 'S':
- if(acty-1 <0 || acty-1>limy ){
- isLost=1;
- lost[tope++]=actx;
- lost[tope++]=acty;
- }
- if(!wasHere(lost,actx,acty)){
- acty--;
- }
- break;
- case 'E':
- if(actx+1 <0 || actx+1>limy ){
- isLost=1;
- lost[tope++]=actx;
- lost[tope++]=acty;
- }
- if(!wasHere(lost,actx,acty)){
- actx++;
- }
- break;
- case 'W':
- if(actx-1 <0 || actx-1>limy ){
- isLost=1;
- lost[tope++]=actx;
- lost[tope++]=acty;
- }
- if(!wasHere(lost,actx,acty)){
- actx--;
- }
- break;
- }
- }
- }
- if(isLost){
- printf("%i %i %c LOST",actx,acty,dir);
- }else{
- printf("%i %i %c",actx,acty,dir);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement