队列1(迷宫问题)

问题描述

给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?

输入

第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2(1 ≤ k ≤ 10, 1 ≤ x1, x2≤ n, 1 ≤ y1, y2≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。

输出

每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。

样例输入

2

5 5

...**

*.**.

.....

.....

*....

1 1 1 1 3

5 5

...**

*.**.

.....

.....

*....

2 1 1 1 3

样例输出

no

yes

#include"iostream"
#include"windows.h"
#include"time.h"
#include"queue"
using namespace std;

void gotoxy(int x, int y) //goto语句  
{  
    COORD pos;  
    pos.X = x;  
    pos.Y = y;  
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);  
}

class Point{
public:
    int x;
    int y;
    int k;                    //转弯次数
    int dire;                //当前方向,上0,下1,左2,右3, -1 表示4个方向(起点)
    Point(int y,int x,int k,int dire){
        this->x = x;
        this->y = y;
        this->k = k;
        this->dire = dire;
    }
};
class Maze{
private:
    char **map;
    int w;
    int h;
public:
    friend void task();
    Maze(int w = 0,int h = 0);
    void deleteMap();
    void initialization();
    int move(int x,int y,int x1,int y1,int k1);
    void show();
    
};

int main(){
    void randMaze();
    randMaze();
//    task();
    return 0;
}
void Maze::initialization(){
    srand(time(NULL));
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            map[i][j] = '.';
        }
    }
    for(i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(rand()%4 == 0){
                map[i][j] = '*';
            }
        }
    }
    map[0][0] = '.';
    map[h - 1][w - 1] = '.';
}
void Maze::deleteMap(){
    for(int i = 0;i < h;i++){
        delete[] map[i];
    }
}
Maze::Maze(int w,int h){
    map = new char*[h];
    for(int i = 0;i < h;i++){
        map[i] = new char[w];
    }
    this->w = w;
    this->h = h;
}
void Maze::show(){
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            cout<<map[i][j];
        }
        cout<<endl;
    }
}
int Maze::move(int x,int y,int x1,int y1,int k1){
    queue<Point> q;
    q.push(Point(x,y,0,-1));
    while(!q.empty()){
        Point p = q.front();
        gotoxy(p.x,p.y);cout<<'+';Sleep(1);            //动画效果
        if(p.x == w - 1 && p.y == h - 1||p.x == x1&&p.y == y1){
            return p.k;
        }
        if(p.x + 1<w&&map[p.y][p.x + 1]=='.'){        //右走
            int k = p.k;
            if(p.dire!=3&&p.dire!=-1){
                k++;
            }
            if(k <= k1){
                map[p.y][p.x + 1] = '+';
                q.push(Point(p.y,p.x + 1,k,3));
            }
        }
        if(p.y + 1<h&&map[p.y + 1][p.x]=='.'){        //下走
            int k = p.k;
            if(p.dire!=1&&p.dire!=-1){
                k++;
            }
            if(k <= k1){
                map[p.y + 1][p.x] = '+';
                q.push(Point(p.y + 1,p.x,k,1));
            }
        }
        if(p.x > 0&&map[p.y][p.x - 1]=='.'){        //左走
            int k = p.k;
            if(p.dire!=2&&p.dire!=-1){
                k++;
            }
            if(k <= k1){
                map[p.y][p.x - 1] = '+';
                q.push(Point(p.y,p.x - 1,k,2));
            }
        }
        if(p.y > 0&&map[p.y - 1][p.x]=='.'){        //上走
            int k = p.k;
            if(p.dire!=0&&p.dire!=-1){
                k++;
            }
            if(k <= k1){
                map[p.y - 1][p.x] = '+';
                q.push(Point(p.y - 1,p.x,k,0));
            }
        }
        q.pop();
    }
    return -1;
}
void randMaze(){
    int w = 90;
    int h = 20;
    void task();
    Maze m(w,h);
    m.initialization();
    m.show();
    system("pause");
    int n = m.move(0,0,w - 1,h - 1,100);
    gotoxy(0,22);
    cout<<n<<endl;
    m.deleteMap();
}

void task(){
    int h = 5,w = 5,n;
    int k = 1,x1 = 1,x2 = 1,y1 = 1,y2 = 3;
    cin>>n;
    while(n--){
        cin>>w>>h;
        Maze m(w,h);
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                cin>>m.map[i][j];
            }
        }
        cin>>k>>x1>>x2>>y1>>y2;
        if(m.move(x1 - 1,y1 - 1,x2 - 1,y2 - 1,k) != -1){
            cout<<"yes"<<endl;
        }
        else{
            cout<<"no"<<endl;
        }
        m.deleteMap();
    }
}

/*
2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

*/

猜你喜欢

转载自www.cnblogs.com/oleolema/p/9028434.html