大连大学2022年4月程序设计竞赛-Raksasa的棋局

 

思路:多次查询的题应该打表

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
typedef long long ll;
using namespace std;
struct yuu{
    int x,y,time;
};
queue<yuu> op;
int meme[1009][1009];
bool vis[1009][1009];
int fx[8][2]={1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
int x,y;
void bfs(yuu m)
{
    if(m.x<1||m.x>x||m.y<1||m.y>y||vis[m.x][m.y])
    return ;
    vis[m.x][m.y]=1;
    meme[m.x][m.y]=m.time;
    op.push(m);
}
int main()
{
    int time,x1,y2;
    cin>>x>>y>>time;
    cin>>x1>>y2;
    vis[x1][y2]=true;
    meme[x1][y2]=0;
    yuu beg;beg.x=x1,beg.y=y2,beg.time=0;
    op.push(beg);
    while(!op.empty())
    {
        beg=op.front();
        op.pop();
        for(int i=0;i<8;i++)
        {
            yuu ne;ne.x=beg.x+fx[i][0],ne.y=beg.y+fx[i][1],ne.time=beg.time+1;
            bfs(ne);
        }
    }
    while(time--)
    {
        int x3,y3;
        cin>>x3>>y3;
        if(vis[x3][y3])
        cout<<meme[x3][y3]<<endl;
        else
        cout<<-1<<endl;
    }
    return 0;
}

 

posted on 2022-04-16 23:12  zesure  阅读(41)  评论(0)    收藏  举报

导航