首页 > 其他分享 >Codeforces Round 859 (Div

Codeforces Round 859 (Div

时间:2023-03-22 21:22:20浏览次数:56  
标签:int Codeforces dfs 859 nx ny Div else dir

F. Bouncy Ball

给定\(n×m\)矩形,起点\(st\),终点\(ed\),有一小球从起点出发,每次可以选择4个方向,如果碰到边界就反弹,询问最后能否到达终点

题解:\(DFS\) + \(map\)记录状态

按照题意\(dfs\)模拟分类讨论即可,但是我们这边说一下什么情况下不会到达终点,也就是我们到达了以前被遍历过的状态,注意这边的状态包括了坐标和方向,所以我们可以用\(map\)记录状态是否被遍历

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e4 + 10, M = 4e5 + 10;

int n, m, sx, sy, ex, ey;
string dir;
int ans; // dir 1: 1 1 , 2:1 -1 ,3:-1 +1,4:-1 -1
bool flag;
bool f;
map<array<int, 3>, int> mp;
int cnt;

void dfs(int dir, int x, int y)
{
    if (flag)
        return;
    if (f == false)
        return;
    if (x == ex && y == ey)
    {
        flag = true;
        return;
    }
    if (mp[{x, y, dir}])
    {
        f = false;
        return;
    }
    mp[{x, y, dir}]++;
    if (dir == 1)
    {
        int nx = x + 1, ny = y + 1;
        if (nx <= n && ny <= m)
            dfs(1, nx, ny);
        else if (nx > n && ny <= m)
        {
            ans++;
            dfs(3, x - 1, ny);
        }
        else if (nx <= n && ny > m)
        {
            ans++;
            dfs(2, nx, y - 1);
        }
        else
        {
            ans++;
            dfs(4, x - 1, y - 1);
        }
    }
    else if (dir == 2)
    {
        int nx = x + 1, ny = y - 1;
        if (nx <= n && ny >= 1)
            dfs(2, nx, ny);
        else if (nx > n && ny >= 1)
        {
            ans++;
            dfs(4, x - 1, ny);
        }
        else if (nx <= n && ny < 1)
        {
            ans++;
            dfs(1, nx, y + 1);
        }
        else
        {
            ans++;
            dfs(3, x - 1, y + 1);
        }
    }
    else if (dir == 3)
    {
        int nx = x - 1, ny = y + 1;
        if (nx >= 1 && ny <= m)
            dfs(3, nx, ny);
        else if (nx < 1 && ny <= m)
        {
            ans++;
            dfs(1, x + 1, ny);
        }
        else if (nx >= 1 && ny > m)
        {
            ans++;
            dfs(4, nx, y - 1);
        }
        else
        {
            ans++;
            dfs(2, x + 1, y - 1);
        }
    }
    else if (dir == 4)
    {
        int nx = x - 1, ny = y - 1;
        if (nx >= 1 && ny >= 1)
            dfs(4, nx, ny);
        else if (nx < 1 && ny >= 1)
        {
            ans++;
            dfs(2, x + 1, ny);
        }
        else if (nx >= 1 && ny < 1)
        {
            ans++;
            dfs(3, nx, y + 1);
        }
        else
        {
            ans++;
            dfs(1, x + 1, y + 1);
        }
    }
}

void solve()
{
    ans = 0;
    cnt = 0;
    flag = false;
    f = true;
    cin >> n >> m >> sx >> sy >> ex >> ey;
    cin >> dir;
    mp.clear();
    if (dir == "DL")
        dfs(2, sx, sy);
    else if (dir == "DR")
        dfs(1, sx, sy);
    else if (dir == "UR")
        dfs(3, sx, sy);
    else
        dfs(4, sx, sy);
    if (flag == true)
        cout << ans << endl;
    else
        cout << -1 << endl;
}
signed main(void)
{
    Zeoy;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

标签:int,Codeforces,dfs,859,nx,ny,Div,else,dir
From: https://www.cnblogs.com/Zeoy-kkk/p/17245493.html

相关文章

  • Edu Round 板刷计划 1. Educational Codeforces Round 1 题解
    ChangeLog:2023.03.21开坑。A-TrickySum简单题。注意到\(n\)以内\(2\)的幂次只有\(O(\logn)\)个,因此只要先算出\(1\)~\(n\)里所有数的和再减去\(2\)......
  • Codeforces Round 368 (Div. 2) D. Persistent Bookcase 主席树维护bitset
    在学主席树时找到了这道题本来yyyy了一个二维的主席树这种东西,然后发现很多信息好像维护不了观察到n和m都很小,考虑把一整行看成一个节点,开一个bitset然后区间取反、单点......
  • Codeforces Round 857 (Div. 2) C-The Very Beautiful Blanket
    题目地址题意:构造一个二维数组,使得任意一个4*4的子矩阵满足:A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42Solution(思路来源:知乎xioac......
  • 整个div居中,不是div中的内容居中
    首先,在jsp前面加入<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">......
  • Codeforces Round 859 (Div. 4)
    A.PlusorMinus#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);i......
  • CF 1368B Codeforces Subsequences
    题目地址题意:给你一个数n,构造一个字符串,使得至少有n个子串为codeforcesSolution用贪心的思想肯定是只在codeforces基础上修改对于每个字符,对答案的贡献都是乘以字符的......
  • CF855 Div3 VP 游记
    比赛链接好长时间不写博文了甚至快忘记了(今天水一发Div3游记,在Div4比赛之前。第一次VP,当然得选一个简单点的了,打了50分钟多一点。排名不错,400多。$T1$:开始时......
  • Codeforces Round 858 (Div. 2)
    Preface这场CF打的好难受啊,A刚开始忘记切换语言了CE了两发,B漏了一种情况挂了一发,C纯靠暴力找性质,D比赛时没人写我题目都没仔细看然后E本来秒出了解法,结果就是因为unorder......
  • Educational Codeforces Round 115 (Rated for Div. 2)(D,E)
    EducationalCodeforcesRound115(RatedforDiv.2)(D,E)DD题目给出\(n\)个问题,每个问题都会有一个主题和一个难度,并且没有两个题目的问题和主题都是一样的,我们需要选......
  • Codeforces 87D Beautiful Road
    Codeforces87DBeautifulRoadCF传送门Description​ 给定一个无向图,\(n\)个点,\(n-1\)条边,保证图联通(就是一棵树),并且给定每条边的权值。任取两个点,连接二者的路径上......