圓的周長可以定義為圓的外邊界。它是圓的周長。圓周圍的每個點都遵循某些屬性,如下所示 -
點 (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中文網其他相關文章!

正在执行的程序称为进程。进程可以是当前操作系统上运行的应用程序,也可以是与操作系统相关的应用程序。如果一个应用程序与操作系统相关,它首先会创建一个进程来执行自己。其他应用程序依赖操作系统服务来执行。大多数应用程序是操作系统服务以及维护操作系统、软件和硬件的后台应用程序。在python中,我们有不同的方法来检查应用程序是否打开。让我们一一详细了解它们。使用psutil.process_iter()函数psutil是python中的一个模块,它为用户提供一个接口来检索正在运行的进程和系统利用率的信息
![拼写检查在团队中不起作用[修复]](https://img.php.cn/upload/article/000/887/227/170968741326618.jpg)
我们已经开始注意到,有时拼写检查停止工作的团队。拼写检查是有效沟通的基本工具,任何对它的打击都会对工作流程造成相当大的破坏。在本文中,我们将探讨拼写检查可能无法按预期运行的常见原因,以及如何将其恢复到以前的状态。所以,如果拼写检查在团队中不起作用,请遵循本文中提到的解决方案。为什么Microsoft拼写检查不起作用?Microsoft拼写检查无法正常工作可能有多种原因。这些原因包括不兼容的语言设置、拼写检查功能被禁用、MSTeam或MSOffice安装损坏等。另外,过时的MSTeams和MSOf

可迭代对象是可以使用循环或可迭代函数迭代其所有元素的对象。列表、字符串、字典、元组等都称为可迭代对象。在Python语言中,有多种方法可以检查对象是否可迭代。让我们一一看看。使用循环在Python中,我们有两种循环技术,一种是使用“for”循环,另一种是使用“while”循环。使用这两个循环中的任何一个,我们可以检查给定的对象是否可迭代。示例在这个例子中,我们将尝试使用“for”循环迭代一个对象并检查它是否被迭代。以下是代码。l=["apple",22,"orang

Windows11中如何检查SSD运行状况?对于其快速的读取、写入和访问速度,SSD正在迅速取代HDD,但即使它们更可靠,您仍然需要在Windows11中检查SSD的运行状况。怎么去操作呢?本篇教程小编就来为大家分享一下方法吧。方法一:使用WMIC1、使用按键组合Win+R,键入wmic,然后按或单击“确定”。Enter2、现在,键入或粘贴以下命令以检查SSD运行状况:diskdrivegetstatus如果您收到“状态:正常”消息,则您的SSD驱动器运行正

Golang中如何检查字符串是否以特定字符开头?在使用Golang编程时,经常会遇到需要检查一个字符串是否以特定字符开头的情况。针对这一需求,我们可以使用Golang中的strings包提供的函数来实现。接下来将详细介绍如何使用Golang检查字符串是否以特定字符开头,并附上具体的代码示例。在Golang中,我们可以使用strings包中的HasPrefix

您可以利用List接口的contains()方法来检查列表中是否存在对象。contains()方法booleancontains(Objecto)如果此列表包含指定的元素,则返回true。更正式地说,如果且仅当此列表包含至少一个元素e,使得(o==null?e==null:o.equals(e)),则返回true。参数c-要测试其在此列表中是否存在的元素。返回值如果此列表包含指定的元素,则返回true。抛出ClassCastException-如果指定元素的类型与此列表不兼容(可选)。NullP

请考虑下表了解不同公司的资格标准-CGPA的中文翻译为:绩点平均成绩符合条件的公司大于或等于8谷歌、微软、亚马逊、戴尔、英特尔、Wipro大于或等于7教程点、accenture、Infosys、Emicon、Rellins大于或等于6rtCamp、Cybertech、Skybags、Killer、Raymond大于或等于5Patronics、鞋子、NoBrokers让我们进入java程序来检查tpp学生参加面试的资格。方法1:使用ifelseif条件通常,当我们必须检查多个条件时,我们会使用

闰年有366天,而普通年有365天,任务是通过程序检查给定的年份是否为闰年。判断的逻辑可以通过检查年份是否能被400或4整除来实现,但如果不能被这两个数整除,则为普通年。示例Input-:year=2000Output-:2000isaLeapYearInput-:year=101Output-:101isnotaLeapyear算法StartStep1->declarefunctionbooltocheckifyearifaleapyearornotboolcheck(intye


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版