真 木乃伊迷宫 上午上传的那个有误 现已更改 给各位造成的不便敬请谅解

本文介绍了一个迷宫追逐游戏的实现算法,其中包括了迷宫的构建、鼠与猫的位置更新逻辑、移动策略及捕获判断等核心内容。通过使用队列来跟踪角色位置,并设计特定条件下的移动规则,确保游戏的进行直至捕获目标或确定无法捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int num_wall;
int x0_m,y0_m;
int x0_r,y0_r;
int x_exist,y_exist;
int x1,y1;
int x2,y2;
int bug;
int position[30][3];
int caught();
char maze[6][6];
bool flag[6][6];
void input();
void init();
void search();
void mov_m();
queue<int> row_r;
queue<int> col_r;
queue<int> row_m;
queue<int> col_m;


int main()
{
input();

    init();   
    
    while(bug==0&&!row_r.empty())
    {
    search();
    }
    if(bug==0)
    {
    printf("No\n");
    }
return 0;
}


void input()
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
maze[i][j]='O';
}
}
scanf("%d\n",&num_wall);
for(i=0;i<num_wall;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&position[i][j]);
}
}
for(i=0;i<num_wall;i++)
{
if(position[i][2]==0)
{
maze[position[i][0]+1][position[i][1]]='X';
}
if(position[i][2]==1)
{
maze[position[i][0]][position[i][1]+1]='X';
}
}
scanf("%d %d\n",&x0_m,&y0_m);
scanf("%d %d\n",&x0_r,&y0_r);
scanf("%d %d",&x_exist,&y_exist);
}


void init()
{
row_r.push(x0_r);
col_r.push(y0_r);
row_m.push(x0_m);
col_m.push(y0_m);
flag[x0_r][y0_r]=1;
}


