一些过去知识

  1. 素数晒

  1. 快速幂

  1. 最大公约数gcd

  1. dfs

#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
int maze[10][10];

bool vis[7][7];

int x1[7]={1,-1,0,0};
int y1[7]={0,0,1,-1};
//控制上下左右的
struct nod
{
    int x;
    int y;
}f[10][10],ans[27];
//f是父亲节点

void bfs(int x,int y)
{
    queue<nod>q;
    q.push({x,y}); 
    vis[x][y]=1;
    while(q.size() )
    {
        nod now;
        now=q.front() ;
        q.pop() ;
        if(now.x ==4&&now.y ==4)
        {
            return;
        }
        for(int i=0;i<4;i++)
        {
            nod run;
            run.x =now.x +x1[i];
            run.y =now.y +y1[i];
            if(vis[run.x ][run.y ]==1||maze[run.x ][run.y ]==1||run.x <0||run.x >4||run.y <0||run.y >4)
            {
                continue;
            }
            vis[run.x ][run.y ]=1;
            q.push(run); 
            f[run.x][run.y]={now.x,now.y};
        }
    }
    return;
}
int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            scanf("%d",&maze[i][j]);
        }
    }
    
    bfs(0,0);
    f[0][0]={-1,-1};
    int mood=0,t=4,k=4;
    while(f[t][k].x !=-1||f[t][k].y !=-1)
    {
        ans[mood++]={t ,k };
        int t1= f[t][k].x ;
        int t2=f[t][k].y ;
        t=t1,k=t2;
    }
    ans[mood]={0,0};
    for(int i=mood;i>=0;i--)
    {
        printf("(%d,%d)\n",ans[i].x,ans[i].y  );
    }
 } 
  1. 还是bfs

#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
int maze[5][5] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 1, 0,
};
int coorx[4]={0,1,0,-1};
int coory[4]={1,0,-1,0};
struct now
{
    int x;
    int y;
}f[7][7],ans[27];
bool dep[7][7];
void bfs(int x,int y)
{
    now s;
    s.x=x,s.y=y;
    queue<now>q;
    q.push(s);
    dep[x][y]=1;
    if()
    while(q.size())
    {
        now nn=q.front();
        q.pop();
            for(int i=0;i<4;i++)
        {
            now pd=nn;
            pd.x+=coorx[i];
            pd.y+=coory[i];
            if(maze[s.x][s.y]==1||pd.x>4||pd.y>4||dep[s.x][s.y]||pd.x<0||pd.y<0)
            {
                continue;
            }
            q.push(pd);
            dep[s.x][s.y]=1;
            f[pd.x][pd.y]={nn.x ,nn.y };    
        }
    }
    
}
int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            if(maze[i][j]==1||dep[i][j]==6)
            {
                continue;
            }
            else
            {
                bfs(i,j);
            }
        }
     } 
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值