现在已知有只BT马,它的跳跃方式就两种,假设当前坐标为(x,y),则这只BT马只可以跳跃到(x+n,y+n+1)或(x+n+1,y+n)这两个位置,其中n > 0。现在问题来了,设BT马的起始位置为(0,0),给出坐标(x,y),问BT马是否能通过有限次跳跃到达(x,y)这个位置。
Input
第一行两个正整数x,y(0 < x, y <= 10000),相隔一个空格。
第二行一个正整数n(0 < n <= 10000)。
Output
如果可以到达输出Yes,反之,No。
Sample Input
3 3 1
Sample Output
Yes
Hint
n = 1 说明该BT马可以从当前坐标(x,y) 跳到 (x+1,y+2) 或 (x+2,y+1) 这两个位置
需要两步从(0,0) 跳到 (3,3):
第一种跳法:(0,0) --> (1,2) --> (3,3)
第二种跳法:(0,0) --> (2,1) --> (3,3)
======================================================================================
附三种解法:
//广搜
#include<stdio.h>
int head,tail;
struct f{
int x,y;
}a[100010];
int next[2][2];
int g(int x0,int y0)
{
for(int i=head;i<tail;i++)
if(a[i].x==x0&&a[i].y==y0) return 0;
return 1;
}
int main(){
int x,y,n;
while(~scanf("%d%d%d",&x,&y,&n)){
head=0,tail=1;
next[0][0]=next[1][1]=n;
next[0][1]=next[1][0]=n+1;
a[head].x=a[head].y=0;
int flag=0;
while(head<tail){ //终止循环的条件一
for(int i=0;i<2;i++)
{
int fx=a[head].x+next[i][0],fy=a[head].y+next[i][1];
if(fx==x&&fy==y){flag=1;break;} //终止循环的条件二
if(fx>x||fy>y) continue; //条件限制否则RE
if(g(fx,fy)){a[tail].x=fx;a[tail++].y=fy;} //去重否则RE
}
if(flag) break;
head++;
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}
//画张大图将马能走的点都标出来(不附图了),然后就可按规律解答如下:
#include<stdio.h>
int main(){
int x,y,n;
scanf("%d%d%d",&x,&y,&n);
int k=0; //k是每个起点之后会有几个数(除去起点)
while(1){
int tx=k*n,ty=k*(n+1); //枚举每个起点坐标并判断
if(tx==x&&ty==y) {printf("Yes\n");return 0;}
if(tx>=x&&ty>=y) {printf("No\n");return 0;}
for(int i=1;i<=k;i++) //枚举起点之后的k个点并判断
{
tx+=1,ty-=1;
if(tx==x&&ty==y) {printf("Yes\n");return 0;}
if(tx>=x&&ty>=y) {printf("No\n");return 0;}
}
k++;
}
}
//ps:代码较简单
//(找的)
#include<stdio.h>
#include<string.h>
struct step
{
int x;//横
int y;//纵
int stepNum; //步数
};
struct step que[100];
int main()
{
int x, y, n;
int next[2][2]= {{0, 1}, {1, 0}};
int first = 1, last = 1;
int i, tx, ty, tStepNum;
int book[10][10];//ps:放主函数外竟RE...
memset(book,0,sizeof(book));
scanf("%d %d", &x, &y);
scanf("%d", &n);
if (n > x || n > y)
{
printf("No\n");
return 0;
}
que[last].x = 0;
que[last].y = 0;
que[last].stepNum = 0;
last++;
book[0][0] = 1;
while(first < last)
{
for(i = 0; i < 2; i++)
{
tx = que[first].x + next[i][0];
ty = que[first].y + next[i][1];
tStepNum = que[first].stepNum + 1;
if(book[tx][ty] == 0 && tx + tStepNum * n < x && ty + tStepNum * n < y)
{
book[tx][ty] = 1;
que[last].x = tx;
que[last].y = ty;
que[last].stepNum = tStepNum;
last++;
}
if(tx + tStepNum * n == x && ty + tStepNum * n == y)
{
printf("Yes\n");
return 0;
}
}
first++;
}
printf("No\n");
return 0;
}
//将空间缩到10*10,队列相应到100;