Simulation questions,
Tips
This is titled 2048 enhanced version:
If there is a line of 32 16 16 0 Then the right result is 0 0 0 64 The left result is 32 32 0 0.
That is, after merging, if there can be merged later, the merge is not counted.
This is the tip of the topic, do as required.
Idea: For example to the right, then swipe from the right I to the left, sweep to a number if the number is equal to the I position, then I position plus the number, continue to sweep to the left until the end or hit a number and I position the number is not equal. After sweep, the number is all to the right end.
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include < vector> #include <list> #include <algorithm>using namespace Std;int a[5][5];//Move the numbers all the way to the corresponding direction void arrange_up () {for (int j=0;j<4;j++) {for (int. i=0;i<4;i++) {for (int k=i;k<4;k++) {if (A[k][j]) {if (k!=i) {A[i][j] = A[k][j];a K [j] = 0;} Break;}}}} void Arrange_down () {for (int. j=0;j<4;j++) {for (int. i=3;i>=0;i--) {for (int k=i;k>=0;k--) {if (A[k][j]) {if (k!=i) {A[i][j] = a[k][j];a[k][j] = 0;} Break;}}}} void Arrange_right () {for (int. i=0;i<4;i++) {for (int j=3;j>=0;j--) {-(int k=j;k>=0;k--) {if (A[i][k]) {if (k!=j ) {A[i][j] = a[i][k];a[i][k] = 0;} Break;}}}} void Arrange_left () {for (int i=0;i<4;i++) {for (int. j=0;j<4;j++) {for (int k=j;k<4;k++) {if (A[i][k]) {if (k!=j) {A I [j] = a[i][k];a[i][k] = 0;} Break;}}}} outputs void output () {for (int. i=0;i<4;i++) {for (int j=0;j<4;j++) {printf ("%5d", A[i][j]), if (j!=3) printf (""); else printf ("\ n"); }}}int Main () {int t;scanf ("%d", &t);(t--) {for (int. i=0;i<4;i++) {for (int j=0;j<4;j++) {scanf ("%d", &a[i][j]);}} int q;scanf ("%d", &q), while (q--) {int t;scanf ("%d", &t), if (t==1) {//up for (int j=0;j<4;j++) {//column for (int i=0;i <4;i++) {//Line if (A[i][j]) {//start from this point for (int k=i+1;k<4;k++) {//Swipe down if (A[i][j]==a[k][j]) {//if equal, then add a[i][j] + = a[k][j]; A[K][J] = 0;} else if (A[k][j]) {//If a value is not equal to exit break;}}}} Organize digital arrange_up ();} else if (t==2) {//down for (int j=0;j<4;j++) {//column for (int. i=3;i>=0;i--) {if (A[i][j]) {for (int k=i-1;k>=0;k--) {if (a[ I][j]==a[k][j]) {A[i][j] + = a[k][j];a[k][j] = 0;} else if (A[k][j]) {break;}}}} Arrange_down ();} else if (t==3) {//left for (int i=0;i<4;i++) {//Line for (int. j=0;j<4;j++) {if (A[i][j]) {for (int k=j+1;k<4;k++) {if (A[i] [J]==a[i][k]) {A[i][j] + = a[i][k];a[i][k] = 0;} else if (A[i][k]) {break;}}}} Arrange_left ();} else if (t==4) {//right for (int i=0;i<4;i++) {//Line for (int. j=3;j>=0;j--) {if (A[i][j]) {for (int k=j-1;k>=0;k--) {if (a[ I][j]==a[i][k]) {A[i][j] + = a[i][k];a[i][k] = 0;} else if (A[i][k]) {break;}}}} ArranGe_right ();} Output ();}} return 0;}
Analog 2048 (Nyoj 1187)