hiho 1599 dfs乱搞 [Offer收割]编程练习赛29 Problem A 逃离迷宫4

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫。

小Hi一开始处于迷宫(x, y)的位置,迷宫的出口在(a, b)。小Hi发现迷宫被女巫施加了魔法,假设当前他处在(x, y)的位置,那么他只能移动到(x+y, y)或者(x, x+y)的位置上。

小Hi想知道自己能不能逃离迷宫。

输入

第一行包含一个整数T,代表测试数据的组数。  

以下N行每行包含4个整数x, y, a, b,表示起点在(x, y),出口在(a, b)。  

对于30%的数据,1 ≤ T ≤ 10, 1 ≤ x, y, a, b ≤ 100  

对于80%的数据,1 ≤ T ≤ 1000, 1 ≤ x, y, a, b ≤ 1000  

对于100%的数据,1 ≤ T ≤ 10000, 1 ≤ x, y, a, b ≤ 109

输出

对于每组数据输出一行YES或者NO,表示小Hi是否能逃离迷宫。

样例输入
2  
1 1 8 13  
2 2 100 101
样例输出
YES  
NO

题解:首先,暴力是过不了的,遇到一个10亿和1,会爆栈,并且TLE。

我的思路是不停的让A和B中大的减去小的,在这个过程中,如果A与目标的X对应相等了,那么就判断一下Y和B是否对A同余就可以了,就不用一直暴力的减了。另外,如果A和B没有跟X和Y相等的,那么直接MOD一下就行,很快的。


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
int T;
int x,y,a,b;

bool dfs(int X,int Y,int A,int B){
    if(A<X||B<Y||A<1||B<1)return 0;
    if(X==A&&Y==B)return 1;
    if(A==B){
        if(A==X||B==Y)return 1;
    }
    if(A>B){
        if(B==Y){
            if(A%B==X%B){
                return 1;
            }
        }
        else{
            if(dfs(X,Y,A%B,B))return 1;
        }

    }
    if(A<B){
        if(A==X){
            if(B%A==Y%A){
                return 1;
            }
        }
        else{
            if(dfs(X,Y,A,B%A))return 1;
        }
    }

    return 0;
}

int main(){
    cin>>T;
    while(T--){
        cin>>x>>y>>a>>b;
        if (dfs(x,y,a,b)){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }

    return 0;
}



//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//
//








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值