当前位置:网站首页>2048项目实现
2048项目实现
2022-07-05 06:18:00 【mentalps】
1 2048
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2048
{
public class MatrixOperation
{
// 计算矩阵的平滑性和连接性
public int[] SmoothAndNum(int[,] matrix)
{
int[,] matrixIndex = new int[5, 5];
int num = 0;
int smooth = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
int[] vs1 = MatrixMove(matrix, i, j, 1, ref matrixIndex);
int[] vs2 = MatrixMove(matrix, i, j, 2, ref matrixIndex);
int[] vs3 = MatrixMove(matrix, i, j, 3, ref matrixIndex);
smooth = smooth - vs1[0] - vs2[0] - vs3[0];
num = num + vs1[1] + vs2[1] + vs3[1];
}
}
return new int[2] {
smooth, num };
}
// 穷举所有可能的填充组合
//public List<List<int>> AllComb(int max,int min,int num)
// {
// List<int> comb = new List<int>();
// return;
// }
public static int[] MatrixMove(int[,] matrix, int x, int y, int z, ref int[,] matrixIndex)
{
int num = 0;
int smooth = 0;
switch (z)
{
case 1:
if (x + 1 <= matrix.GetUpperBound(1))
{
smooth = (int)Math.Abs(Math.Log(matrix[x, y], 2) - Math.Log(matrix[x + 1, y], 2));
if (smooth == 0)
{
if (matrixIndex[x, y] == 0)
{
matrixIndex[x, y] = 1;
num = num + 1;
}
if (matrixIndex[x + 1, y] == 0)
{
matrixIndex[x + 1, y] = 1;
num = num + 1;
}
}
}
else
{
smooth = 0;
}
break;
case 2:
if ((x + 1 <= matrix.GetUpperBound(1)) && (y + 1 <= matrix.GetUpperBound(0)))
{
smooth = (int)Math.Abs(Math.Log(matrix[x, y], 2) - Math.Log(matrix[x + 1, y + 1], 2));
if (smooth == 0)
{
if (matrixIndex[x, y] == 0)
{
matrixIndex[x, y] = 1;
num = num + 1;
}
if (matrixIndex[x + 1, y + 1] == 0)
{
matrixIndex[x + 1, y + 1] = 1;
num = num + 1;
}
}
}
else
{
smooth = 0;
}
break;
case 3:
if (y + 1 <= matrix.GetUpperBound(0))
{
smooth = (int)Math.Abs(Math.Log(matrix[x, y], 2) - Math.Log(matrix[x, y + 1], 2));
if (smooth == 0)
{
if (matrixIndex[x, y] == 0)
{
matrixIndex[x, y] = 1;
num = num + 1;
}
if (matrixIndex[x, y + 1] == 0)
{
matrixIndex[x, y + 1] = 1;
num = num + 1;
}
}
}
else
{
smooth = 0;
}
break;
}
return new int[2] {
smooth, num };
}
public int[,] FillMatrix(int[,] matrix)
{
List<Tuple<int, int>> fillSteps = new List<Tuple<int, int>>();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (matrix[i, j] == 0)
{
Tuple<int, int> pos = new Tuple<int, int>(i, j);
fillSteps.Add(pos);
}
}
}
return matrix;
}
// total:需要填充的个数
public static int [] AllComb(int total,int [] nums,int start, ref List<int []> all_list ,int [] all)
{
if (start < total) {
for (int i = 0; i < nums.Length; i++)
{
all[start] = nums[i];
AllComb(total, nums, start+1,ref all_list,all);
int[] b = new int[total];
Array.Copy(all, b, total);
all_list.Add(b);
}
return all;
}
else
{
return all;
}
}
static void Main(string[] args)
{
int[] nums = {
2, 4, 8 };
int num = 3;
List<int []> all = new List<int []>();
int[] s = new int[num];
AllComb(3, nums, 0,ref all, s);
for(int i=0;i<all.Count;i++)
{
for(int j=0;j<num;j++)
{
Console.WriteLine(all[i][j]);
}
}
}
}
}
边栏推荐
- MySQL advanced part 1: triggers
- How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
- Applicable to Net free barcode API [off] - free barcode API for NET [closed]
- 【LeetCode】Easy | 20. Valid parentheses
- MIT-6874-Deep Learning in the Life Sciences Week 7
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- C job interview - casting and comparing - C job interview - casting and comparing
- 1.14 - assembly line
- 做 SQL 性能优化真是让人干瞪眼
- Open source storage is so popular, why do we insist on self-development?
猜你喜欢
随机推荐
Single chip computer engineering experience - layered idea
1040 Longest Symmetric String
SQLMAP使用教程(一)
1039 Course List for Student
什么是套接字?Socket基本介绍
MySQL advanced part 2: the use of indexes
Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
[rust notes] 14 set (Part 2)
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Sum of three terms (construction)
C job interview - casting and comparing - C job interview - casting and comparing
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
打印机脱机时一种容易被忽略的原因
Records of some tools 2022
[rust notes] 15 string and text (Part 1)
MySQL advanced part 1: View
What is socket? Basic introduction to socket
[rust notes] 17 concurrent (Part 2)