题意:给出起点和终点,棋盘有两种颜色黑和白,跳的归则是只能跳到相同的颜色格子,问能跳到终到吗,能则输出跳的路径。
补题:CSU-1459
思路:与普通的BFS有点不一样,无非就是每一个方向可以走多格,所以再加个for循环枚举一下次数就好,这题补题AC不了,可能是测评机出问题了,,我用的标程也AC不了……
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
struct point
{
int x,y,step,fro,id;
};
point s,e;
int dx[]= {-1,1,-1,1};
int dy[]= {1,1,-1,-1};
int vis[10][10],color,tot;
stack<point> ans;
int bfs()
{
point all[100];
queue<point> q;
while(!ans.empty())
{
ans.pop();
}
q.push(s);
vis[s.x][s.y]=1;
tot=0;
all[0]=s;
while(!q.empty())
{
point f=q.front();
q.pop();
if(f.x==e.x&&f.y==e.y)
{
// point tmp;
// tmp.x=x,tmp.y=y,tmp.step=f.step+1,tmp.fro=f.id;
e.step=f.step;
ans.push(e);
while(f.fro>=0)
{
ans.push(f);
f=all[f.fro];
}
ans.push(s);
return 1;
}
for(int i=0; i<4; i++)
{
for(int j=1; j<=8; j++)
{
int x=f.x+dx[i]*j;
int y=f.y+dy[i]*j;
if(x>0&&x<=8&&y>0&&y<=8&&!vis[x][y])
{
if(color==(x+y)%2)
{
if(x==e.x&&y==e.y)
{
// point tmp;
// tmp.x=x,tmp.y=y,tmp.step=f.step+1,tmp.fro=f.id;
e.step=f.step+1;
ans.push(e);
while(f.fro>=0)
{
ans.push(f);
f=all[f.fro];
}
ans.push(s);
return 1;
}
else
{
++tot;
point tmp;
tmp.x=x,tmp.y=y,tmp.step=f.step+1,tmp.fro=f.id,tmp.id=tot;
if(tmp.step<5)
{
q.push(tmp);
all[tot]=tmp;
}
vis[x][y]=1;
}
}
}
}
}
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--)
{
memset(vis,0,sizeof(vis));
char a,b;
int c,d;
cin>>a>>c>>b>>d;
s.x=9-c,s.y=a-'A'+1,s.step=0,s.fro=-1,s.id=0;
e.x=9-d,e.y=b-'A'+1;
color=(s.x+s.y)%2;
if(color!=(e.x+e.y)%2)
{
cout<<"Impossible"<<endl;
}
else if(s.x==e.x&&s.y==e.y)
{
cout<<"0 "<<a<<" "<<c<<endl;
}
else
{
if(bfs())
{
cout<<e.step<<" ";
// cout<<ans.size()-1<<" ";
while(!ans.empty())
{
point t=ans.top();
ans.pop();
cout<<char(t.y+'A'-1)<<" "<<9-t.x<<" ";
}
cout<<endl;
}
else
{
cout<<"Impossible"<<endl;
}
}
}
return 0;
}