【codevs1215】迷宫

problem

solution

codes

#include<iostream>
#include<queue>
using namespace std;
struct xyz{
    int x, y;
    xyz(int x = 0, int y = 0):x(x),y(y){};
};
int m, n, book[20][20];
char a[20][20];
const int dx[] = {0,1,-1,0};
const int dy[] = {1,0,0,-1};
int main(){
    cin>>m;
    while(m--){
        cin>>n;
        for(int i = 0; i < n; i++)cin>>a[i];
        bool ok = false;
        queue<xyz>q;  q.push(xyz(0,0));
        while(!q.empty()){
            xyz t = q.front();  q.pop();
            book[t.x][t.y] = 1;
            if(t.x==n-1 && t.y==n-1){ ok = true; break;}
            for(int i = 0; i < 4; i++){
                int newx = t.x+dx[i], newy = t.y+dy[i];
                if(newx>=0 && newx<n && newy>=0 && newy<n && (a[newx][newy]=='.'||a[newx][newy]=='e') && !book[newx][newy])
                    { book[newx][newy] = 1; q.push(xyz(newx, newy)); }
            }
        }
        if(ok)cout<<"YES
";else cout<<"NO
";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444708.html