素数晒
快速幂
最大公约数gcd
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 );
}
}
还是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);
}
}
}
}