220 DIV2 A. Inna and Pink Pony
2017-11-08 14:15
369 查看
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万,肯定超时了。
还是思想题,注意:判断边界
View Code
输入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万,肯定超时了。
还是思想题,注意:判断边界
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
- 220 DIV2 B. Inna and Nine
- 【打CF,学算法——二星级】CodeForces 374A Inna and Pink Pony (水题/坑题)
- cf A. Inna and Pink Pony(思维题)
- 220 DIV2 B. Inna and Nine
- CF #220 Inna and Pink Pony
- Codeforces 374A - Inna and Pink Pony
- CF:374A - Inna and Pink Pony(思想题)
- CodeForces 374 A. Inna and Pink Pony
- Codeforces Round #220 (Div. 2) A. Inna and Pink Pony 这个题目不简单的
- Codeforces Round #220 (Div. 2)C题:Inna and Dima(记忆化搜索+DP)
- CodeForces 374A Inna and Pink Pony
- A. Inna and Pink Pony----暴力
- codeforces 374A Inna and Pink Pony(数学)
- A. Inna and Pink Pony
- codeforces#234_div2_B Inna and New Matrix of Candies
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum
- Codeforces Div. 2 #259-A. Little Pony and Crystal Mine
- codeforces round #234B(DIV2) A Inna and Choose Options