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. 小tips:微信小程序登录后返回登录前的页面继续操作(保留参数)

    在app.js中添加如下两个方法即可: // 设置需要回调的地址 setCallbackUrl: function(mode) { return new Promise((resolve,reject ...

  2. QT6框架WebEngine模块之WebEngine总体介绍以及WebEngine能做什么?

    QT6框架WebEngine模块之WebEngine总体介绍以及WebEngine能做什么? 简介 本文简略介绍QT6框架WebEngine模块之WebEngine总体介绍以及WebEngine能做什 ...

  3. maven安装本地jar命令

    mvn install:install-file -Dfile=jar包的位置 -DgroupId=pom.xml的groupId -DartifactId=pom.xm的artifactId -Dv ...

  4. 暑假集训CSP提高模拟11

    A.Fate 求次短路方案数. 这题有点小水了,好像之前做过. 具体的方案显然是 DP,考虑枚举当前每一个路径长度,假如比最短路更优则覆盖最短路,之前的最短路用来覆盖次短路. 否则如果比次短路更优,则 ...

  5. 构建数据大屏,塑造IT运维可视化核心竞争力

    随着大数据.云计算等新兴技术的发展与运用,在金融.交通.教育.政府等行业的信息化在飞速发展.与此同时,各行业的IT建设与维护管理成本也在与日俱增,大量的运维工作下产生庞大的运维数据,如何进行运维数据可 ...

  6. Redis 发布订阅模式

    概述 Redis 的发布/订阅是一种消息通信模式:发送者(Pub)向频道(Channel)发送消息,订阅者(Sub)接收频道上的消息.Redis 客户端可以订阅任意数量的频道,发送者也可以向任意频道发 ...

  7. 称骨算命免费api接口_json数据接口示例_八字称骨测算程序php接口

    称骨算命是算命方法的一种,和生辰八字算命.紫微斗数算命异曲同工,略有不同,虽然都是用出生的时间算命,但比较而言,称骨算命将命运分的比较粗略,只是把命运分为五十一种.故对命运的考察不细致,不太准确,可以 ...

  8. Perfetto分析进阶

    一.Perfetto介绍 Perfetto是Android Q中引入的全新下一代平台级跟踪工具,为Android.Linux和Chrome平台提供了一种通用的性能检测和跟踪分析工具集.其核心是引入了一 ...

  9. C#扩展方法 Where Any Count Signal SignalOrDefault First 等方法的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  10. Stream流,集合与基本数组的相互转换

    Arrays类的Api stream()方法传入数组,返回对应的stream流. Collection集合的Api: stream()不传参数,返回Stream流. 有了上述Api可以完成如下转换.. ...