广搜水题

诡异的楼梯
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;
struct posiT
{
    int x,y,n;
};
int steps[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int main()
{
    queue<posiT> que;
    char map[22][22],what;
    int m,n,min=999,newx,newy,add_step;
    cin>>m>>n;
    posiT start,end,newtemp,temp;
    for(int i=0;i<=m+1;i++)
    {
        for(int j=0;j<=n+1;j++)
        {
            if(i>=1&&i<=m&&j>=1&&j<=n)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                {
                    start.x=i;start.y=j;
                }
                if(map[i][j]=='T'){end.x=i;end.y=j;}
            }
            else
            {
                map[i][j]='*';
            }
        }
    }
    start.n=0;
    que.push(start);
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        if(temp.x==end.x&&temp.y==end.y)
        {
            if(temp.n<min)min=temp.n;
        }
        else{
        for(int i=0;i<4;i++)
        {
            newx=temp.x+steps[i][0];newy=temp.y+steps[i][1];
            if(map[newx][newy]=='.'||map[newx][newy]=='T')
            {
                newtemp.x=newx;
                newtemp.y=newy;
                newtemp.n=temp.n+1;
                que.push(newtemp);
                map[temp.x][temp.y]='*';
            }
            // steps[0]  steps[1]  代表左右, steps[2] step[3]上下
            else if(map[newx][newy]=='|'||map[newx][newy]=='-')
            {
                add_step=0;
                what=map[newx][newy];
                if(temp.n%2==1)
                {
                    if(what=='|')what='-';
                    else{what='|';}
                }
                if((i>1&&what=='|')||(i<=1&&what=='-'))add_step=1;
                newx+=steps[i][0];newy+=steps[i][1];
                if(map[newx][newy]=='.'||map[newx][newy]=='T')
                {
                   newtemp.x=newx;
                   newtemp.y=newy;
                   newtemp.n=temp.n+1+add_step;
                   que.push(newtemp);
                   map[temp.x][temp.y]='*';
                }
            }
        }
        }
    }
    cout<<min;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值