您的位置:首页 > 其它

bzoj1193: [HNOI2006]马步距离

2014-07-10 14:35 197 查看
bfs打表,在范围之外一直走斜线

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int dx[]={0,1,-1,1,-1,2,-2,2,-2};
int dy[]={0,2,2,-2,-2,1,1,-1,-1};
int dis[22][22];
int vis[22][22];
bool check(int x,int y)
{
if(x>0&&x<=21&&y>0&&y<=21)
return true;
return false;
}
struct node
{
int x,y;
};
void bfs()
{
queue<node>q;
node st,cur,tmp;
int xx,yy;
st.x=11;
st.y=11;
vis[st.x][st.y]=1;
q.push(st);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=1;i<=8;i++)
{
xx=cur.x+dx[i];
yy=cur.y+dy[i];
if(!check(xx,yy)) continue;
if(!vis[xx][yy])
{
vis[xx][yy]=vis[cur.x][cur.y]+1;
tmp.x=xx;
tmp.y=yy;
q.push(tmp);
}
}
}
}

int main()
{

int x1,x2,y1,y2,x,y,ans=0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x=x1-x2; x=abs(x); y=y1-y2; y=abs(y);
while ((x>=10)||(y>=10))
{
if (x>y) x-=2,y-=1;
else x-=1,y-=2;
ans++;
x=abs(x);
y=abs(y);
}

bfs();

for(int i=1;i<=11;i++)
{
for(int j=1;j<=11;j++)
{
dis[i][j]=vis[i+10][j+10]-1;
}
}
x++; y++;
printf("%d\n",ans+dis[x][y]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: