PKU 1573 Robot Motion(简单模拟)

原题大意:原题链接

给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去)

此题虽然属于水题,但是完全独立完成而且直接1A还是很开心的

注意:对于形成环的情况则从进入环的交点处重新走一遍,记录步数即可

#include<cstdio>
#include<cstring>
int n,m,p;
bool vis[1010][1010];
char str[1010][1010];
bool can(int x,int y)
{
    if(x<1||x>n||y<1||y>m||vis[x][y])
        return false;
    return true;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&p),n|m|p){
        memset(vis,0,sizeof(vis));
        getchar();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)
                scanf("%c",&str[i][j]);
            getchar();
        }
        int x=1,y=p,step1=0;
        while(can(x,y)){
            int px=x,py=y;
            vis[px][py]=1;
            if(str[px][py]==‘E‘)
                {y++,step1++;}
            else if(str[px][py]==‘S‘)
                {x++,step1++;}
            else if(str[px][py]==‘W‘)
                {y--,step1++;}
            else if(str[x][y]==‘N‘)
                {x--,step1++;}
        }
        if(vis[x][y]){
            int step2=0,ox=x,oy=y;
            while(1){
                int px=x,py=y;
                if(str[px][py]==‘E‘)
                    {y++,step2++;}
                else if(str[px][py]==‘S‘)
                    {x++,step2++;}
                else if(str[px][py]==‘W‘)
                    {y--,step2++;}
                else if(str[px][py]==‘N‘)
                    {x--,step2++;}
                if(x==ox&&y==oy){
                    printf("%d step(s) before a loop of %d step(s)\n",step1-step2,step2);
                    break;
                }
            }
        }
        else printf("%d step(s) to exit\n",step1);
    }
} 
时间: 2024-08-02 14:48:29

PKU 1573 Robot Motion(简单模拟)的相关文章

POJ 1573 Robot Motion(模拟)

题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14195   Accepted: 6827 Description A robot has been programmed to follow the instructions in its path. Instr

HDU ACM 1035 Robot Motion 简单模拟题

分析:一步步的走,走出矩阵则说明没有环,若走到已经走过的地方,说明有环,按格式输出结果,OK. #include<iostream> using namespace std; #define N 15 int dir[4][2]={-1,0,1,0,0,-1,0,1}; char map[N][N]; int vis[N][N]; char ch[]="NSWE"; int n,m; int id(char c) { int i; for(i=0;i<4;i++) i

poj1573&amp;&amp;hdu1035 Robot Motion(模拟题)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1035 POJ:   http://poj.org/problem?id=1573 Description A robot has been programmed to follow the instructions in its path. Instructions for

HDOJ1035 Robot Motion 【模拟】

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6079    Accepted Submission(s): 2844 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

1573 Robot Motion

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10219   Accepted: 4977 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

Poj OpenJudge 百练 1573 Robot Motion

1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10856   Accepted: 5260 Description A robot has been programmed to follow the instru

POJ 1573 Robot Motion【是搜索,就不要纠结是DFS还是BFS】

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12845   Accepted: 6234 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

POJ 1573 Robot Motion 搜索

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6297    Accepted Submission(s): 2952 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

HDOJ 题目1035 Robot Motion(模拟)

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7372    Accepted Submission(s): 3389 Problem Description A robot has been programmed to follow the instructions in its path. Instruc