2048 move

 avatar
unknown
c_cpp
3 years ago
759 B
6
Indexable
#include <iostream>
#include <vector>
using namespace std;

vector<int> moveElementToEnd(vector<int> array)
{
    int i = 0;
    int j = array.size() - 1;
    while (i < j) {
        while (i < j && array[j] == 0)
            j--;

        if (array[i] == 0)
            swap(array[i], array[j]);
        i++;
    }
    return array;
}

int main() {
    vector<vector<int>>  plateau,newPlateau;
    plateau =  { { 0, 2, 4, 4 }, { 2, 0, 0, 4 }};

    for (int i = 0; i < plateau.size() ; i++) {
        vector<int> ans = moveElementToEnd(plateau[i]);
        newPlateau.push_back(ans);
    }

    for (int i = 0; i < newPlateau.size() ; i++) {
        for (int j = 0; j < newPlateau[i].size() ; j++) {
            cout << newPlateau[i][j];
        }
    }

}