题解 | #寻找最后之作#

寻找最后之作

https://ac.nowcoder.com/acm/problem/21444

缠斗了一个小时 发现是getchar问题 吐血 下午去查查getchar用法 https://www.runoob.com/cprogramming/c-function-getchar.html 牛啊

#include<iostream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
int n,m;
struct Point{
    int src,des,step;
    //Point();
    Point(int _src,int _des,int _step){
        src=_src;
        des=_des;
        step=_step;
    }
};
//bool p[105];
char a[105][105];
bool used[105][105];
int ax,ay,lx,ly,ans;
int x[10]={-1,1,0,0,-1,1,-1,1};
int y[10]={0,0,-1,1,-1,-1,1,1};
//int fa[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};
queue<Point> q;
void bfs(){
    memset(used,0,sizeof(used));
//         Point c,b;
//         c.src=ax;
//         c.des=ay;
//         c.step=0;
        //q.push(c);
        q.push(Point(ax,ay,0));
        used[ax][ay]=1;
        while(!q.empty()){
            Point p = q.front();
            q.pop();
            int px = p.src,py=p.des,step=p.step+1;
            if(px==lx&&py==ly) ans =p.step;
            for(int i =0;i<8;i++){
                int newx=px+x[i],newy=py+y[i];
                if(a[newx][newy]!='#'&&newx>=1&&newx<=m&&newy>=1&&newy<=n
                   &&!used[newx][newy])
                {
//                     c.src=newx;
//                     c.des=newy;
//                     c.step=step;
//                     q.push(c);
                    q.push(Point(newx,newy,step));
                    used[newx][newy]=1;
                }
            }
        }
        if(used[lx][ly]) cout<<ans<<endl;
        else cout<<"I Need Move Point\n";
        return;
}

int main(){
    int t;
    cin >>t;
    while(t--){
        cin >>m>>n;
        getchar();
        for(int i =1;i<=m;i++){
            for(int j =1;j<=n;j++){
                cin >> a[i][j];
                if(a[i][j]=='A'){
                    ax=i,ay=j;
                }
                else if(a[i][j]=='L')
                {
                    lx=i,ly=j;
                }
            }
            getchar();
        }
        bfs();
    }
    return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务