检查是否可能从原点到达给定圆的周长上的任意点
圆的周长可以定义为圆的外边界。它是圆的周长。圆周围的每个点都遵循某些属性,如下所示 -
点 (x,y) 位于圆内,使得 $mathrm{x^2 + y^2
点 (x,y) 位于圆上,使得 $mathrm{x^2 + y^2 = R^2}$
点 (x,y) 位于圆外,使得 $mathrm{x^2 + y^2 > R^2}$
其中 R = 圆的半径。
问题陈述
给定一个表示一系列移动(L、R、U、D)的字符串 S 和一个表示圆半径的整数 R。检查是否可以通过选择从S开始的任何移动子序列来到达以原点为半径为R的圆的圆周上的任何点。每个移动的操作如下所示,
L = 减少 x 坐标
R = 增量 x 坐标
U = y 坐标增量
D = 递减 y 坐标
示例 1
输入
S = “RURDLR” R = 2
输出
yes
说明
选择子序列“RR” -
最初,(0, 0) + R -> (1, 0) + R -> (2, 0)。
周长可能为 22 + 02 = 4 = R2
示例 2
输入
S = “UUUUU” R = 6
输出
no
说明
选择最大的子序列“UUUU” -
最初,(0, 0) + U -> (0, 1) + U -> (0, 2) + U -> (0, 3) + U -> (0, 4) + U -> (0, 5)。
不可能达到圆周,因为 02 + 52 = 25 R2
方法 1:暴力破解
问题的解决方法是找到字符串S的所有可能的子序列,然后检查每个子序列是否可以到达圆周。通过维护 x 和 y 的计数器来检查这些条件,其中 x 在每个 L 上递减并在每个 R 上递增。类似地,y 在每个 D 上递减并在每个 U 上递增。然后检查 x2 + y2 = R2 检查终点是否在圆周上。
伪代码
procedure subsequence (S, sub, vec): if S is empty add sub to vec return end if subsequence(S.substring(1), sub + S[0], vec) subsequence(S.substring(1), sub, vec) end procedure procedure checkSeq (S, R) x = 0 y = 0 for move in S do if move == 'L' then x = x - 1 else if move == 'R' then x = x + 1 else if move == 'U' then y = y + 1 else if move == 'D' then y = y - 1 end if if x^2 + y^2 = R^2 then return true end if end for return false end procedure procedure reachCircumference (S, R): v = [] subsequence(S, "", v) for str in v: if checkSeq(str, R) return "yes" end if return "no" end procedure
示例:C++ 实现
在下面的程序中,创建字符串 S 的所有可能的子序列,并检查它们是否到达圆的圆周。
#include <bits/stdc++.h> using namespace std; // Function to create all the possible subsequence of string S void subsequence(string S, string sub, vector<string> &vec){ // Base Case if (S.empty()) { vec.push_back(sub); return; } // Subsequence including the character subsequence(S.substr(1), sub + S[0], vec); // Subsequence excluding the character subsequence(S.substr(1), sub, vec); } // Function to check if a given sequence of steps lead to the circumference of the circle with radius R bool checkSeq(string S, int R){ // Initialising centre of circle as (0, 0) int x = 0, y = 0; for (char move : S) { if (move == 'L') { x -= 1; } else if (move == 'R') { x += 1; } else if (move == 'U') { y += 1; } else if (move == 'D') { y -= 1; } // Check to find if (x, y) lie on circumference using the circle equation if (x*x + y*y == R*R) { return true; } } return false; } // function to check if any subsequence of string S leads to any point on the circumference of the circle string reachCircumference(string S, int R){ vector <string> v; string sub = ""; // Storing all subsequence in vector v subsequence(S, sub, v); // Checking the condition for each subsequence for (auto str: v) { if(checkSeq(str, R)) { return "yes"; break; } } return "no"; } // Driver Code int main(){ string S = "RURDLR"; int R = 2; cout << reachCircumference(S, R) << endl; return 0; }
输出
yes
方法2:优化方法
解决该问题的一个有效方法是检查使用(L、R、U 或 D)的任意一对 x 和 y 的 x 和 y 的平方和是否等于半径的平方。
首先,我们计算每个步骤的最大出现次数,并检查是否有任何一个等于 R。如果不相等,则我们检查是否有任何数量的 L 或 R 以及 U 或 D 的对可以导致等于 R 的距离起源。
伪代码
procedure reachCircumference (S, R) cL = 0 cR = 0 cD = 0 cU = 0 for move in S do if move == 'L' then x = x - 1 else if move == 'R' then x = x + 1 else if move == 'U' then y = y + 1 else if move == 'D' then y = y - 1 end if if x^2 + y^2 = R^2 then return true end if end for if max(max(cL, cR), max(cD, cU)) >= R return “yes” maxLR = max(cL, cR) maxUD = max(cU, cD) Initialise unordered map mp sq = R * R for i = 1 till i * i = sq if sq - i*i is not in the map if maxLR>= mp[sq - i * i] and maxUD >= i return “yes” end if if maxLR >= i && maxUD >= mp[sq - i * i] return “yes” end if end if end for return “no” end procedure
下面是 C++ 实现,
在下面的程序中,我们使用映射来检查是否存在通向圆周长的子序列。
#include <bits/stdc++.h> using namespace std; // Function to check if any subsequence of string S leads to any point on the circumference of the circle string reachCircumference (string S, int R){ // Counting total occurrenceof each L, R, U, D int cL = 0, cR = 0, cD = 0, cU = 0; for (char move : S) { if (move == 'L') { cL++; } else if (move == 'R') { cR++; } else if (move == 'U') { cU++; } else if (move == 'D') { cD++; } } // Checking for a path to circumference using only one type of move if (max(max(cL, cR), max(cD, cU)) >= R) { return "yes"; } int maxLR = max(cL, cR), maxUD = max(cU, cD); unordered_map<int, int> mp; int sq = R * R; for (int i = 1; i * i <= sq; i++) { mp[i * i] = i; if (mp.find(sq - i * i) != mp.end()) { // Checking if it is possible to reach (± mp[r_square - i*i], ± i) if (maxLR>= mp[sq - i * i] && maxUD >= i) return "yes"; // Checking if it is possible to reach (±i, ±mp[r_square-i*i]) if (maxLR >= i && maxUD >= mp[sq - i * i]) return "yes"; } } return "no"; } // Driver Code int main(){ string S = "RURDLR"; int R = 5; cout << reachCircumference(S, R) << endl; return 0; }
输出
no
结论
总之,为了找出是否可以使用字符串 S 中的步骤子序列来获得以原点为中心的圆的周长,可以使用上述任何方法。第二种方法是更快的方法,但使用额外的空间,而第一种方法是强力方法,效率不是很高,但易于理解。
以上是检查是否可能从原点到达给定圆的周长上的任意点的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

使用正则表达式从PHP数组中去除重复值的方法:使用正则表达式/(.*)(.+)/i匹配并替换重复项。遍历数组元素,使用preg_match检查匹配情况。如果匹配,跳过值;否则,将其添加到无重复值的新数组中。

1、编程可以用于开发各种软件和应用程序,包括网站、手机应用、游戏和数据分析工具等。它的应用领域非常广泛,覆盖了几乎所有行业,包括科学研究、医疗保健、金融、教育、娱乐等。2、学习编程可以帮助我们提高问题解决能力和逻辑思维能力。编程过程中,我们需要分析和理解问题,找出解决方案,并将其转化为代码。这种思维方式能够培养我们的分析和抽象能力,提高我们解决实际问题的能力。

使用Golang构建基于浏览器的应用程序Golang结合JavaScript构建了动态的前端体验。安装Golang:访问https://golang.org/doc/install。设置Golang项目:创建一个名为main.go的文件。使用GorillaWebToolkit:添加GorillaWebToolkit代码以处理HTTP请求。创建HTML模板:在templates子目录中创建index.html,这是主模板。

C++编程谜题涵盖斐波那契数列、阶乘、汉明距离、数组最大值和最小值等算法和数据结构概念,通过解决这些谜题,可以巩固C++知识,提升算法理解和编程技巧。

Python 使初学者能够解决问题。其用户友好的语法、广泛的库以及变量、条件语句和循环等功能可实现高效的代码开发。从管理数据到控制程序流程和执行重复任务,Python 提供了

通过GoGet,可以快速便捷地获取Go模块,步骤如下:在终端中运行:goget[module-path],其中module-path为模块路径。GoGet会自动下载模块及其依赖项。安装的位置由GOPATH环境变量指定。

Python通过其易学性和强大功能,是初学者的理想编程入门语言。其基础包括:变量:用于存储数据(数字、字符串、列表等)。数据类型:定义变量中数据的类型(整数、浮点数等)。运算符:用于数学运算和比较。控制流:控制代码执行流(条件语句、循环)。

Go中的错误处理包括包装错误和展开错误。包装错误允许用一个错误类型包装另一个,提供更丰富上下文的错误。展开错误遍历嵌套错误链,找到最底层错误,便于调试。通过结合使用这两种技术,可以有效处理错误条件,提供更丰富的错误上下文和更好的调试能力。
