Codeforces Round 859 (Div
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的更多相关文章
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- 合合信息扫描全能王亮相静安区3·15活动,AI扫描带来绿色消费新体验
保护消费者的合法权益,是全社会的共同责任.为优化消费环境.促进品质消费高地建设,打造安全优质和谐的消费环境,上海静安区消保委于3月15日举办静安区2024年"3·15"国际消费者权 ...
- Angular 18+ 高级教程 – 学以致用
前言 读这么多原理,到底为了什么?真实项目中真的会用得到吗? 你正在疑惑 "知识的力量" 吗? 本篇会给一个非常非常好的案例,让你感悟 -- 知识如何用于实战. 记住,我的目的是让 ...
- Git冲突解决技巧
在多人协作的软件开发项目中,Git 冲突是不可避免的现象.当两个或更多的开发者同时修改了同一段代码,并且尝试将这些修改合并到一起时,冲突就发生了.解决这些冲突是确保代码库健康和项目顺利进行的关键.以下 ...
- 系统编程-进程-wait、waitpid和WIFEXITED系列宏 超级详解
1. wait.waitpid 函数简介 补充:对于waitpid,如果返回值为0,表示指定去等待的子进程尚未结束. 该系列宏的使用方法展示: PART1 实验思路: 使用wait系统调用让父进程给 ...
- [OI] Testlib
Testlib 是用于实现 SpecialJudge 的一种方式 为了使用 Testlib,你需要在你的文件中引用 Testlib.h testlib.h 下载 使用 Testlib 程序 以 Tes ...
- Kubernetes StatefulSet 控制器(二十一)
前面我们学习了 Deployment 和 ReplicaSet 两种资源对象得使用,在实际使用的过程中,Deployment 并不能编排所有类型的应用,对无状态服务编排是非常容易的,但是对于有状态服务 ...
- USB协议详解第3讲(USB描述符-设备描述符)
我们第一个学习要点就是USB描述符,所谓描述符其实就是C语言里面的结构体或者数组,数组包含的信息说明当前的设备具有哪些特征.USB描述符有设备描述符.配置描述符.接口描述符.端点描述符.字符串描述符, ...
- Java日期时间API系列32-----Jdk8中java.time包中的新的日期时间API类应用,时间工具包 xk-time 1.0.0 版本完成。
从Java日期时间API系列第一篇博客开始,利用业余时间对Java日期时间API源码进行了系统的.多次的阅读实践,包括Date.LocalDate.LocalDateTime.LocalTime.In ...
- kotlin类与对象——>对象表达式与对象声明、内联类
1.对象表达式与对象声明 有时候,我们需要创建一个对某个类做了轻微改动的类的对象,而不用为之显式声明新的子类.Kotlin 用对象表达式和对象声明处理这种情况 2.对象表达式 要创建一个继承自某个(或 ...
- 背靠AI,让AI当牛马,解决程序员的烦恼
开篇问题? 作为程序员的你,写代码累吗?累!苦嘛?苦,想哭嘛?哭不出来. 还在为工作中繁重的编码任务.复杂的调试过程以及不断更新的技术栈而苦恼吗?这些挑战不仅消耗大量的时间和精力,还时常让人陷入思维的 ...