跳马问题

#include<stdio.h>

#include<iostream>

#include<queue>

using namespace std;

int x1,y1,x2,y2;

int num=0;

int a=0;

int s1,s2;

int map[203][203]={0};

int end[1001];

queue <int> x;

queue <int> y;

void input();

void bfs();

void output();

void clean();

int main()

{

int i;

cin>>num;

for(i=0;i<num;i++)

{

cin>>x1>>y1>>x2>>y2;

 
x.push(x1);

 
y.push(y1);

 
s1=x1;

 
s2=y1;

 

 
bfs();

 
clean();

}

for(i=0;i<num;i++)

{

cout<<end[i]<<endl;

}

return 0;

}

void clean()

{

int i,j;

for(i=0;i<=200;i++)

{

for(j=0;j<=200;j++)

{

map[i][j] = 0;

}

}

while(!x.empty())

{

x.pop();

}

while(!y.empty())

{

y.pop();

}

}

void bfs()

{

int i,j;

int flag=1;

while(flag == 1)

{

if(map[x2][y2] != 0)

{

end[a]=map[x2][y2];

a++;

break;

}

x1=x.front();

x.pop();

y1=y.front();

y.pop();

if(map[x1+1][y1+2] == 0 && x1+1<=200&&y1+2<=200)

{

x.push(x1+1);

y.push(y1+2);

map[x1+1][y1+2]=map[x1][y1]+1;

}

if(map[x1+1][y1-2] == 0&& x1+1<=200&&y1-2>=0)

{

x.push(x1+1);

y.push(y1-2);

map[x1+1][y1-2]=map[x1][y1]+1;

}

if(map[x1+2][y1+1] == 0&& x1+2<=200&&y1+1<=200)

{

x.push(x1+2);

y.push(y1+1);

map[x1+2][y1+1]=map[x1][y1]+1;

}

if(map[x1+2][y1-1] == 0&& x1+2<=200&&y1-1>=0)

{

x.push(x1+2);

y.push(y1-1);

map[x1+2][y1-1]=map[x1][y1]+1;

}

if(map[x1-1][y1+2] == 0&& x1-1>=0&&y1+2<=200)

{

x.push(x1-1);

y.push(y1+2);

map[x1-1][y1+2]=map[x1][y1]+1;

}

if(map[x1-1][y1-2] == 0&& x1-1>=0&&y1-2>=0)

{

x.push(x1-1);

y.push(y1-2);

map[x1-1][y1-2]=map[x1][y1]+1;

}

if(map[x1-2][y1+1] == 0&& x1-2>=0&&y1+1<=200)

{

x.push(x1-2);

y.push(y1+1);

map[x1-2][y1+1]=map[x1][y1]+1;

}

if(map[x1-2][y1-1] == 0&& x1-2>=0&&y1-1>=0)

{

x.push(x1-2);

y.push(y1-1);

map[x1-2][y1-1]=map[x1][y1]+1;

}

if(x1 ==s1&&y1==s2)

{

map[s1][s2]=1;

}

}

 

}

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/zym339/article/details/78468640
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