<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 沿給定方向移動矩陣元素并添加具有相同值的元素 > 原文: [https://www.geeksforgeeks.org/move-matrix-elements-given-direction-add-elements-value/](https://www.geeksforgeeks.org/move-matrix-elements-given-direction-add-elements-value/) 給定大小為 **n x n** 的矩陣 **m [] []** ,該矩陣由整數組成,并給出表示方向的字符“ x”。 “ x”的值可以是“ u”,“ d”,“ l”,“ r”,分別表示上,下,左,右。 任務是將元素移至給定方向,以便將具有相同值的連續元素添加到單個值中,并移動其余元素。 同樣,如果給定方向上的下一個元素為 0,則移動該元素。 **例如**: 考慮 x ='l'且矩陣 m [] [], 32 3 3 0 0 1 10 10 8 在第一行中添加 3,第三行中添加 10 并在第二行中移動 1 之后, 矩陣將變為 32 6 0 1 0 0 20 8 0 **示例**: ``` Input : x = 'l' m[][] = { { 32, 3, 3, 3, 3 }, { 0, 0, 1, 0, 0 }, { 10, 10, 8, 1, 2}, { 0, 0, 0, 0, 1}, { 4, 5, 6, 7, 8 } } Output : 32 6 6 0 0 1 0 0 0 0 20 8 1 2 0 1 0 0 0 0 0 4 5 6 7 8 Input : x = 'u' m[][] = { { 10, 3, 32 }, { 10, 0, 96 }, { 5, 32, 96 } } Output : 20 3 32 5 32 192 0 0 0 ``` **方法**:想法是從行或列的側面 x 朝 x′(與 x 相對)遍歷每一行或每一列(取決于給定的方向)。 例如,如果給定的 x 值為“ l”(左),則開始從左側到右側掃描每一行。 遍歷時,通過跳過具有值 0 的元素和連續元素之和(如果它們具有相等的值),將行或列元素存儲在臨時一維數組中(例如 temp [])。 之后,開始將臨時數組 temp [0..k]從(行或列的 x 的)x 側復制到當前行或列到 x'(與 x 相對),然后用 0 填充元素的重置。 令 x =“ l”,即向左移動。 因此,從該行的最左索引到最右索引開始處理每一行,并通過忽略 0 并將兩個連續元素(如果它們具有相同的值)加到一個中的處理,存儲在臨時數組中。 下面是第 1 行的圖示, ![](https://img.kancloud.cn/ce/32/ce32c2498edea2a8a9d740cd873ccdba_544x190.png) 現在,對于每個復制行,將臨時數組從最左側的索引復制到最右側的索引。 以下是第 1 行的圖示, ![](https://img.kancloud.cn/ef/c3/efc3f549baae71f431ed80a18508453f_534x185.png) **以下是此方法的實現**: ## C++ ```cpp // CPP code to move matrix elements // in given direction with add // element with same value #include <bits/stdc++.h> using namespace std; #define MAX 50 // Function to shift the matrix // in the given direction void moveMatrix(char d[], int n, ????????????????int a[MAX][MAX]) { ????// For right shift move. ????if (d[0] == 'r') { ????????// for each row from ????????// top to bottom ????????for (int i = 0; i < n; i++) { ????????????vector<int> v, w; ????????????int j; ????????????// for each element of ????????????// row from right to left ????????????for (j = n - 1; j >= 0; j--) { ????????????????// if not 0 ????????????????if (a[i][j]) ????????????????????v.push_back(a[i][j]); ????????????} ????????????// for each temporary array ????????????for (j = 0; j < v.size(); j++) { ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if (j < v.size() - 1 && v[j] == v[j + 1]) { ????????????????????// insert only one element ????????????????????// as sum of two same element. ????????????????????w.push_back(2 * v[j]); ????????????????????j++; ????????????????} ????????????????else ????????????????????w.push_back(v[j]); ????????????} ????????????// filling the each row element to 0\. ????????????for (j = 0; j < n; j++) ????????????????a[i][j] = 0; ????????????j = n - 1; ????????????// Copying the temporary ????????????// array to the current row. ????????????for (auto it = w.begin(); ?????????????????it != w.end(); it++) ????????????????a[i][j--] = *it; ????????} ????} ????// for left shift move ????else if (d[0] == 'l') { ????????// for each row ????????for (int i = 0; i < n; i++) { ????????????vector<int> v, w; ????????????int j; ????????????// for each element of the ????????????// row from left to right ????????????for (j = 0; j < n; j++) { ????????????????// if not 0 ????????????????if (a[i][j]) ????????????????????v.push_back(a[i][j]); ????????????} ????????????// for each temporary array ????????????for (j = 0; j < v.size(); j++) { ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if (j < v.size() - 1 && v[j] == v[j + 1]) { ????????????????????// insert only one element ????????????????????// as sum of two same element. ????????????????????w.push_back(2 * v[j]); ????????????????????j++; ????????????????} ????????????????else ????????????????????w.push_back(v[j]); ????????????} ????????????// filling the each row element to 0\. ????????????for (j = 0; j < n; j++) ????????????????a[i][j] = 0; ????????????j = 0; ????????????for (auto it = w.begin(); ?????????????????it != w.end(); it++) ????????????????a[i][j++] = *it; ????????} ????} ????// for down shift move. ????else if (d[0] == 'd') { ????????// for each column ????????for (int i = 0; i < n; i++) { ????????????vector<int> v, w; ????????????int j; ????????????// for each element of ????????????// column from bottom to top ????????????for (j = n - 1; j >= 0; j--) { ????????????????// if not 0 ????????????????if (a[j][i]) ????????????????????v.push_back(a[j][i]); ????????????} ????????????// for each temporary array ????????????for (j = 0; j < v.size(); j++) { ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if (j < v.size() - 1 && v[j] == v[j + 1]) { ????????????????????// insert only one element ????????????????????// as sum of two same element. ????????????????????w.push_back(2 * v[j]); ????????????????????j++; ????????????????} ????????????????else ????????????????????w.push_back(v[j]); ????????????} ????????????// filling the each column element to 0\. ????????????for (j = 0; j < n; j++) ????????????????a[j][i] = 0; ????????????j = n - 1; ????????????// Copying the temporary array ????????????// to the current column ????????????for (auto it = w.begin(); ?????????????????it != w.end(); it++) ????????????????a[j--][i] = *it; ????????} ????} ????// for up shift move ????else if (d[0] == 'u') { ????????// for each column ????????for (int i = 0; i < n; i++) { ????????????vector<int> v, w; ????????????int j; ????????????// for each element of column ????????????// from top to bottom ????????????for (j = 0; j < n; j++) { ????????????????// if not 0 ????????????????if (a[j][i]) ????????????????????v.push_back(a[j][i]); ????????????} ????????????// for each temporary array ????????????for (j = 0; j < v.size(); j++) { ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if (j < v.size() - 1 && v[j] == v[j + 1]) { ????????????????????// insert only one element ????????????????????// as sum of two same element. ????????????????????w.push_back(2 * v[j]); ????????????????????j++; ????????????????} ????????????????else ????????????????????w.push_back(v[j]); ????????????} ????????????// filling the each column element to 0\. ????????????for (j = 0; j < n; j++) ????????????????a[j][i] = 0; ????????????j = 0; ????????????// Copying the temporary array ????????????// to the current column ????????????for (auto it = w.begin(); ?????????????????it != w.end(); it++) ????????????????a[j++][i] = *it; ????????} ????} } // Driven Program int main() { ????char d[2] = "l"; ????int n = 5; ????int a[MAX][MAX] = { { 32, 3, 3, 3, 3 }, ????????????????????????{ 0, 0, 1, 0, 0 }, ????????????????????????{ 10, 10, 8, 1, 2 }, ????????????????????????{ 0, 0, 0, 0, 1 }, ????????????????????????{ 4, 5, 6, 7, 8 } }; ????moveMatrix(d, n, a); ????// Printing the final array ????for (int i = 0; i < n; i++) { ????????for (int j = 0; j < n; j++) ????????????cout << a[i][j] << " "; ????????cout << endl; ????} ????return 0; } ``` ## Java ```java // Java code to move matrix // elements in given direction // with add element with same value import java.io.*; import java.util.*; class GFG { ????// Function to shift the matrix ????// in the given direction ????static void moveMatrix(char d, ???????????????????????????int n, ???????????????????????????int a[][]) ????{ ????????// For right shift move. ????????if (d == 'r') { ????????????// for each row from ????????????// top to bottom ????????????for (int i = 0; i < n; i++) { ????????????????ArrayList<Integer> v = new ArrayList<Integer>(); ????????????????ArrayList<Integer> w = new ArrayList<Integer>(); ????????????????int j; ????????????????// for each element of ????????????????// row from right to left ????????????????for (j = n - 1; j >= 0; j--) { ????????????????????// if not 0 ????????????????????if (a[i][j] != 0) ????????????????????????v.add(a[i][j]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.size(); j++) { ????????????????????// if two element have ????????????????????// same value at ????????????????????// consecutive position. ????????????????????if (j < v.size() - 1 && v.get(j) == v.get(j + 1)) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.add(2 * v.get(j)); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.add(v.get(j)); ????????????????} ????????????????// filling the each ????????????????// row element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[i][j] = 0; ????????????????j = n - 1; ????????????????// Copying the temporary ????????????????// array to the current row. ????????????????for (int it = 0; it < w.size(); it++) ????????????????????a[i][j--] = w.get(it); ????????????} ????????} ????????// for left shift move ????????else if (d == 'l') { ????????????// for each row ????????????for (int i = 0; i < n; i++) { ????????????????ArrayList<Integer> v = new ArrayList<Integer>(); ????????????????ArrayList<Integer> w = new ArrayList<Integer>(); ????????????????int j; ????????????????// for each element of the ????????????????// row from left to right ????????????????for (j = 0; j < n; j++) { ????????????????????// if not 0 ????????????????????if (a[i][j] != 0) ????????????????????????v.add(a[i][j]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.size(); j++) { ????????????????????// if two element have ????????????????????// same value at ????????????????????// consecutive position. ????????????????????if (j < v.size() - 1 && v.get(j) == v.get(j + 1)) { ????????????????????????// insert only one ????????????????????????// element as sum ????????????????????????// of two same element. ????????????????????????w.add(2 * v.get(j)); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.add(v.get(j)); ????????????????} ????????????????// filling the each ????????????????// row element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[i][j] = 0; ????????????????j = 0; ????????????????for (int it = 0; it < w.size(); it++) ????????????????????a[i][j++] = w.get(it); ????????????} ????????} ????????// for down shift move. ????????else if (d == 'd') { ????????????// for each column ????????????for (int i = 0; i < n; i++) { ????????????????ArrayList<Integer> v = new ArrayList<Integer>(); ????????????????ArrayList<Integer> w = new ArrayList<Integer>(); ????????????????int j; ????????????????// for each element of ????????????????// column from bottom to top ????????????????for (j = n - 1; j >= 0; j--) { ????????????????????// if not 0 ????????????????????if (a[j][i] != 0) ????????????????????????v.add(a[j][i]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.size(); j++) { ????????????????????// if two element have ????????????????????// same value at consecutive ????????????????????// position. ????????????????????if (j < v.size() - 1 && v.get(j) == v.get(j + 1)) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.add(2 * v.get(j)); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.add(v.get(j)); ????????????????} ????????????????// filling the each ????????????????// column element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[j][i] = 0; ????????????????j = n - 1; ????????????????// Copying the temporary array ????????????????// to the current column ????????????????for (int it = 0; it < w.size(); it++) ????????????????????a[j--][i] = w.get(it); ????????????} ????????} ????????// for up shift move ????????else if (d == 'u') { ????????????// for each column ????????????for (int i = 0; i < n; i++) { ????????????????ArrayList<Integer> v = new ArrayList<Integer>(); ????????????????ArrayList<Integer> w = new ArrayList<Integer>(); ????????????????int j; ????????????????// for each element of column ????????????????// from top to bottom ????????????????for (j = 0; j < n; j++) { ????????????????????// if not 0 ????????????????????if (a[j][i] != 0) ????????????????????????v.add(a[j][i]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.size(); j++) { ????????????????????// if two element have ????????????????????// same value at ????????????????????// consecutive position. ????????????????????if (j < v.size() - 1 && v.get(j) == v.get(j + 1)) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.add(2 * v.get(j)); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.add(v.get(j)); ????????????????} ????????????????// filling the each ????????????????// column element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[j][i] = 0; ????????????????j = 0; ????????????????// Copying the temporary ????????????????// array to the current ????????????????// column ????????????????for (int it = 0; it < w.size(); it++) ????????????????????a[j++][i] = w.get(it); ????????????} ????????} ????} ????// Driver Code ????public static void main(String args[]) ????{ ????????char d = 'l'; ????????int n = 5; ????????int a[][] = { { 32, 3, 3, 3, 3 }, ??????????????????????{ 0, 0, 1, 0, 0 }, ??????????????????????{ 10, 10, 8, 1, 2 }, ??????????????????????{ 0, 0, 0, 0, 1 }, ??????????????????????{ 4, 5, 6, 7, 8 } }; ????????moveMatrix(d, n, a); ????????// Printing the ????????// final array ????????for (int i = 0; i < n; i++) { ????????????for (int j = 0; j < n; j++) ????????????????System.out.print(a[i][j] + " "); ????????????System.out.println(); ????????} ????} } // This code is contributed by // Manish Shaw(manishshaw1) ``` ## C# ```cs // C# code to move matrix elements // in given direction with add // element with same value using System; using System.Collections.Generic; class GFG { ????// Function to shift the matrix ????// in the given direction ????static void moveMatrix(char d, int n, ???????????????????????????int[, ] a) ????{ ????????// For right shift move. ????????if (d == 'r') { ????????????// for each row from ????????????// top to bottom ????????????for (int i = 0; i < n; i++) { ????????????????List<int> v = new List<int>(); ????????????????List<int> w = new List<int>(); ????????????????int j; ????????????????// for each element of ????????????????// row from right to left ????????????????for (j = n - 1; j >= 0; j--) { ????????????????????// if not 0 ????????????????????if (a[i, j] != 0) ????????????????????????v.Add(a[i, j]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.Count; j++) { ????????????????????// if two element have ????????????????????// same value at ????????????????????// consecutive position. ????????????????????if (j < v.Count - 1 && v[j] == v[j + 1]) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.Add(2 * v[j]); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.Add(v[j]); ????????????????} ????????????????// filling the each ????????????????// row element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[i, j] = 0; ????????????????j = n - 1; ????????????????// Copying the temporary ????????????????// array to the current row. ????????????????for (int it = 0; it < w.Count; it++) ????????????????????a[i, j--] = w[it]; ????????????} ????????} ????????// for left shift move ????????else if (d == 'l') { ????????????// for each row ????????????for (int i = 0; i < n; i++) { ????????????????List<int> v = new List<int>(); ????????????????List<int> w = new List<int>(); ????????????????int j; ????????????????// for each element of the ????????????????// row from left to right ????????????????for (j = 0; j < n; j++) { ????????????????????// if not 0 ????????????????????if (a[i, j] != 0) ????????????????????????v.Add(a[i, j]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.Count; j++) { ????????????????????// if two element have ????????????????????// same value at ????????????????????// consecutive position. ????????????????????if (j < v.Count - 1 && v[j] == v[j + 1]) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.Add(2 * v[j]); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.Add(v[j]); ????????????????} ????????????????// filling the each ????????????????// row element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[i, j] = 0; ????????????????j = 0; ????????????????for (int it = 0; it < w.Count; it++) ????????????????????a[i, j++] = w[it]; ????????????} ????????} ????????// for down shift move. ????????else if (d == 'd') { ????????????// for each column ????????????for (int i = 0; i < n; i++) { ????????????????List<int> v = new List<int>(); ????????????????List<int> w = new List<int>(); ????????????????int j; ????????????????// for each element of ????????????????// column from bottom to top ????????????????for (j = n - 1; j >= 0; j--) { ????????????????????// if not 0 ????????????????????if (a[j, i] != 0) ????????????????????????v.Add(a[j, i]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.Count; j++) { ????????????????????// if two element have same ????????????????????// value at consecutive position. ????????????????????if (j < v.Count - 1 && v[j] == v[j + 1]) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.Add(2 * v[j]); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.Add(v[j]); ????????????????} ????????????????// filling the each ????????????????// column element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[j, i] = 0; ????????????????j = n - 1; ????????????????// Copying the temporary array ????????????????// to the current column ????????????????for (int it = 0; it < w.Count; it++) ????????????????????a[j--, i] = w[it]; ????????????} ????????} ????????// for up shift move ????????else if (d == 'u') { ????????????// for each column ????????????for (int i = 0; i < n; i++) { ????????????????List<int> v = new List<int>(); ????????????????List<int> w = new List<int>(); ????????????????int j; ????????????????// for each element of column ????????????????// from top to bottom ????????????????for (j = 0; j < n; j++) { ????????????????????// if not 0 ????????????????????if (a[j, i] != 0) ????????????????????????v.Add(a[j, i]); ????????????????} ????????????????// for each temporary array ????????????????for (j = 0; j < v.Count; j++) { ????????????????????// if two element have same ????????????????????// value at consecutive position. ????????????????????if (j < v.Count - 1 && v[j] == v[j + 1]) { ????????????????????????// insert only one element ????????????????????????// as sum of two same element. ????????????????????????w.Add(2 * v[j]); ????????????????????????j++; ????????????????????} ????????????????????else ????????????????????????w.Add(v[j]); ????????????????} ????????????????// filling the each ????????????????// column element to 0\. ????????????????for (j = 0; j < n; j++) ????????????????????a[j, i] = 0; ????????????????j = 0; ????????????????// Copying the temporary array ????????????????// to the current column ????????????????for (int it = 0; it < w.Count; it++) ????????????????????a[j++, i] = w[it]; ????????????} ????????} ????} ????// Driven Code ????static void Main() ????{ ????????char d = 'l'; ????????int n = 5; ????????int[, ] a = new int[, ] { { 32, 3, 3, 3, 3 }, ??????????????????????????????????{ 0, 0, 1, 0, 0 }, ??????????????????????????????????{ 10, 10, 8, 1, 2 }, ??????????????????????????????????{ 0, 0, 0, 0, 1 }, ??????????????????????????????????{ 4, 5, 6, 7, 8 } }; ????????moveMatrix(d, n, a); ????????// Printing the final array ????????for (int i = 0; i < n; i++) { ????????????for (int j = 0; j < n; j++) ????????????????Console.Write(a[i, j] + " "); ????????????Console.WriteLine(); ????????} ????} } // This code is contributed by // Manish Shaw(manishshaw1) ``` ## PHP ```php <?php // PHP code to move matrix? // elements in given? // direction with add element // with same value $MAX = 50; // Function to shift the matrix // in the given direction function moveMatrix($d, $n, &$a) {? ????global $MAX; ????// For right shift move. ????if ($d[0] == 'r')? ????{ ????????// for each row from? ????????// top to bottom ????????for ($i = 0; $i < $n; $i++)? ????????{ ????????????$v = array(); ????????????$w = array(); ????????????$j = 0; ????????????// for each element of? ????????????// row from right to left ????????????for ($j = $n - 1; $j >= 0; $j--)? ????????????{ ????????????????// if not 0 ????????????????if ($a[$i][$j]) ????????????????????array_push($v, $a[$i][$j]); ????????????} ????????????// for each temporary array ????????????for ($j = 0; $j < count($v); $j++)? ????????????{ ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if ($j < count($v) - 1 &&? ????????????????????$v[$j] == $v[$j + 1])? ????????????????{ ????????????????????// insert only one element? ????????????????????// as sum of two same element. ????????????????????array_push($w, 2 * $v[$j]); ????????????????????$j++; ????????????????} ????????????????else ????????????????????array_push($w, $v[$j]); ????????????} ????????????// filling the each? ????????????// row element to 0\. ????????????for ($j = 0; $j < $n; $j++) ????????????????$a[$i][$j] = 0; ????????????$j = $n - 1; ????????????// Copying the temporary? ????????????// array to the current row. ????????????for ($it = 0; $it != count($w); $it++) ????????????????????$a[$i][$j--] = $w[$it]; ????????} ????} ????// for left shift move ????else if ($d[0] == 'l')? ????{? ????????// for each row ????????for ($i = 0; $i < $n; $i++)? ????????{ ????????????$v = array(); $w = array(); ????????????$j = 0; ????????????// for each element of the? ????????????// row from left to right ????????????for ($j = 0; $j < $n; $j++)? ????????????{ ????????????????// if not 0 ????????????????if ($a[$i][$j]) ????????????????????array_push($v,? ???????????????????????????????$a[$i][$j]); ????????????} ????????????// for each temporary array ????????????for ($j = 0; $j < count($v); $j++)? ????????????{ ????????????????// if two element have ????????????????// same value at consecutive? ????????????????// position. ????????????????if ($j < count($v) - 1 && ????????????????????$v[$j] == $v[$j + 1])? ????????????????{ ????????????????????// insert only one element? ????????????????????// as sum of two same element. ????????????????????array_push($w, 2 * $v[$j]); ????????????????????$j++; ????????????????} ????????????????else ????????????????????array_push($w, $v[$j]); ????????????} ????????????// filling the each? ????????????// row element to 0\. ????????????for ($j = 0; $j < $n; $j++) ????????????????$a[$i][$j] = 0; ????????????$j = 0; ????????????for ($it = 0; $it != count($w); $it++) ????????????????????$a[$i][$j++] = $w[$it]; ????????} ????} ????// for down shift move. ????else if ($d[0] == 'd')? ????{ ????????// for each column ????????for ($i = 0; $i < $n; $i++)? ????????{ ????????????$v = array(); $w = array(); ????????????$j = 0; ????????????// for each element? ????????????// of column from ????????????// bottom to top ????????????for ($j = $n - 1; $j >= 0; $j--)? ????????????{ ????????????????// if not 0 ????????????????if ($a[$j][$i]) ????????????????????array_push($v, $a[$j][$i]); ????????????} ????????????// for each temporary array ????????????for ($j = 0; $j < count($v); $j++)? ????????????{ ????????????????// if two element have? ????????????????// same value at? ????????????????// consecutive position. ????????????????if ($j < count($v) - 1 &&? ????????????????????$v[$j] == $v[$j + 1])? ????????????????{ ????????????????????// insert only one element ????????????????????// as sum of two same element. ????????????????????array_push($w, 2 * $v[$j]); ????????????????????$j++; ????????????????} ????????????????else ????????????????????array_push($w, $v[$j]); ????????????} ????????????// filling the each? ????????????// column element to 0\. ????????????for ($j = 0; $j < $n; $j++) ????????????????$a[$j][$i] = 0; ????????????$j = $n - 1; ????????????// Copying the temporary array ????????????// to the current column ????????????for ($it = 0; $it != count($w); $it++) ????????????????????$a[$j--][$i] = $w[$it]; ????????} ????} ????// for up shift move ????else if ($d[0] == 'u')? ????{ ????????// for each column ????????for ($i = 0; $i < $n; $i++)? ????????{ ????????????$v = array(); $w = array(); ????????????$j = 0; ????????????// for each element of column ????????????// from top to bottom ????????????for ($j = 0; $j < $n; $j++)? ????????????{ ????????????????// if not 0 ????????????????if ($a[$j][$i]) ????????????????????array_push($v,? ???????????????????????????????$a[$j][$i]); ????????????} ????????????// for each temporary array ????????????for ($j = 0; $j < count($v); $j++)? ????????????{ ????????????????// if two element have same ????????????????// value at consecutive position. ????????????????if ($j < count($v) - 1 && ????????????????????$v[$j] == $v[$j + 1])? ????????????????{ ????????????????????// insert only one element? ????????????????????// as sum of two same element. ????????????????????array_push($w, 2 * $v[$j]); ????????????????????$j++; ????????????????} ????????????????else ????????????????????array_push($w, $v[$j]); ????????????} ????????????// filling the each? ????????????// column element to 0\. ????????????for ($j = 0; $j < $n; $j++) ????????????????$a[$j][$i] = 0; ????????????$j = 0; ????????????// Copying the temporary array ????????????// to the current column ????????????for ($it = 0; $it != count($w); $it++) ????????????????????$a[$j++][$i] = $w[$it]; ????????} ????} } // Driven Code $d = array("l"); $n = 5; $a = array( array(32, 3, 3, 3, 3), ????????????array(0, 0, 1, 0, 0), ????????????array(10, 10, 8, 1, 2), ????????????array(0, 0, 0, 0, 1), ????????????array(4, 5, 6, 7, 8)); moveMatrix($d, $n, $a); // Printing the final array for ($i = 0; $i < $n; $i++) { ????for ($j = 0; $j < $n; $j++) ????????echo ($a[$i][$j]." "); ????echo ("\n"); } // This code is contributed // by Manish Shaw(manishshaw1) ?> ``` **輸出**: ``` 32 6 6 0 0 1 0 0 0 0 20 8 1 2 0 1 0 0 0 0 4 5 6 7 8 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看