GCPC 2013 C(BFS+输出路径)

题意:给出起点和终点,棋盘有两种颜色黑和白,跳的归则是只能跳到相同的颜色格子,问能跳到终到吗,能则输出跳的路径。

补题: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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值