CF:374A - Inna and Pink Pony(思想题)

Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n × m chessboard, a very tasty candy and two numbers a and b.

Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:

  • move the candy from position (x, y) on the board to position (x - a, y - b);
  • move the candy from position (x, y) on the board to position (x + a, y - b);
  • move the candy from position (x, y) on the board to position (x - a, y + b);
  • move the candy from position (x, y) on the board to position (x + a, y + b).

Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.

Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!

Input

The first line of the input contains six integers n, m, i, j, a, b (1 ≤ n, m ≤ 106; 1 ≤ i ≤ n; 1 ≤ j ≤ m; 1 ≤ a, b ≤ 106).

You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m)(n, 1)(n, m)(1, 1).

Output

In a single line print a single integer — the minimum number of moves needed to get the candy.

If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.

Sample test(s)
input
5 7 1 3 2 2
output
2
input
5 5 2 3 1 1
output
Poor Inna and pony!
Note

Note to sample 1:

Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.


思路:唉 昨天做题的时候想成了BFS,然后直接T了,确实,数据都达到10^6了,不BFS不T才怪呢……实在是不该啊……刚才又想了一下,唉……真是思想题……

输入n,m,i,j,a,b。有图去考虑的话就知道是思想题了。因为如果那点左上方、右下方、右上方、左下方其与边界横坐标、纵坐标的距离如果都可以整除a,b的话,那说明这肯定可以达到其边界的其中一个顶点的……唉,这想想就简单了好多……可能是上周因为专攻BFS,所以就一直想用BFS,怎么都不想了,结果T了之后,才明白不能乱来啊,还以为这次比赛很难呢,其实是自己练得太少了。思想题真得多练练了……

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#define mem(a,b) memset(a,b,sizeof(a))
#define M 1000005
typedef long long ll;
using namespace std;
int n,m,i,j,a,b,ans=1<<30;
int abc(int x,int y)
{
    int x1=x%a;
    int y1=y%b;  
    if(!x1&&!y1)   //如果都整除
    {
        int p=abs(x/a-y/b);   //并且其次数之差为偶数才行
        if(!(p&1)) return min(max(x/a,y/b),ans);   //因为次数不相等的话,那大的那个得先变为与次数小的一样才行,变的过程借助a或者b,故偶数时才得正常转化
    }
    return ans;
}
int main()
{
    cin>>n>>m>>i>>j>>a>>b;
    if(i==1&&j==1||i==n&&j==1||i==1&&j==m||i==n&&j==m)
        printf("0\n");
    else
    {
        if(i-1>0&&j-1>0) ans=abc(i-1,j-1);
        if(i-1>0&&m-j>0) ans=abc(i-1,m-j);  //如果不在边界正常判断,调用判断函数
        if(n-i>0&&j-1>0) ans=abc(n-i,j-1);
        if(n-i>0&&m-j>0) ans=abc(n-i,m-j);
        if((i+a<=n&&i==1&&!((j-1)%b)||n-a>0&&i==n&&!((j-1)%b))&&((j-1)/b)%2==0) ans=min((j-1)/b,ans);  //如果在边界可以直接判断,符合的话,次数会更少
        if((i+a<=n&&i==1&&!((m-j)%b)||n-a>0&&i==n&&!((m-j)%b))&&((m-j)/b)%2==0) ans=min((m-j)/b,ans);  //但是在边界因为要借助a或者b
        if((j+b<=m&&j==1&&!((i-1)%a)||m-b>0&&j==m&&!((i-1)%a))&&((i-1)/a)%2==0) ans=min((i-1)/a,ans);  //所以需要判断i+a,n-i,j+b,m-j不能超过边界
        if((j+b<=m&&j==1&&!((n-i)%a)||m-b>0&&j==m&&!((n-i)%a))&&((n-i)/a)%2==0) ans=min((n-i)/a,ans);
        if(ans==(1<<30)) printf("Poor Inna and pony!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
下面是比赛的时候写的BFS解法:超时了,哈哈

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<cmath>  
#include<set>  
#include<map>  
#include<queue>  
#include<vector>  
#include<stack>  
#include<ctime>  
#include<cstdlib>  
#define mem(a,b) memset(a,b,sizeof(a))  
#define M 1000005  
typedef long long ll;  
using namespace std;  
int n,m,i,j,a,b;   
struct abc  
{  
    int x,y,t;  
};  
queue<abc>q;  
multimap<int,int>w;  
multimap<int,int>::iterator it;  
int bfs()  
{  
    abc p;  
    p.x=i;  
    p.y=j;  
    p.t=0;  
    q.push(p);  
    while(!q.empty())  
    {  
        abc ans,tem;  
        ans=q.front();  
        q.pop();  
        if(ans.x==1&&ans.y==m||ans.x==1&&ans.y==1||ans.x==n&&ans.y==1||ans.x==n&&ans.y==m)  
                return ans.t;  
        int a1,b1;  
        for(int i=0; i<4; i++)  
        {  
            if(i==0)  
            {  
                a1=-a;  
                b1=-b;  
            }  
            else if(i==1)  
            {  
                a1=-a;  
                b1=b;  
            }  
            else if(i==2)  
            {  
                a1=a;  
                b1=-b;  
            }  
            else  
            {  
                a1=a;  
                b1=b;  
            }  
            tem.x=ans.x+a1;  
            tem.y=ans.y+b1;  
            if(tem.x>=1&&tem.y>=1&&tem.x<=n&&tem.y<=m)  
            {  
                it=w.find(tem.x);  
                if(it==w.end()||it->second!=tem.y)  
                {  
                    tem.t=ans.t+1;  
                    q.push(tem);  
                    pair<int,int> p1(tem.x,tem.y);  
                    w.insert(p1);  
                }  
            }  
            if(tem.x==1&&tem.y==m||tem.x==1&&tem.y==1||tem.x==n&&tem.y==1||tem.x==n&&tem.y==m)  
                return tem.t;  
        }  
    }  
    return -1;  
}  
int main()  
{  
    cin>>n>>m>>i>>j>>a>>b;  
    int sum=bfs();  
    if(sum==-1) printf("Poor Inna and pony!\n");  
    else printf("%d\n",sum);  
    return 0;  
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值