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;
}

Codeforces Round 859 (Div的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. C#自定义控件—指示灯

    C#用户控件之指示灯 在体现通讯状态.运行状态等用一个靓眼的指示灯如何做? 思路(GDI) 外环用笔绘制(Pen),内圆用画刷(SolidBrush); 两个方法(用笔画圆,用画刷填充圆的内部): 绘 ...

  2. 游戏AI行为决策——MLP(多层感知机/人工神经网络)

    游戏AI行为决策(特别篇)--MLP(附代码与项目) 你一定听说过神经网络的大名,你有想过将它用于游戏AI的行为决策上吗?其实在(2010年发布的)<最高指挥官2>中就有应用了,今天请允许 ...

  3. LeetCode 验证二叉搜索树的两种不同题型

    一.leetcode 98. 验证二叉搜索树 通过辅助函数增加参数进行判断. 虽然节点值在INT范围内,但要求其中序遍历序列严格单调递增,因此等于也不行,而且需要用LONG_MIN\LONG_MAX初 ...

  4. Goby 漏洞发布|(CVE-2024-45195)Apache OFBiz /viewdatafile 代码执行漏洞【已复现】

    漏洞名称:Apache OFBiz /viewdatafile 代码执行漏洞(CVE-2024-45195) English Name:Apache OFBiz /viewdatafile Code ...

  5. WPF 实现一个吃豆豆的Loading加载动画

    运行的效果如下 先引入一下我们需要的库 在nuget上面搜一下"expression.Drawing",安装一下这个包 我们再创建一个Window,引入一下这个包的命名空间 我们设 ...

  6. ASP.NET Core C# 反射 & 表达式树 (第四篇)

    前言 上一篇说完了动态创建表达式树, 这篇来说一说解析表达式树. 我之所以会用到是因为最近开始大量使用 trigger 来维护冗余. 但 trigger 本身并不好维护 (EF Core 也不支持), ...

  7. 嵌入式Linux ubi文件系统制作、分区设置、只读文件系统,uboot启动参数root

    当前平台, 基于君正的X10000平台的嵌入式Linux 系统 0  目的 我要设置根文件系统为可读写, 设置data分区上的文件系统为只读 1 设置各文件系统的读写属性 /bin/mount -o ...

  8. 提升软件测试效率与灵活性:探索Mock测试的重要性

    Mock测试是测试过程中的一种方法,用于替代那些难以构造或获取的对象,通过创建虚拟对象来进行测试.所谓难以构造的对象如何理解呢? 举例来说,像HttpServletRequest这样的对象需要在具有s ...

  9. Windows系统之“关闭windows 文件夹分组”

    不知道怎么就按出来了 文件夹分组 ,也不知道怎么关闭,别扭了好久 解决办法: 右键点击文件夹空白处,选择 "分组依据" --> 然后选择 "无"

  10. nicegui太香了,跨平台开发和跨平台运行--使用Python+nicegui实现系统布局界面的开发

    在现今国产化浪潮的驱动下,跨平台或者缩小范围说基于国产化Linux或者基于国产鸿蒙系统的开发才是未来的趋势了,风口浪尖上,我们开发人员也只能顺势而为,本篇随笔介绍在Python开发中,使用使用Pyth ...