void search()
{
int p,q,r,s;

p=row_r.front();
row_r.pop();
q=col_r.front();
col_r.pop();
r=row_m.front();
row_m.pop();
s=col_m.front();
col_m.pop();

x1=p;
y1=q;
x2=r;
y2=s;

if(x1>0&&maze[x1-1][y1]=='O')
{
x1=x1-1;
y1=y1;
        if(caught()==1)
        {
        if(x1==x_exist&&y1==y_exist)
   {
   printf("Yes\n");
   bug=1;
   return ;
   }
else
{
            mov_m();
}
        }
}

x1=p;
y1=q;
x2=r;
y2=s;

if(x1<5&&maze[x1+1][y1]=='O')
{
x1=x1+1;
y1=y1;
        if(caught()==1)
        {
        if(x1==x_exist&&y1==y_exist)
   {
   printf("Yes\n");
   bug=1;
   return ;
   }
else
{
            mov_m();
}
        }
}

x1=p;
y1=q;
x2=r;
y2=s;

if(y1>0&&maze[x1][y1-1]=='O')
{
x1=x1;
y1=y1-1;
        if(caught()==1)
        {
        if(x1==x_exist&&y1==y_exist)
   {
   printf("Yes\n");
   bug=1;
   return ;
   }
else
{
            mov_m();
}
        }
}

x1=p;
y1=q;
x2=r;
y2=s;

if(y1<5&&maze[x1][y1+1]=='O')
{
x1=x1;
y1=y1+1;
        if(caught()==1)
        {
        if(x1==x_exist&&y1==y_exist)
   {
   printf("Yes\n");
   bug=1;
   return ;
   }
else
{
            mov_m();
}
        }
}
}
int caught()
{
if(x1!=x2||y1!=y2)
{
return 1;
}
else
{
return 0;
}
}
void mov_m()
{
if(y2<y1&&maze[x2][y2+1]=='O')
{
y2=y2+1;
x2=x2;
        if(caught()==1)
        {
        if(y2<y1&&maze[x2][y2+1]=='O')
        {
        y2=y2+1;
        x2=x2;
        if(caught()==1&&flag[x1][y1]==0)
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2);  
flag[x1][y1]=1;      
        }
        }
        else if(y2<y1&&maze[x2][y2+1]=='X')
        {
         row_r.push(x1);
           col_r.push(y1);
           row_m.push(x2);
           col_m.push(y2);  
flag[x1][y1]=1;        
        }
        else if(y2==y1)
        {
        if(x2<x1&&maze[x2+1][y2]=='O')
        {
        x2=x2+1;
        y2=y2;
                    if(caught()==1&&flag[x1][y1]==0)
                    {
                 row_r.push(x1);
                   col_r.push(y1);
                   row_m.push(x2);
                   col_m.push(y2); 
   flag[x1][y1]=1;                  
                    }
        }
        else if(x2<x1&&maze[x2+1][y2]=='X')
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
flag[x1][y1]=1;                  
        }        
        else if(x2>x1&&maze[x2-1][y2]=='O')
        {
        x2=x2-1;
        y2=y2;
                    if(caught()==1&&flag[x1][y1]==0)
                    {
                 row_r.push(x1);
                   col_r.push(y1);
                   row_m.push(x2);
                   col_m.push(y2);   
     flag[x1][y1]=1;                
                    }        
        }
        else if(x2>x1&&maze[x2-1][y2]=='X')
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
flag[x1][y1]=1;        
        }
        }
        }
}
else if(y2<y1&&maze[x2][y2+1]=='X')
{
row_r.push(x1);
   col_r.push(y1);
   row_m.push(x2);
   col_m.push(y2); 
flag[x1][y1]=1;
}
else if(y2==y1)
{
        if(x2<x1&&maze[x2+1][y2]=='O')
        {
        x2=x2+1;
        y2=y2;
            if(x2<x1&&maze[x2+1][y2]=='O')
            {
            x2=x2+1;
            y2=y2;
            if(caught()==1&&flag[x1][y1]==0)
            {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
flag[x1][y1]=1;                
            }
            }
            else if(x2<x1&&maze[x2+1][y2]=='X')
            {
         row_r.push(x1);
           col_r.push(y1);
           row_m.push(x2);
           col_m.push(y2); 
flag[x1][y1]=1;            
            }
        }
        else if(x2<x1&&maze[x2+1][y2]=='O')
        {
     row_r.push(x1);
       col_r.push(y1);
       row_m.push(x2);
       col_m.push(y2); 
flag[x1][y1]=1;      
        }
        else if(x2>x1&&maze[x2-1][y2]=='O')
        {
        x2=x2-1;
        y2=y2;
            if(x2>x1&&maze[x2-1][y2]=='O')   
{
x2=x2-1;
y2=y2;
            if(caught()==1&&flag[x1][y1]==0)
            {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
flag[x1][y1]=1;              
            }

else if(x2>x1&&maze[x2-1][y2]=='X')  
{
row_r.push(x1);
           col_r.push(y1);
           row_m.push(x2);
           col_m.push(y2); 
flag[x1][y1]=1;
}
        }
else if(x2>x1&&maze[x2-1][y2]=='X')
{
     row_r.push(x1);
       col_r.push(y1);
       row_m.push(x2);
       col_m.push(y2); 
flag[x1][y1]=1;
}
}
else if(y2>y1&&maze[x2][y2-1]=='O')
{
y2=y2-1;
x2=x2;
        if(caught()==1)
        {
        if(y2>y1&&maze[x2][y2-1]=='O')
        {
        y2=y2-1;
        x2=x2;
        if(caught()==1&&flag[x1][y1]==0)
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2);
  flag[x1][y1]=1;      
        }
        }
        else if(y2>y1&&maze[x2][y2-1]=='O')
        {
         row_r.push(x1);
           col_r.push(y1);
           row_m.push(x2);
           col_m.push(y2);
  flag[x1][y1]=1;        
        }
        else if(y2==y1)
        {
        if(x2<x1&&maze[x2+1][y2]=='O')
        {
        x2=x2+1;
        y2=y2;
                    if(caught()==1&&flag[x1][y1]==0)
                    {
                 row_r.push(x1);
                   col_r.push(y1);
                   row_m.push(x2);
                   col_m.push(y2); 
     flag[x1][y1]=1;                  
                    }
        }
        else if(x2<x1&&maze[x2+1][y2]=='X')
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
  flag[x1][y1]=1;        
        }
        else if(x2>x1&&maze[x2-1][y2]=='O')
        {
        x2=x2-1;
        y2=y2;
                    if(caught()==1&&flag[x1][y1]==0)
                    {
                 row_r.push(x1);
                   col_r.push(y1);
                   row_m.push(x2);
                   col_m.push(y2); 
     flag[x1][y1]=1;                  
                    }        
        }
        else if(x2>x1&&maze[x2-1][y2]=='X')
        {
             row_r.push(x1);
               col_r.push(y1);
               row_m.push(x2);
               col_m.push(y2); 
  flag[x1][y1]=1;        
        }
        }
        }
}
else if(y2>y1&&maze[x2][y2-1]=='X')
{
  row_r.push(x1);
   col_r.push(y1);
   row_m.push(x2);
   col_m.push(y2); 
  flag[x1][y1]=1;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值