zcmu2138

2138: 我们的征途是星辰大海

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 29   Solved: 20
[ Submit][ Status][ Web Board]

Description

  最新的火星探测机器人curiosity被困在了一个二维迷宫里,迷宫由一个个方格组成。
共有四种方格:
‘.’ 代表空地,curiosity可以穿过它
‘#’ 代表障碍物,不可穿越,不可停留
‘S’ 代表curiosity的起始位置
‘T’ 代表curiosity的目的地
NASA将会发送一系列的命令给curiosity,格式如下:“LRUD”分别代表向左,向右,向上,向下走一步。由于地球和火星之间最近时也有55000000km!所以我们必须提前判断这一系列的指令会让curiosity最终处在什么样的状态,请编程完成它。

Input

       第一行是一个整数T,代表有几个测试样例
每个测试样例第一行是一个整数N(1<=N<=50))代表迷宫的大小(N*N)。随后的N行每行由N个字符串组成,代表迷宫。接下来的 一行是一个整数Q,代表有多少次询问,接下来的Q行每行是一个仅由“LRUD”四个字母的组成的字符串,字符转长度小于1000.

Output

  对于每个询问输出单独的一行:
“I get there!”:执行给出的命令后curiosity最终到达了终点。
“I have no idea!”:执行给出的命令后curiosity未能到达终点。
“I am dizzy!”:curiosity在执行命令的过程中撞到了障碍物。
“I am out!”:代表curiosity在执行命令的过程中走出了迷宫的边界。

Sample Input

2
2
S.
#T
2
RD
DR
3
S.#
.#.
.T#
3
RL
DDD
DDRR

Sample Output

I get there!
I am dizzy!
I have no idea!
I am out!I get there!

解析:模拟机器人走的路线,每一次判断一下。简单模拟题。

#include<bits/stdc++.h> 
using namespace std; 
char maze[55][55];
string op; 
int N, Q;
int solve(int sx, int sy, int tx, int ty)  
{
    if(sx==tx&&sy==ty)  
        return 0;  
    for(int i=0; i<(int)op.length(); ++i)  
    {  
        if(op[i]=='L')      sy -= 1;  
        else if(op[i]=='R') sy += 1;  
        else if(op[i]=='U') sx -= 1;  
        else                  sx += 1;  
   
        if(sx<0||sy<0||sx>=N||sy>=N) return 3;  
        if(maze[sx][sy]=='#')  return 2;  
        if(sx==tx&&sy==ty) return 0;  
    }  
    return 1;  
}
int main()  
{  
    int t;
    int sx,sy,tx,ty;
    string ans[4] = {"I get there!", "I have no idea!", "I am dizzy!", "I am out!"};
    cin >> t;
    while(t--)  
    {
        cin>>N;  
        for(int i=0; i<N; i++)  
            for(int j=0; j<N; j++)
            {  
                cin>>maze[i][j];  
                if(maze[i][j] == 'S')sx = i,sy = j;  
                if(maze[i][j] == 'T')tx = i,ty = j;  
            }
        cin>>Q;
        for(int i=0; i<Q; i++)
        {
            cin>>op;
            cout<<ans[solve(sx, sy, tx, ty)]<<endl;
        }  
    }
    return 0;  
} 

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/80483852
今日推荐