1 条题解
-
0
C++ :
#include <cstdlib> #include <cstring> #include <iostream> using namespace std; int dx[12]={-2,-2,-1,1,2,2,2,2,1,-1,-2,-2}, dy[12]={-1,-2,-2,-2,-2,-1,1,2,2,2,2,1}; int main() { int s[101][101],que[10000][4]={0},x1,y1,x2,y2; memset(s,0xff,sizeof(s)); //s数组的初始化 int head=1,tail=1; //初始位置入队 que[1][1]=1;que[1][2]=1;que[1][3]=0; cin>>x1>>y1>>x2>>y2; //读入黑马和白马的出发位置 while(head<=tail) //若队列非空,则扩展队首结点 { for(int d=0;d<=11;++d) //枚举12个扩展方向 { int x=que[head][1]+dx[d]; //计算马按d方向跳跃后的位置 int y=que[head][2]+dy[d]; if(x>0&&y>0) if(s[x][y]==-1) //若(x,y)满足约束条件 { s[x][y]=que[head][3]+1; //计算(1,1)到(x,y)的最少步数 tail=tail+1; //(1,1)至(x,y)的最少步数入队 que[tail][1]=x; que[tail][2]=y; que[tail][3]=s[x][y]; if(s[x1][y1]>0&&s[x2][y2]>0) //输出问题的解 { cout<<s[x1][y1]<<endl; cout<<s[x2][y2]<<endl; //system("pause"); return 0; } } } ++head; } }
- 1
信息
- ID
- 1042
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者