我们的征途是星辰大海 ( 蓝桥杯~算法提高 )

最新的火星探测机器人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

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


没什么说的,模拟加约束就行,注意每个样例都要初始化开始的位置


int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
int t,n,k,nx,ny,x,y;
char dp[50+5][50+5];
int judge(int r,int c)
{
    if(r<1 || r>n || c<1 || c>n)
    {
        cout<<"I am out!"<<endl;
        return 0;
    }
    else if(dp[r][c]=='#')
    {
        cout<<"I am dizzy!"<<endl;
        return 0;
    }
    else if(dp[r][c]=='T')
    {
        cout<<"I get there!"<<endl;
        return 0;
    }
    else if(dp[r][c]=='.')
        return 1;
}
int main()
{

    string str;
    cin>>t;
    for(int i=1; i<=t; i++)
    {
        int flag = 1;
        cin>>n;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                cin>>dp[i][j];
                if(dp[i][j]=='S')
                    x=i,y=j;
            }

        cin>>k;
        while(k--)
        {
            cin>>str;
            nx=x;
            ny=y;
            for(int i=0; i<str.size(); i++)
            {
                if(str[i]=='L')
                {
                    ny -= 1;
                    if( !judge(nx,ny) )
                        break;
                    else
                    {
                        if(i==str.size()-1)
                            cout<<"I have no idea!"<<endl;
                        else
                            continue;
                    }
                }
                else if(str[i]=='R')
                {
                    ny += 1;
                    if( !judge(nx,ny) )
                        break;
                    else
                    {
                        if(i==str.size()-1)
                            cout<<"I have no idea!"<<endl;
                        else
                            continue;
                    }
                }
                else if(str[i]=='U')
                {
                    nx -= 1;
                    if( !judge(nx,ny) )
                        break;
                    else
                    {
                        if(i==str.size()-1)
                            cout<<"I have no idea!"<<endl;
                        else
                            continue;
                    }
                }
                else if(str[i]=='D')
                {
                    nx += 1;
                    if( !judge(nx,ny) )
                        break;
                    else
                    {
                        if(i==str.size()-1)
                            cout<<"I have no idea!"<<endl;
                        else
                            continue;
                    }

                }
            }
        }
    }
    ok;
}

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11538003.html