首页 > 其他 > 详细

220 DIV2 A. Inna and Pink Pony

时间:2014-11-28 19:53:57      阅读:355      评论:0      收藏:0      [点我收藏+]

Inna and Pink Pony

输入n,m,i,j,a,b

可以看成n行m列的矩阵,起点(i,j),每次移动(a,b),(-a,-b),(-a,b),(a,-b)

可移动到(1,m),(n,1),(n,m),(1,1)四个方向

如果可以移动到这四个点中的一个或多个。

输出到其中一个点的最短距离

如果不能输出 Poor Inna and pony!

input

5 7 1 3 2 2

output

2

input

5 5 2 3 1 1

Poor Inna and pony!

PS:开始想成BFS了,一看100万,肯定超时了。

还是思想题,注意:判断边界

bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<queue>
 6 #include<limits.h>
 7 using namespace std;
 8 int n,m,i,j,a,b;
 9 
10 int check(int x,int y)
11 {
12     int fx,fy;
13 
14     if(x==i && y==j)
15     return 0;
16     if(i+a>n&&i-a<1 || j+b>m&&j-b<1)//边界
17     return INT_MAX;
18     fx=abs(x-i);
19     fy=abs(y-j);
20     if(fx%a!=0 || fy%b!=0)//同为整数步
21     return INT_MAX;
22     fx=fx/a;//得到步数
23     fy=fy/b;//得到步数
24     int fz;
25     fz=abs(fx-fy);
26     if(fz%2==0)//同奇同偶
27     {
28         fz=max(fx,fy);
29         return fz;
30     }
31     else
32     return INT_MAX;
33 }
34 int main()
35 {
36     while(~scanf("%d%d%d%d%d%d",&n,&m,&i,&j,&a,&b))
37     {
38         int count=INT_MAX;
39         count=min(check(1,1),check(1,m));
40         count=min(count,check(n,1));
41         count=min(count,check(n,m));
42         if(count==INT_MAX)
43         printf("Poor Inna and pony!\n");
44         else
45         printf("%d\n",count);
46     }
47     return 0;
48 }
View Code

 

 

220 DIV2 A. Inna and Pink Pony

原文:http://www.cnblogs.com/xuesen1995/p/4129218.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!