介绍
基于 easyx 和 C++ 的寻路算法演示图。
效果展示
基于 easyx 和 C++ 的寻路算法演示视频
主体代码
头文件:
#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<windows.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<bits/stdc++.h>
大模板(可有可无)
namespace IO{
#define ll long long
long long fast_pow(long long a,long long b,long long mod){
ll sum=1;
while(b){
if(b&1) sum=(sum*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return sum%mod;
}
ll fast_times(ll a,ll b,ll p){
ll ret=0;
while(b){
if(b&1) ret=(ret+a)%p;
b>>=1;
a=(a+a)%p;
}
return ret;
}
ll very_fast_times(ll a,ll b,ll m){
unsigned long long c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m;
if(c<m) return c;
return c+m;
}
ll inv(ll a,ll p){
return fast_pow(a,p-2,p);
}
#undef ll
}
using namespace IO;
class BigNum{
#define MAXN 9999
#define SIZE 10000
#define DLEN 4
private:
int a[5000],len;
bool neg;
public:
BigNum(){len=1;neg=0;memset(a,0,sizeof(a));}
BigNum(const int b){
int c,d=b;
if(d<0) neg=1,d=-d;
else neg=0;
len=0;
memset(a,0,sizeof(a));
while(d>MAXN){
c=d-(d/(MAXN+1))*(MAXN+1);
d=d/(MAXN+1);
a[len++]=c;
}
a[len++]=d;
}
BigNum(const long long b){
long long c,d=b;
if(d<0) neg=1,d=-d;
else neg=0;
len=0;
memset(a,0,sizeof(a));
while(d>MAXN){
c=d-(d/(MAXN+1))*(MAXN+1);
d=d/(MAXN+1);
a[len++]=c;
}
a[len++]=d;
}
BigNum(char* s){
memset(a,0,sizeof(a));
int l=strlen(s);
if(s[0]=='-'){
neg=1;
for(int i=0;i<l;i++) s[i]=s[i+1];
l--;
}
else neg=0;
len=l/DLEN;
if(l%DLEN) len++;
int idx=0;
for(int i=l-1;i>=0;i-=DLEN){
int t=0,k=i-DLEN+1;
if(k<0) k=0;
for(int j=k;j<=i;j++) t=t*10+s[j]-'0';
a[idx++]=t;
}
}
BigNum(string s){
memset(a,0,sizeof(a));
if(s[0]=='-'){
neg=1;
s=s.substr(1);
}
else neg=0;
int l=s.size();
len=l/DLEN;
if(l%DLEN) len++;
int idx=0;
for(int i=l-1;i>=0;i-=DLEN){
int t=0,k=i-DLEN+1;
if(k<0) k=0;
for(int j=k;j<=i;j++) t=t*10+s[j]-'0';
a[idx++]=t;
}
}
BigNum(const char c){
memset(a,0,sizeof(a));
neg=0;
len=1;
a[0]=c-'0';
}
BigNum(const BigNum &T):len(T.len){
neg=T.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=T.a[i];
}
friend istream& operator>>(istream& in,BigNum &b){
string ch;
in>>ch;
b=BigNum(ch);
return in;
}
friend ostream& operator<<(ostream& out,BigNum b){
if(b.neg) out<<"-";
out<<b.a[b.len-1];
for(int i=b.len-2;i>=0;i--){
out.width(DLEN);
out.fill('0');
out<<b.a[i];
}
return out;
}
BigNum& operator=(const BigNum& T){
len=T.len;
neg=T.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=T.a[i];
return *this;
}
template<typename T1>
BigNum& operator=(const T1& T){
BigNum tmp=BigNum(T);
len=tmp.len;
neg=tmp.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=tmp.a[i];
return *this;
}
bool operator>(const BigNum& T)const{
if(len>T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l>=0&&a[l]>T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator>(const T1& T)const{
BigNum t2(T);
return *this>t2;
}
bool operator<(const BigNum& T)const{
if(len<T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l>=0&&a[l]<T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator<(const T1& T)const{
BigNum t2(T);
return *this<t2;
}
bool operator>=(const BigNum T)const{
if(len>T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l<0) return true;
if(a[l]>=T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator>=(const T1 T)const{
BigNum t2(T);
return *this>=t2;
}
bool operator<=(const BigNum T)const{
if(len<T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l<0) return true;
if(a[l]<=T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator<=(const T1 T)const{
BigNum t2(T);
return *this<=t2;
}
bool operator==(const BigNum T)const{
if(len!=T.len) return false;
for(int i=0;i<len;i++){
if(a[i]!=T.a[i]) return false;
}
return true;
}
template<typename T1>
bool operator==(const T1 T)const{
BigNum t(T);
return t==(*this);
}
BigNum operator+(const BigNum T){
BigNum t(*this);
int big=T.len>len?T.len:len;
for(int i=0;i<big;i++){
t.a[i]+=T.a[i];
if(t.a[i]>MAXN){
t.a[i+1]++;
t.a[i]-=MAXN+1;
}
}
if(t.a[big]!=0) t.len=big+1;
else t.len=big;
return t;
}
template<typename T1>
BigNum operator+(const T1 T){
BigNum t1(*this),t2(T);
return t1+t2;
}
BigNum operator-(const BigNum T){
BigNum t1,t2;
bool flag;
if(*this>T){
t1=*this,t2=T;
flag=0;
}
else{
t1=T,t2=*this;
flag=1;
}
int big=t1.len;
for(int i=0;i<big;i++){
if(t1.a[i]<t2.a[i]){
int j=i+1;
while(t1.a[j]==0) j++;
t1.a[j]--;
j--;
while(j>i) t1.a[j--]+=MAXN;
t1.a[i]+=MAXN+1-t2.a[i];
}
else t1.a[i]-=t2.a[i];
}
t1.len=big;
while(t1.a[t1.len-1]==0&&t1.len>1){
t1.len--;
big--;
}
if(flag) t1.neg^=1;
return t1;
}
template<typename T1>
BigNum operator-(const T1 T){
BigNum t1(*this),t2(T);
return t1-t2;
}
BigNum operator*(const BigNum &T)const{
BigNum ret;
int i,j;
for(i=0;i<len;i++){
int up=0;
for(j=0;j<T.len;j++){
int tmp=a[i]*T.a[j]+ret.a[i+j]+up;
if(tmp>MAXN){
int tmp1=tmp-tmp/(MAXN+1)*(MAXN+1);
up=tmp/(MAXN+1);
ret.a[i+j]=tmp1;
}
else{
up=0;
ret.a[i+j]=tmp;
}
}
if(up!=0) ret.a[i+j]+=up;
}
ret.len=i+j;
while(ret.a[ret.len-1]==0&&ret.len>1) ret.len--;
return ret;
}
template<typename T1>
BigNum operator*(const T1 T)const{
BigNum t1(*this),t2(T);
BigNum ans=t1*t2;
return ans;
}
BigNum operator/(const BigNum T){
BigNum remain,res;
for(int i=len-1;i>=0;i--){
remain=remain*SIZE+a[i];
int cnt=0;
while(remain>=T){
cnt++;
remain=remain-T;
}
res.a[i]=cnt;
}
res.len=len;
while(res.a[res.len-1]==0&&res.len>1) res.len--;
return res;
}
template<typename T1>
BigNum operator/(const T1 T){
BigNum t1(*this),t2(T);
return t1/t2;
}
BigNum operator%(const BigNum T){
BigNum t(*this);
return t-t/T*T;
}
template<typename T1>
BigNum operator%(const T1 T){
BigNum t1(*this),t2(T);
return t1%t2;
}
int size(){
int ans=DLEN*(len-1),tmp=a[len-1];
while(tmp){
ans++;
tmp/=10;
}
return ans;
}
int length(){return size();}
bool empty(){return len==1&&a[0]==0;}
void clear(){
len=1;
memset(a,0,sizeof(a));
}
BigNum gcd(BigNum a,BigNum b){
if(b.empty()) return a;
return gcd(b,a%b);
}
BigNum qpow(BigNum a,BigNum b){
BigNum ans=1;
while(!b.empty()){
if(b%2==1) ans=ans*a;
a=a*a;
b=b/2;
}
return ans;
}
BigNum qpow(BigNum a,BigNum b,BigNum mod){
BigNum ans=1;
while(!b.empty()){
if(b%2==1) ans=ans*a%mod;
a=a*a%mod;
b=b/2;
}
return ans%mod;
}
#undef MAXN
#undef DLEN
#undef SIZE
};
struct HASH{
int sed,mod,h[500006],pw[500006];
HASH(int ssed=128,int mmod=998244353){
sed=ssed,mod=mmod;
pw[0]=1;
for(int i=1; i<500001; i++) pw[i]=pw[i-1]*1ll*sed%mod;
}
void make(string s){
h[0]=s[0]%mod;
for(int i=1; i<s.size(); i++){
h[i]=(h[i-1]*1ll*sed%mod+s[i])%mod;
}
}
void make(char* s){
h[0]=s[0]%mod;
for(int i=1; s[i]; i++){
h[i]=(h[i-1]*1ll*sed%mod+s[i])%mod;
}
}
int get(int l,int r){
if(l==0) return h[r];
return (h[r]-h[l-1]*1ll*pw[r-l+1]%mod+mod)%mod;
}
int get(int r){
return h[r];
}
};
template<int length2>
class tree_array{
private:
long long sum[length2+5]={0};
size_t sum_size=0;
public:
long long lowbit(long long x){
return x&-x;
}
long long getsum(long long pos){
long long ans=0;
for(int i=pos; i;i-=this->lowbit(i)){
ans+=sum[i];
}
return ans;
}
long long mod_getsum(long long pos,long long mod){
long long ans=0;
for(int i=pos; i;i-=this->lowbit(i)){
ans+=sum[i];
ans%=mod;
}
return ans;
}
long long getsum(long long pos1,long long pos2){
return this->getsum(pos2)-this->getsum(pos1-1);
}
long long mod_getsum(long long pos1,long long pos2,long long mod){
return (this->getsum(pos2)-this->getsum(pos1-1))%mod;
}
void updata(long long pos,long long y){
sum_size++;
for(long long i=pos; i<=length2; i+=this->lowbit(i)) sum[i]+=y;
}
void erase(long long pos,long long y){
this->updata(pos,-y);
}
void replace(long long pos,long long y,long long a[]){
if(y>a[pos]) this->updata(pos,y-a[pos]);
else if(y<a[pos]) this->erase(pos,y-a[pos]);
}
size_t size(){
return sum_size;
}
};
步入正题
结构体存储方块情况
struct Gird{
COLORREF color;//颜色
bool no;//是否是墙
bool start,end;//是否是开头或结尾;
int data;//步数
};
绘制按钮
namespace Button{
void button(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn,TCHAR text[20]){//绘制按钮
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle((rn.first-lu.first+1)/4+2, 0, _T("宋体"));
outtextxy(lu.first+(rn.first-lu.first)/4,lu.second+((rn.second-lu.second+1)/5)/2,text);
}
}
填充&输出方块状态
namespace CLR{
void Color(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn){//没字的填充方格
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
}
void Color2(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn,int data,int length){//有字的填充方格
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
int k=(int)ceil(log10(data));
if(k<=0) k=1;
setbkmode(TRANSPARENT);
TCHAR text[20];
_stprintf(text,_T("%d"),data);
settextcolor(WHITE);
settextstyle(length/4+2, 0, _T("宋体"));
outtextxy(lu.first+length/4,lu.second+(length/5*4)/2,text);
}
void Fill(Gird a[105][105],int n,int m,int length){//填充
for(int i=0,ii=1; i<=(n-1)*length; i+=length,ii++){
for(int j=300,jj=1; j<=300+(m-1)*length; j+=length,jj++){
if(a[ii][jj].no==1) Color(RED,WHITE,{j,i},{j+length,i+length});
else if(a[ii][jj].start==1) button(BLUE,WHITE,{j,i},{j+length,i+length},"起点");
else if(a[ii][jj].end==1) button(GREEN,WHITE,{j,i},{j+length,i+length},"终点");
else if(a[ii][jj].data==-1||a[ii][jj].data==0) Color(RGB(0,191,255),WHITE,{j,i},{j+length,i+length});
else Color2(a[ii][jj].color,WHITE,{j,i},{j+length,i+length},a[ii][jj].data,length);
}
}
}
};
好了,接下来到达本篇重点,寻路模块
检查是否有起点和终点
bool check(pair<int,int> start,pair<int,int> end){
if(start.first==0) return false;
if(end.first==0) return false;
return true;
}
BFS 模块
vector<pair<int,int> > v[105][105];
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
void BFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
双向 BFS 模块,使用的一些变量在上面
void DBFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q,pp,qq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
int f[105][105]={0};
f[start.first][start.second]=1;
f[end.first][end.second]=2;
p.push(start.first);
q.push(start.second);
pp.push(end.first);
qq.push(end.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
int xxx=pp.front();
pp.pop();
int yyy=qq.front();
qq.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]!=1){
if(f[xx][yy]==2){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].insert(v[xx][yy].begin(),v[x][y][j]);
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size(),{x,y});
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size()+1,{xx,yy});
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xx][yy].push_back({x,y});
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
for(int i=0;i<4;i++){
int xxxx=xxx+dx[i],yyyy=yyy+dy[i];
if(xxxx>=1&&xxxx<=n&&yyyy>=1&&yyyy<=n){
if(a[xxxx][yyyy].no==0&&f[xxxx][yyyy]!=2){
if(f[xxxx][yyyy]==1){
reverse(v[xxxx][yyyy].begin(),v[xxxx][yyyy].end());
v[xxxx][yyyy].push_back({xxxx,yyyy});
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=v[xxx][yyy].size()-1;j>-1; j--) v[xxxx][yyyy].push_back(v[xxx][yyy][v[xxx][yyy].size()-j-1]);
for(int i=0;i<v[xxxx][yyyy].size(); i++){
a[v[xxxx][yyyy][i].first][v[xxxx][yyyy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=0;j<v[xxx][yyy].size(); j++) v[xxxx][yyyy].push_back(v[xxx][yyy][j]);
a[xxxx][yyyy].data=v[xxxx][yyyy].size();
a[xxxx][yyyy].color=GREEN;
f[xxxx][yyyy]=2;
pp.push(xxxx);
qq.push(yyyy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
DFS(无随机)模块
int DFSflag=0,DFSvis[105][105];
void DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) DFSflag=0,memset(DFSvis,0,sizeof DFSvis);
if(DFSflag==1) return;
DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
DFSflag=1;
return;
}
int x=now.first,y=now.second;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
DFS(a,start,end,n,{xx,yy},v);
if(DFSflag) return;
}
}
}
}
有随机 DFS 模块
int R_DFSflag,R_DFSvis[105][105];
void R_DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) R_DFSflag=0,memset(R_DFSvis,0,sizeof R_DFSvis),srand(time(NULL));
if(R_DFSflag==1) return;
R_DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
R_DFSflag=1;
return;
}
int x=now.first,y=now.second;
int t[5]={0,1,2,3};
int i=rand()%4;
int i2=rand()%4;
while(i2==i) i2=rand()%4;
int i3=rand()%4;
while(i3==i||i3==i2) i3=rand()%4;
int i4=rand()%4;
while(i4==i||i4==i2||i4==i3) i4=rand()%4;
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i2;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i3;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i4;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
}
A* 模块(启发函数为曼哈顿距离)
struct Node{
int x,y;
int F,G,H;
bool operator<(const Node &a)const{
return F==a.F?G>a.G:F>a.F;
}
};
int AStar_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return abs(x-x1)+abs(y-y1);
}
Node make_node(pair<int,int> now,int G,int H){
int F=G+H;
Node Temp;
Temp.x=now.first;
Temp.y=now.second;
Temp.F=F;
Temp.G=G;
Temp.H=H;
return Temp;
}
priority_queue<Node> NEW;
void AStar(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
A* 模块(启发函数为欧几里得距离)(某些函数为上面代码中的)
int AStar_2_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return sqrt(pow(x-x1,2)+pow(y-y1,2));
}
void AStar_2(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_2_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_2_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
Dijkstra 模块
void DIJ(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
寻路模块总体代码
namespace Pathfinding{
bool check(pair<int,int> start,pair<int,int> end){
if(start.first==0) return false;
if(end.first==0) return false;
return true;
}
vector<pair<int,int> > v[105][105];
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
void BFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
void DBFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q,pp,qq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
int f[105][105]={0};
f[start.first][start.second]=1;
f[end.first][end.second]=2;
p.push(start.first);
q.push(start.second);
pp.push(end.first);
qq.push(end.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
int xxx=pp.front();
pp.pop();
int yyy=qq.front();
qq.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]!=1){
if(f[xx][yy]==2){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].insert(v[xx][yy].begin(),v[x][y][j]);
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size(),{x,y});
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size()+1,{xx,yy});
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xx][yy].push_back({x,y});
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
for(int i=0;i<4;i++){
int xxxx=xxx+dx[i],yyyy=yyy+dy[i];
if(xxxx>=1&&xxxx<=n&&yyyy>=1&&yyyy<=n){
if(a[xxxx][yyyy].no==0&&f[xxxx][yyyy]!=2){
if(f[xxxx][yyyy]==1){
reverse(v[xxxx][yyyy].begin(),v[xxxx][yyyy].end());
v[xxxx][yyyy].push_back({xxxx,yyyy});
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=v[xxx][yyy].size()-1;j>-1; j--) v[xxxx][yyyy].push_back(v[xxx][yyy][v[xxx][yyy].size()-j-1]);
for(int i=0;i<v[xxxx][yyyy].size(); i++){
a[v[xxxx][yyyy][i].first][v[xxxx][yyyy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=0;j<v[xxx][yyy].size(); j++) v[xxxx][yyyy].push_back(v[xxx][yyy][j]);
a[xxxx][yyyy].data=v[xxxx][yyyy].size();
a[xxxx][yyyy].color=GREEN;
f[xxxx][yyyy]=2;
pp.push(xxxx);
qq.push(yyyy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
int DFSflag=0,DFSvis[105][105];
void DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) DFSflag=0,memset(DFSvis,0,sizeof DFSvis);
if(DFSflag==1) return;
DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
DFSflag=1;
return;
}
int x=now.first,y=now.second;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
DFS(a,start,end,n,{xx,yy},v);
if(DFSflag) return;
}
}
}
}
int R_DFSflag,R_DFSvis[105][105];
void R_DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) R_DFSflag=0,memset(R_DFSvis,0,sizeof R_DFSvis),srand(time(NULL));
if(R_DFSflag==1) return;
R_DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
R_DFSflag=1;
return;
}
int x=now.first,y=now.second;
int t[5]={0,1,2,3};
int i=rand()%4;
int i2=rand()%4;
while(i2==i) i2=rand()%4;
int i3=rand()%4;
while(i3==i||i3==i2) i3=rand()%4;
int i4=rand()%4;
while(i4==i||i4==i2||i4==i3) i4=rand()%4;
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i2;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i3;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i4;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
}
struct Node{
int x,y;
int F,G,H;
bool operator<(const Node &a)const{
return F==a.F?G>a.G:F>a.F;
}
};
int AStar_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return abs(x-x1)+abs(y-y1);
}
Node make_node(pair<int,int> now,int G,int H){
int F=G+H;
Node Temp;
Temp.x=now.first;
Temp.y=now.second;
Temp.F=F;
Temp.G=G;
Temp.H=H;
return Temp;
}
priority_queue<Node> NEW;
void AStar(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
int AStar_2_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return sqrt(pow(x-x1,2)+pow(y-y1,2));
}
void AStar_2(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_2_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_2_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
void DIJ(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
};
清空地图
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
a[i][j].data=-1;
a[i][j].no=a[i][j].end=a[i][j].start=0;
a[i][j].color=RGB(0,191,255);
}
}
主函数
定义
Gird a[105][105];
初始化
printf("请输入矩阵边长和速度:");
int n;
cin>>n>>Speed;
initgraph(1425,825);
setbkcolor(RGB(156,156,156));
int length=825/n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
a[i][j].data=-1;
a[i][j].no=a[i][j].end=a[i][j].start=0;
a[i][j].color=RGB(0,191,255);
}
}
Fill(a,n,n,length);
button(RED,BLACK,{40,20},{280,100},"BFS");
button(BLUE,BLACK,{40,120},{280,200},"D-BFS");
button(RGB(155,48,255),BLACK,{40,220},{280,300},"DFS");
button(RGB(255,69,0),BLACK,{40,320},{280,400},"R-DFS");
button(RGB(255,215,0),BLACK,{40,420},{280,500},"A*(1)");
button(RGB(255,0,255),BLACK,{40,520},{280,600},"A*(2)");
button(GREEN,BLACK,{40,620},{280,700},"DIJ");
button(GREEN,BLACK,{1150,20},{1400,100},"Clear");
pair<int,int> start,end;
start=end={0,0};
接收鼠标消息&执行鼠标消息(左键设置障碍,中键设置起点,右键设置终点)
while(1){
if(MouseHit()){
MOUSEMSG m=GetMouseMsg();
switch(m.uMsg){
case WM_LBUTTONDOWN:
if(m.x>=1150&&m.x<=1400&&m.y>=20&&m.y<=100){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
a[i][j].data=-1;
a[i][j].no=a[i][j].end=a[i][j].start=0;
a[i][j].color=RGB(0,191,255);
}
}
start={0,0};
end={0,0};
Fill(a,n,n,length);
}
else if(m.x>=40&&m.x<=280){
if(m.y>=20&&m.y<=100){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
BFS(a,start,end,n);
}
else if(m.y>=120&&m.y<=200){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
DBFS(a,start,end,n);
}
else if(m.y>=220&&m.y<=300){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
vector<pair<int,int> > v;
v.clear();
DFS(a,start,end,n,start,v);
}
else if(m.y>=320&&m.y<=400){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
vector<pair<int,int> > v;
v.clear();
R_DFS(a,start,end,n,start,v);
}
else if(m.y>=420&&m.y<=500){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
AStar(a,start,end,n);
}
else if(m.y>=520&&m.y<=600){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
AStar_2(a,start,end,n);
}
else if(m.y>=620&&m.y<=700){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
DIJ(a,start,end,n);
}
}
else if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start){
break;
}
if(a[yy][xx].no==1) a[yy][xx].no=0;
else a[yy][xx].no=1;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
Fill(a,n,n,length);
}
break;
case WM_RBUTTONDOWN:
if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
a[end.first][end.second].data=-1;
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start||a[yy][xx].no==1){
break;
}
a[end.first][end.second].end=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
end={yy,xx};
a[end.first][end.second].end=1;
Fill(a,n,n,length);
}
break;
case WM_MBUTTONDOWN:
if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start||a[yy][xx].no==1){
break;
}
a[start.first][start.second].start=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
start={yy,xx};
a[start.first][start.second].start=1;
Fill(a,n,n,length);
}
break;
}
}
}
最终代码
#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<windows.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
namespace IO{
#define ll long long
long long fast_pow(long long a,long long b,long long mod){
ll sum=1;
while(b){
if(b&1) sum=(sum*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return sum%mod;
}
ll fast_times(ll a,ll b,ll p){
ll ret=0;
while(b){
if(b&1) ret=(ret+a)%p;
b>>=1;
a=(a+a)%p;
}
return ret;
}
ll very_fast_times(ll a,ll b,ll m){
unsigned long long c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m;
if(c<m) return c;
return c+m;
}
ll inv(ll a,ll p){
return fast_pow(a,p-2,p);
}
#undef ll
}
using namespace IO;
class BigNum{
#define MAXN 9999
#define SIZE 10000
#define DLEN 4
private:
int a[5000],len;
bool neg;
public:
BigNum(){len=1;neg=0;memset(a,0,sizeof(a));}
BigNum(const int b){
int c,d=b;
if(d<0) neg=1,d=-d;
else neg=0;
len=0;
memset(a,0,sizeof(a));
while(d>MAXN){
c=d-(d/(MAXN+1))*(MAXN+1);
d=d/(MAXN+1);
a[len++]=c;
}
a[len++]=d;
}
BigNum(const long long b){
long long c,d=b;
if(d<0) neg=1,d=-d;
else neg=0;
len=0;
memset(a,0,sizeof(a));
while(d>MAXN){
c=d-(d/(MAXN+1))*(MAXN+1);
d=d/(MAXN+1);
a[len++]=c;
}
a[len++]=d;
}
BigNum(char* s){
memset(a,0,sizeof(a));
int l=strlen(s);
if(s[0]=='-'){
neg=1;
for(int i=0;i<l;i++) s[i]=s[i+1];
l--;
}
else neg=0;
len=l/DLEN;
if(l%DLEN) len++;
int idx=0;
for(int i=l-1;i>=0;i-=DLEN){
int t=0,k=i-DLEN+1;
if(k<0) k=0;
for(int j=k;j<=i;j++) t=t*10+s[j]-'0';
a[idx++]=t;
}
}
BigNum(string s){
memset(a,0,sizeof(a));
if(s[0]=='-'){
neg=1;
s=s.substr(1);
}
else neg=0;
int l=s.size();
len=l/DLEN;
if(l%DLEN) len++;
int idx=0;
for(int i=l-1;i>=0;i-=DLEN){
int t=0,k=i-DLEN+1;
if(k<0) k=0;
for(int j=k;j<=i;j++) t=t*10+s[j]-'0';
a[idx++]=t;
}
}
BigNum(const char c){
memset(a,0,sizeof(a));
neg=0;
len=1;
a[0]=c-'0';
}
BigNum(const BigNum &T):len(T.len){
neg=T.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=T.a[i];
}
friend istream& operator>>(istream& in,BigNum &b){
string ch;
in>>ch;
b=BigNum(ch);
return in;
}
friend ostream& operator<<(ostream& out,BigNum b){
if(b.neg) out<<"-";
out<<b.a[b.len-1];
for(int i=b.len-2;i>=0;i--){
out.width(DLEN);
out.fill('0');
out<<b.a[i];
}
return out;
}
BigNum& operator=(const BigNum& T){
len=T.len;
neg=T.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=T.a[i];
return *this;
}
template<typename T1>
BigNum& operator=(const T1& T){
BigNum tmp=BigNum(T);
len=tmp.len;
neg=tmp.neg;
memset(a,0,sizeof(a));
for(int i=0;i<len;i++) a[i]=tmp.a[i];
return *this;
}
bool operator>(const BigNum& T)const{
if(len>T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l>=0&&a[l]>T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator>(const T1& T)const{
BigNum t2(T);
return *this>t2;
}
bool operator<(const BigNum& T)const{
if(len<T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l>=0&&a[l]<T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator<(const T1& T)const{
BigNum t2(T);
return *this<t2;
}
bool operator>=(const BigNum T)const{
if(len>T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l<0) return true;
if(a[l]>=T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator>=(const T1 T)const{
BigNum t2(T);
return *this>=t2;
}
bool operator<=(const BigNum T)const{
if(len<T.len) return true;
if(len==T.len){
int l=len-1;
while(a[l]==T.a[l]&&l>=0) l--;
if(l<0) return true;
if(a[l]<=T.a[l]) return true;
return false;
}
return false;
}
template<typename T1>
bool operator<=(const T1 T)const{
BigNum t2(T);
return *this<=t2;
}
bool operator==(const BigNum T)const{
if(len!=T.len) return false;
for(int i=0;i<len;i++){
if(a[i]!=T.a[i]) return false;
}
return true;
}
template<typename T1>
bool operator==(const T1 T)const{
BigNum t(T);
return t==(*this);
}
BigNum operator+(const BigNum T){
BigNum t(*this);
int big=T.len>len?T.len:len;
for(int i=0;i<big;i++){
t.a[i]+=T.a[i];
if(t.a[i]>MAXN){
t.a[i+1]++;
t.a[i]-=MAXN+1;
}
}
if(t.a[big]!=0) t.len=big+1;
else t.len=big;
return t;
}
template<typename T1>
BigNum operator+(const T1 T){
BigNum t1(*this),t2(T);
return t1+t2;
}
BigNum operator-(const BigNum T){
BigNum t1,t2;
bool flag;
if(*this>T){
t1=*this,t2=T;
flag=0;
}
else{
t1=T,t2=*this;
flag=1;
}
int big=t1.len;
for(int i=0;i<big;i++){
if(t1.a[i]<t2.a[i]){
int j=i+1;
while(t1.a[j]==0) j++;
t1.a[j]--;
j--;
while(j>i) t1.a[j--]+=MAXN;
t1.a[i]+=MAXN+1-t2.a[i];
}
else t1.a[i]-=t2.a[i];
}
t1.len=big;
while(t1.a[t1.len-1]==0&&t1.len>1){
t1.len--;
big--;
}
if(flag) t1.neg^=1;
return t1;
}
template<typename T1>
BigNum operator-(const T1 T){
BigNum t1(*this),t2(T);
return t1-t2;
}
BigNum operator*(const BigNum &T)const{
BigNum ret;
int i,j;
for(i=0;i<len;i++){
int up=0;
for(j=0;j<T.len;j++){
int tmp=a[i]*T.a[j]+ret.a[i+j]+up;
if(tmp>MAXN){
int tmp1=tmp-tmp/(MAXN+1)*(MAXN+1);
up=tmp/(MAXN+1);
ret.a[i+j]=tmp1;
}
else{
up=0;
ret.a[i+j]=tmp;
}
}
if(up!=0) ret.a[i+j]+=up;
}
ret.len=i+j;
while(ret.a[ret.len-1]==0&&ret.len>1) ret.len--;
return ret;
}
template<typename T1>
BigNum operator*(const T1 T)const{
BigNum t1(*this),t2(T);
BigNum ans=t1*t2;
return ans;
}
BigNum operator/(const BigNum T){
BigNum remain,res;
for(int i=len-1;i>=0;i--){
remain=remain*SIZE+a[i];
int cnt=0;
while(remain>=T){
cnt++;
remain=remain-T;
}
res.a[i]=cnt;
}
res.len=len;
while(res.a[res.len-1]==0&&res.len>1) res.len--;
return res;
}
template<typename T1>
BigNum operator/(const T1 T){
BigNum t1(*this),t2(T);
return t1/t2;
}
BigNum operator%(const BigNum T){
BigNum t(*this);
return t-t/T*T;
}
template<typename T1>
BigNum operator%(const T1 T){
BigNum t1(*this),t2(T);
return t1%t2;
}
int size(){
int ans=DLEN*(len-1),tmp=a[len-1];
while(tmp){
ans++;
tmp/=10;
}
return ans;
}
int length(){return size();}
bool empty(){return len==1&&a[0]==0;}
void clear(){
len=1;
memset(a,0,sizeof(a));
}
BigNum gcd(BigNum a,BigNum b){
if(b.empty()) return a;
return gcd(b,a%b);
}
BigNum qpow(BigNum a,BigNum b){
BigNum ans=1;
while(!b.empty()){
if(b%2==1) ans=ans*a;
a=a*a;
b=b/2;
}
return ans;
}
BigNum qpow(BigNum a,BigNum b,BigNum mod){
BigNum ans=1;
while(!b.empty()){
if(b%2==1) ans=ans*a%mod;
a=a*a%mod;
b=b/2;
}
return ans%mod;
}
#undef MAXN
#undef DLEN
#undef SIZE
};
struct HASH{
int sed,mod,h[500006],pw[500006];
HASH(int ssed=128,int mmod=998244353){
sed=ssed,mod=mmod;
pw[0]=1;
for(int i=1; i<500001; i++) pw[i]=pw[i-1]*1ll*sed%mod;
}
void make(string s){
h[0]=s[0]%mod;
for(int i=1; i<s.size(); i++){
h[i]=(h[i-1]*1ll*sed%mod+s[i])%mod;
}
}
void make(char* s){
h[0]=s[0]%mod;
for(int i=1; s[i]; i++){
h[i]=(h[i-1]*1ll*sed%mod+s[i])%mod;
}
}
int get(int l,int r){
if(l==0) return h[r];
return (h[r]-h[l-1]*1ll*pw[r-l+1]%mod+mod)%mod;
}
int get(int r){
return h[r];
}
};
template<int length2>
class tree_array{
private:
long long sum[length2+5]={0};
size_t sum_size=0;
public:
long long lowbit(long long x){
return x&-x;
}
long long getsum(long long pos){
long long ans=0;
for(int i=pos; i;i-=this->lowbit(i)){
ans+=sum[i];
}
return ans;
}
long long mod_getsum(long long pos,long long mod){
long long ans=0;
for(int i=pos; i;i-=this->lowbit(i)){
ans+=sum[i];
ans%=mod;
}
return ans;
}
long long getsum(long long pos1,long long pos2){
return this->getsum(pos2)-this->getsum(pos1-1);
}
long long mod_getsum(long long pos1,long long pos2,long long mod){
return (this->getsum(pos2)-this->getsum(pos1-1))%mod;
}
void updata(long long pos,long long y){
sum_size++;
for(long long i=pos; i<=length2; i+=this->lowbit(i)) sum[i]+=y;
}
void erase(long long pos,long long y){
this->updata(pos,-y);
}
void replace(long long pos,long long y,long long a[]){
if(y>a[pos]) this->updata(pos,y-a[pos]);
else if(y<a[pos]) this->erase(pos,y-a[pos]);
}
size_t size(){
return sum_size;
}
};
struct Gird{
COLORREF color;
bool no;
bool start,end;
int data;
};
namespace Button{
void button(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn,TCHAR text[20]){
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle((rn.first-lu.first+1)/4+2, 0, _T("宋体"));
outtextxy(lu.first+(rn.first-lu.first)/4,lu.second+((rn.second-lu.second+1)/5)/2,text);
}
}
using namespace Button;
namespace CLR{
void Color(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn){
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
}
void Color2(COLORREF fill_color,COLORREF line_color,pair<int,int> lu,pair<int,int> rn,int data,int length){
setfillcolor(fill_color);
solidrectangle(lu.first,lu.second,rn.first,rn.second);
setlinecolor(line_color);
line(lu.first,lu.second,lu.first,rn.second);
line(lu.first,lu.second,rn.first,lu.second);
line(rn.first,lu.second,rn.first,rn.second);
line(lu.first,rn.second,rn.first,rn.second);
int k=(int)ceil(log10(data));
if(k<=0) k=1;
setbkmode(TRANSPARENT);
TCHAR text[20];
_stprintf(text,_T("%d"),data);
settextcolor(WHITE);
settextstyle(length/4+2, 0, _T("宋体"));
outtextxy(lu.first+length/4,lu.second+(length/5*4)/2,text);
}
void Fill(Gird a[105][105],int n,int m,int length){
for(int i=0,ii=1; i<=(n-1)*length; i+=length,ii++){
for(int j=300,jj=1; j<=300+(m-1)*length; j+=length,jj++){
if(a[ii][jj].no==1) Color(RED,WHITE,{j,i},{j+length,i+length});
else if(a[ii][jj].start==1) button(BLUE,WHITE,{j,i},{j+length,i+length},"起点");
else if(a[ii][jj].end==1) button(GREEN,WHITE,{j,i},{j+length,i+length},"终点");
else if(a[ii][jj].data==-1||a[ii][jj].data==0) Color(RGB(0,191,255),WHITE,{j,i},{j+length,i+length});
else Color2(a[ii][jj].color,WHITE,{j,i},{j+length,i+length},a[ii][jj].data,length);
}
}
}
};
using CLR::Fill;
int Speed;
using namespace Button;
namespace Pathfinding{
bool check(pair<int,int> start,pair<int,int> end){
if(start.first==0) return false;
if(end.first==0) return false;
return true;
}
vector<pair<int,int> > v[105][105];
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
void BFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
void DBFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q,pp,qq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
int f[105][105]={0};
f[start.first][start.second]=1;
f[end.first][end.second]=2;
p.push(start.first);
q.push(start.second);
pp.push(end.first);
qq.push(end.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
int xxx=pp.front();
pp.pop();
int yyy=qq.front();
qq.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]!=1){
if(f[xx][yy]==2){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].insert(v[xx][yy].begin(),v[x][y][j]);
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size(),{x,y});
v[xx][yy].insert(v[xx][yy].begin()+v[x][y].size()+1,{xx,yy});
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xx][yy].push_back({x,y});
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
for(int i=0;i<4;i++){
int xxxx=xxx+dx[i],yyyy=yyy+dy[i];
if(xxxx>=1&&xxxx<=n&&yyyy>=1&&yyyy<=n){
if(a[xxxx][yyyy].no==0&&f[xxxx][yyyy]!=2){
if(f[xxxx][yyyy]==1){
reverse(v[xxxx][yyyy].begin(),v[xxxx][yyyy].end());
v[xxxx][yyyy].push_back({xxxx,yyyy});
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=v[xxx][yyy].size()-1;j>-1; j--) v[xxxx][yyyy].push_back(v[xxx][yyy][v[xxx][yyy].size()-j-1]);
for(int i=0;i<v[xxxx][yyyy].size(); i++){
a[v[xxxx][yyyy][i].first][v[xxxx][yyyy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
v[xxxx][yyyy].push_back({xxx,yyy});
for(int j=0;j<v[xxx][yyy].size(); j++) v[xxxx][yyyy].push_back(v[xxx][yyy][j]);
a[xxxx][yyyy].data=v[xxxx][yyyy].size();
a[xxxx][yyyy].color=GREEN;
f[xxxx][yyyy]=2;
pp.push(xxxx);
qq.push(yyyy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
int DFSflag=0,DFSvis[105][105];
void DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) DFSflag=0,memset(DFSvis,0,sizeof DFSvis);
if(DFSflag==1) return;
DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
DFSflag=1;
return;
}
int x=now.first,y=now.second;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
DFS(a,start,end,n,{xx,yy},v);
if(DFSflag) return;
}
}
}
}
int R_DFSflag,R_DFSvis[105][105];
void R_DFS(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n,pair<int,int> now,vector<pair<int,int> > lujing){
if(check(start,end)==false) return;
if(now==start) R_DFSflag=0,memset(R_DFSvis,0,sizeof R_DFSvis),srand(time(NULL));
if(R_DFSflag==1) return;
R_DFSvis[now.first][now.second]=1;
if(now==end){
for(int i=0;i<lujing.size(); i++){
a[lujing[i].first][lujing[i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
R_DFSflag=1;
return;
}
int x=now.first,y=now.second;
int t[5]={0,1,2,3};
int i=rand()%4;
int i2=rand()%4;
while(i2==i) i2=rand()%4;
int i3=rand()%4;
while(i3==i||i3==i2) i3=rand()%4;
int i4=rand()%4;
while(i4==i||i4==i2||i4==i3) i4=rand()%4;
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i2;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i3;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
i=i4;
xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&R_DFSvis[xx][yy]==0){
vector<pair<int,int> > v=lujing;
v.push_back(now);
a[xx][yy].data=v.size();
a[xx][yy].color=BLUE;
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
R_DFS(a,start,end,n,{xx,yy},v);
if(R_DFSflag) return;
}
}
}
struct Node{
int x,y;
int F,G,H;
bool operator<(const Node &a)const{
return F==a.F?G>a.G:F>a.F;
}
};
int AStar_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return abs(x-x1)+abs(y-y1);
}
Node make_node(pair<int,int> now,int G,int H){
int F=G+H;
Node Temp;
Temp.x=now.first;
Temp.y=now.second;
Temp.F=F;
Temp.G=G;
Temp.H=H;
return Temp;
}
priority_queue<Node> NEW;
void AStar(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
int AStar_2_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return sqrt(pow(x-x1,2)+pow(y-y1,2));
}
void AStar_2(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
priority_queue<Node> pq;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
pq.push(make_node(start,0,AStar_2_js(start,end)));
while(!pq.empty()){
Node now=pq.top();
pq.pop();
int x=now.x;
int y=now.y;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
pq.push(make_node({xx,yy},a[xx][yy].data,AStar_2_js({xx,yy},end)));
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
void DIJ(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
if(check(start,end)==false) return;
queue<int> p,q;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
v[i][j].clear();
}
}
bool f[105][105]={0};
f[start.first][start.second]=1;
p.push(start.first);
q.push(start.second);
while(!q.empty()){
int x=p.front();
p.pop();
int y=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(a[xx][yy].no==0&&f[xx][yy]==0){
for(int j=0;j<v[x][y].size(); j++) v[xx][yy].push_back(v[x][y][j]);
v[xx][yy].push_back({x,y});
if(make_pair(xx,yy)==end){
for(int i=0;i<v[xx][yy].size(); i++){
a[v[xx][yy][i].first][v[xx][yy][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
return;
}
a[xx][yy].data=v[xx][yy].size();
a[xx][yy].color=BLUE;
f[xx][yy]=1;
p.push(xx);
q.push(yy);
Fill(a,n,n,825/n);
if(Speed<=1000) Sleep(1000/Speed);
}
}
}
}
}
};
using namespace Pathfinding;
Gird a[105][105];
int main(){
printf("请输入矩阵边长和速度:");
int n;
cin>>n>>Speed;
initgraph(1425,825);
setbkcolor(RGB(156,156,156));
int length=825/n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
a[i][j].data=-1;
a[i][j].no=a[i][j].end=a[i][j].start=0;
a[i][j].color=RGB(0,191,255);
}
}
Fill(a,n,n,length);
button(RED,BLACK,{40,20},{280,100},"BFS");
button(BLUE,BLACK,{40,120},{280,200},"D-BFS");
button(RGB(155,48,255),BLACK,{40,220},{280,300},"DFS");
button(RGB(255,69,0),BLACK,{40,320},{280,400},"R-DFS");
button(RGB(255,215,0),BLACK,{40,420},{280,500},"A*(1)");
button(RGB(255,0,255),BLACK,{40,520},{280,600},"A*(2)");
button(GREEN,BLACK,{40,620},{280,700},"DIJ");
button(GREEN,BLACK,{1150,20},{1400,100},"Clear");
pair<int,int> start,end;
start=end={0,0};
while(1){
if(MouseHit()){
MOUSEMSG m=GetMouseMsg();
switch(m.uMsg){
case WM_LBUTTONDOWN:
if(m.x>=1150&&m.x<=1400&&m.y>=20&&m.y<=100){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
a[i][j].data=-1;
a[i][j].no=a[i][j].end=a[i][j].start=0;
a[i][j].color=RGB(0,191,255);
}
}
start={0,0};
end={0,0};
Fill(a,n,n,length);
}
else if(m.x>=40&&m.x<=280){
if(m.y>=20&&m.y<=100){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
BFS(a,start,end,n);
}
else if(m.y>=120&&m.y<=200){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
DBFS(a,start,end,n);
}
else if(m.y>=220&&m.y<=300){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
vector<pair<int,int> > v;
v.clear();
DFS(a,start,end,n,start,v);
}
else if(m.y>=320&&m.y<=400){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
vector<pair<int,int> > v;
v.clear();
R_DFS(a,start,end,n,start,v);
}
else if(m.y>=420&&m.y<=500){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
AStar(a,start,end,n);
}
else if(m.y>=520&&m.y<=600){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
AStar_2(a,start,end,n);
}
else if(m.y>=620&&m.y<=700){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
DIJ(a,start,end,n);
}
}
else if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start){
break;
}
if(a[yy][xx].no==1) a[yy][xx].no=0;
else a[yy][xx].no=1;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
Fill(a,n,n,length);
}
break;
case WM_RBUTTONDOWN:
if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
a[end.first][end.second].data=-1;
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start||a[yy][xx].no==1){
break;
}
a[end.first][end.second].end=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
end={yy,xx};
a[end.first][end.second].end=1;
Fill(a,n,n,length);
}
break;
case WM_MBUTTONDOWN:
if(m.x>=300&&m.x<=300+n*length&&m.y>=0&&m.y<=n*length){
Fill(a,n,n,length);
int xx=(m.x-300)/length+1,yy=m.y/length+1;
if(make_pair(yy,xx)==end||make_pair(yy,xx)==start||a[yy][xx].no==1){
break;
}
a[start.first][start.second].start=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int k=a[i][j].no+a[i][j].start+a[i][j].end;
if(k==0) a[i][j].data=-1,a[i][j].color=RGB(0,191,255);
}
}
start={yy,xx};
a[start.first][start.second].start=1;
Fill(a,n,n,length);
}
break;
}
}
}
return 0;
}