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为还需要的蜡烛数, ...
随机推荐
- 基于Service Worker实现WebRTC局域网大文件传输能力
基于Service Worker实现WebRTC局域网大文件传输能力 Service Worker是一种驻留在用户浏览器后台的脚本,能够拦截和处理网络请求,从而实现丰富的离线体验.缓存管理和网络效率优 ...
- 小tips:HTML5的ruby标签实现给汉字加拼音、details标签实现折叠面板、原生进度条progress和度量meter
ruby标签实现给汉字加拼音 ruby 元素由一个或多个字符(需要一个解释/发音)和一个提供该信息的 rt 元素组成,还包括可选的 rp 元素,定义当浏览器不支持 "ruby" 元 ...
- Hadoop & Redis未授权漏洞实战——Vulfocus服务攻防
什么是未授权访问漏洞?Hadoop & Redis靶场实战--Vulfocus服务攻防 一.介绍 未授权访问,也称为未经授权的访问或非法访问,是指在没有得到适当权限或授权的情况下,个人或系统访 ...
- QT数据可视化框架编程实战之三维柱状图_补天云QT技术培训专家
QT数据可视化框架编程实战之三维柱状图_补天云QT技术培训专家 文章目录 QT数据可视化框架编程实战:三维柱状图可视化运行效果 主程序实现C++代码 主场景 QML代码 坐标轴QML代码 数据模型定义 ...
- 多线程ExecutorService 的理解与使用
原文链接:https://www.cnblogs.com/gxz-sw/p/6754476.html 接口 Java.util.concurrent.ExecutorService 表述了异步执行的机 ...
- Dockerfile相关(推送镜像?私有仓库?)(九)
上面我们讲到了 Dockerfile 的基本写法以及构建镜像的时候一些注意事项,那么镜像构建完成后,如何把我们的镜像给到别人使用呢?第一种方法就是利用 Docker 官方提供的公共的 Docker H ...
- WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前言 今天大姚给大家分享一套基于.NET 8.0 + LayUI的快速开发框架,项目完全开源.免费(MIT License)且开箱即用:WaterCloud. 可完全实现二次开发让开发更多关注业务逻辑 ...
- Java项目笔记(三)
一.前端传参类似以下格式,对象中包含一个对象,后台此时接收option为stirng类型 curriculumid question answer option {optionOne ,optionT ...
- nginx服务器下laravel项目无法访问
nginx服务器下laravel项目无法访问 后台用的nginx服务器,之前在本地开发项目时用的apache服务器,没想到切换到线上访问时除了首页一直显示404的错误,网页无法访问,网上搜索发现是ng ...
- iOS堆和栈的使用小结
堆和栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.堆,队列优先,先进先出(FIFO-first in first out):栈,先进后出(FILO-Fir ...