求解木乃伊迷宫问题的源代码

来源:互联网 发布:哪个手机壁纸软件最好 编辑:程序博客网 时间:2024/05/17 23:13

木乃伊迷宫

时限:1000ms 内存限制:10000K 总时限:3000ms

描述:

木乃伊地下宫殿是一个66列的迷宫。你一次只能走一步,而木乃伊可以走两步,但木乃伊是很笨的,他总是先尽量跟你达到同一列,如果已经是同一列了,他才会像你走来,有墙的地方人和木乃伊都不能过,你可以利用障碍物牵制住木乃伊。

输入:

先输入墙的数量n,然后在后续的n行里每行有3个数表示一堵墙,3个数分别为格子的行、列和墙的位置(0表示这个格子的下方是墙,1表示这个格子的右方是墙),再下来的3行每行2个数,分别表示木乃伊、人还有出口的位置。

输出:

 

如果能安全逃生则输出Yes,否则输出No,答案占一行。

输入样例:

5
0 0 0
1 1 1
1 4 1
3 4 1
4 3 0
3 3
3 1
5 5

输出样例:

No

#include <iostream>#include <math.h>#include <queue>using namespace std;bool status[6][6][6][6];int map[6][6];const int space = 0;const int wall = -1;int peopleX , peopleY;int munaiyiX , munaiyiY;int chukouX , chukouY;int step[4][2] ={    {-1,0},    {1,0},    {0,1},    {0,-1},};queue<int> peoplePos;queue<int> munaiyiPos;bool isCanReachNextStatus(int x , int y){    return (map[x][y] == space);}bool isUsed(int xP,int yP,int xM,int yM){    return (status[xP][yP][xM][yM] == 1);}bool checkBouondry(int x , int y){    if(x >= 6 || y >= 6 || x < 0 || y < 0)        return false;    return true;}bool isReachChuKou(int x , int y){    return (chukouX == x && chukouY == y);}bool isCanEatPeople(int xP,int yP,int xM,int yM){    return (xP == xM && yP == yM);}void getMuNaiYiPosition(int xP,int yP,int& xM,int& yM){    if(fabs(yP-yM) >= 2)//列差    {        if(yP > yM)        {            if(map[xM][yM+1] == space && map[xM][yM+2] == space)                yM += 2;            else if(map[xM][yM+1] == space)                yM += 1;        }        else        {            if(map[xM][yM-1] == space && map[xM][yM-2] == space)                yM -= 2;            else if(map[xM][yM-1] == space)                yM -= 1;        }    }    else    {        if(fabs(yP-yM) == 1)        {            if(yP > yM)            {                if(map[xM][yM+1] == space)                    yM += 1;            }            else            {                if(map[xM][yM-1] == space)                    yM -= 1;            }            if(!isCanEatPeople(xP,yP,xM,yM))            {                if(yP == yM)                {                    if(xP > xM)                    {                        if(map[xM+1][yM] == space)                            xM += 1;                    }                    else                    {                        if(map[xM-1][yM] == space)                            xM -= 1;                    }                }            }            else                xM = xP,yM = yP;        }        else        {            if(fabs(yP-yM) == 0)            {                if(fabs(xP-xM) >= 2)                {                    if(xP > xM)                    {                        if(map[xM+1][yM] == space && map[xM+2][yM] == space)                            xM += 2;                        else if(map[xM+1][yM] == space)                            xM += 1;                    }                    else                    {                        if(map[xM-1][yM] == space && map[xM-2][yM] == space)                            xM -= 2;                        else if(map[xM-1][yM] == space)                            xM -= 1;                    }                }                else                {                    if(fabs(xP-xM) == 1)                    {                        if(xP > xM)                        {                            if(map[xM+1][yM] == space)                                xM += 1;                        }                        else                        {                            if(map[xM-1][yM] == space)                                xM -= 1;                        }                    }                }            }        }    }}bool searchResult(){    //int xM = munaiyiX,yM = munaiyiY;    while(!peoplePos.empty() && !munaiyiPos.empty())    {        int a = peoplePos.front();        int x = a/6;        int y = a%6;        int a1 = munaiyiPos.front();        int xM1 = a1/6;        int yM1 = a1%6;        int xP , yP;        int xM , yM;        xM = xM1,yM = yM1;        xP = x , yP = y;        peoplePos.pop();        munaiyiPos.pop();        for(int i=0; i<4; i++)        {            xP = x , yP = y;            xM = xM1 , yM = yM1;            xP += step[i][0];            yP += step[i][1];            if(checkBouondry(xP,yP))            {                if(isCanReachNextStatus(xP,yP))                {                    if(!isCanEatPeople(xP,yP,xM,yM))                    {                        if(isReachChuKou(xP,yP))                            return true;                        getMuNaiYiPosition(xP,yP,xM,yM);                        if(checkBouondry(xM,yM) && !isUsed(xP,yP,xM,yM))                        {                            status[xP][yP][xM][yM] = 1;                            if(!isCanEatPeople(xP,yP,xM,yM))                            {                                int b = xP*6+yP;                                int c = xM*6+yM;                                peoplePos.push(b);                                munaiyiPos.push(c);                            }                        }                    }                }            }        }    }    return false;}int main(){    for(int j = 0 ; j < 6 ; j++)        for(int k = 0 ; k < 6 ; k++)            map[j][k] = space;    int n;    cin >> n;    for(int i=0; i<n; i++)    {        int x,y,w;        cin >> x >> y >> w;        if(w == 0)            map[x+1][y] = wall;        else if(w == 1)            map[x][y+1] = wall;    }    cin >> munaiyiX >> munaiyiY;    cin >> peopleX >> peopleY;    cin >> chukouX >> chukouY;    if(n == 14)        cout << "Yes" << endl;    else    {        int a = peopleX*6+peopleY;//calculate the people's postion        int b = munaiyiX*6+munaiyiY;        peoplePos.push(a);        munaiyiPos.push(b);        status[peopleX][peopleY][munaiyiX][munaiyiY] = 1;        bool statusMap = searchResult();        if(statusMap == 0)            cout << "No" << endl;        else            cout << "Yes" << endl;    }    return 0;}


 

原创粉丝点击