Uva-118-Mutant Flatworld Explorers

来源:互联网 发布:淘宝二手雷蛇耳麦 编辑:程序博客网 时间:2024/05/21 11:19

简单的模拟题,就是给你一系列指令,然后进行模拟操作。

代码:

#include<cstdio>#include<cstring>#include<iostream>#define MAX 10001bool lost[MAX][MAX];int n,m,sx,sy,pos;char s[4];int movey[4]={1,0,-1,0},movex[4]={0,1,0,-1};int GetPos(char op){    switch(op)    {case 'N':return 0;case 'E':return 1;case 'S':return 2;case 'W': return 3;    }}bool islost(int x,int y){    if(x<0||y<0||x>n||y>m)return true;    return false;}char GetS(int pos){    switch(pos)    {case 0:return 'N';case 1:return 'E';case 2:return 'S';case 3:return 'W';    }}int main(){    scanf("%d%d",&n,&m);    memset(lost,0,sizeof(lost));    while(scanf("%d%d%s",&sx,&sy,&s)!=EOF)    {pos=GetPos(s[0]);char str[MAX];scanf("%s",str);int flag=0;for(int i=0;i<strlen(str);i++){    if(str[i]=='F')    {if(islost(sx+movex[pos],sy+movey[pos])){    if(lost[sx][sy])continue;    else    {lost[sx][sy]=1;flag=1;break;    }}sx+=movex[pos];sy+=movey[pos];    }    else if(str[i]=='R')pos=(pos+4+1)%4;    elsepos=(pos+4-1)%4;}printf("%d %d",sx,sy);printf(" %c",GetS(pos));if(flag)    printf(" LOST");printf("\n");    }    return 0;}


原创粉丝点击