<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 最后一個方塊的方向 > 原文: [https://www.geeksforgeeks.org/direction-last-square-block/](https://www.geeksforgeeks.org/direction-last-square-block/) 給定 R x C(1 < = R,C < = 1000000000)網格,初始位置為左上角,方向為東。 現在,我們開始向前移動并交叉矩陣的每個正方形塊。 每當發現死角或到達已被訪問的像元時,我們就采取正確的態度,因為我們無法再次越過被訪問的方塊。 告訴方向我們什么時候將到達最后一個方塊。 **例如:** 考慮 R = 3,C = 3 的情況。遵循的路徑將是(0,0)—(0,1)—(0,2)— (1,2)-(2,2)-(2,1)-(2,0)-(1,0)-(1,1)。 至此,所有廣場均已訪問,并朝右。 **示例**: ``` Input : R = 1, C = 1 Output : Right Input : R = 2, C = 2 Output : Left Input : R = 3, C = 1 Output : Down Input : R = 3, C = 3 Output : Right ``` **簡單解決方案:** 一個簡單的解決方案是使 R x C 矩陣初始化為零,并以螺旋形式遍歷它,并采用變量“ Dir”指示當前方向 。 每當我們在任何行和列的末尾時,都請選擇“右”并根據您當前的方向更改“目錄”的值。 現在遵循給定條件: * 如果您要遍歷第一行,則當前方向為“右”。 * 如果您是對的話,那么您當前的方向是“向下”。 * 如果您要遍歷底行,則當前方向為“左”。 * 如果您正在橫穿左廊,那么您當前的方向是“上”。 當我們到達最后一個方塊時,只需打印當前方向即可; “ Dir”變量的值。 此問題的時間和空間復雜度為 O(R x C),這僅適用于 R,C 的較小值,但此處 R 和 C 太大,因此無法針對太大的值創建 R x C 矩陣 R 和 C。 **高效方法:** 這種方法幾乎不需要觀察,并且需要一些筆紙工作。 在這里,我們必須考慮 R 和 C 的所有可能情況,然后只需要為所有可能情況設置 IF 條件。 在這里,我們具備所有可能的條件: 1. R!= C,R 為偶數,C 為奇數,R < C,方向為“左”。 2. R!= C,R 為奇數,C 為偶數,R < C,方向為“右”。 3. R!= C,R 為偶數,C 為偶數,R < C,方向為“左”。 4. R!= C,R 為奇數,C 為奇數,R < C 方向為“右”。 5. R!= C,R 為偶數,C 為奇數,R > C,方向為“向下”。 6. R!= C,R 為奇數,C 為偶數,R > C,方向為“上”。 7. R!= C,R 為偶數,C 為偶數,R > C,方向為“上”。 8. R!= C,R 為奇數,C 為奇數,R > C,方向為“向下”。 9. R == C,R 為偶數,C 為偶數,方向為“左”。 10. R == C,R 為奇數,C 為奇數,方向為“右”。 以下是上述想法的實現。 ## C++ ```cpp // C++ program to tell the Current direction in // R x C grid #include <iostream> using namespace std; typedef long long int ll; // Function which tells the Current direction void direction(ll R, ll C) { ????if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) { ????????cout << "Left" << endl; ????????return; ????} ????if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) { ????????cout << "Up" << endl; ????????return; ????} ????if (R == C && R % 2 != 0 && C % 2 != 0) { ????????cout << "Right" << endl; ????????return; ????} ????if (R == C && R % 2 == 0 && C % 2 == 0) { ????????cout << "Left" << endl; ????????return; ????} ????if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) { ????????cout << "Right" << endl; ????????return; ????} ????if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) { ????????cout << "Down" << endl; ????????return; ????} ????if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) { ????????cout << "Left" << endl; ????????return; ????} ????if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) { ????????cout << "Up" << endl; ????????return; ????} ????if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) { ????????cout << "Down" << endl; ????????return; ????} ????if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) { ????????cout << "Right" << endl; ????????return; ????} } // Driver program to test the Cases int main() { ????ll R = 3, C = 1; ????direction(R, C); ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看