Knight Moves POJ - 1915(双向广度优先搜索)

算法思路

从起点和终点分别开始bfs

while(!q1.empty()||!q2.empty()){
   if(!q1.empty()){
       node e=q1.front();
       q1.pop()
       while(对其的一个临界点){
          if(在q2中出现过)搜索完毕,return 两个bfs的步数之和
          else if(没有在q1中出现过)入队}
}
if(!q2.empty(){
  操作与前面相同……
}
}

题目

https://vjudge.net/problem/POJ-1915

代码

#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn=310;
int vis[maxn][maxn];
int step[maxn][maxn];
int n;
struct node{
int x,y;
};
queueq1;
queueq2;
int dx[]={2,2,-2,-2,1,1,-1,-1};
int dy[]={1,-1,1,-1,-2,2,2,-2};
bool check(node a){
  if(a.x>=0&&a.x=0&&a.y>tt;
   while(cin>>n){
    memset(vis,0,sizeof(vis));
    //memset(step,0,sizeof(step));
    node s;node t;
    cin>>s.x>>s.y>>t.x>>t.y;
    if(s.x==t.x&&s.y==t.y) cout<<0<

你可能感兴趣的:(Knight Moves POJ - 1915(双向广度优先搜索))