int GetFCost(NumArray openSet) { int fCost = 0; fCost = openSet.gCost + openSet.hCost; return(fCost); }
public List <NumArray> GetChild(NumArray currArray) { List <NumArray> childArray = new List <NumArray>(); int emptyX = 0; int emptyY = 0; for (int y = 0; y < blockLine; y++) { for (int x = 0; x < blockLine; x++) { //번호 blockLine(3)이 비어있는 블럭 if (currArray.myArray[x, y] == blockLine) { emptyX = x; emptyY = y; } } } if (checkFirst == true) { tempArray = new NumArray[1000]; for (int i = 0; i < 1000; i++) { tempArray[i] = gameObject.AddComponent <NumArray>(); tempArray[i].MakeLine(blockLine); } checkFirst = false; } int[,] copyCrr = new int[blockLine, blockLine]; copyCrr = currArray.myArray.Clone() as int[, ]; for (int i = count; i < 1000; i++) { tempArray[i].myArray = copyCrr.Clone() as int[, ]; } //오른쪽 왼쪽 위 아래 Vector2[] vecD = { new Vector2(1, 0), new Vector2(-1, 0), new Vector2(0, 1), new Vector2(0, -1) }; for (int i = 0; i < vecD.Length; i++) { int checkX = emptyX + (int)vecD[i].x; int checkY = emptyY + (int)vecD[i].y; if ((checkX >= 0 && checkX < blockLine) && (checkY >= 0 && checkY < blockLine)) { int tempNum = tempArray[count].myArray[emptyX, emptyY]; tempArray[count].myArray[emptyX, emptyY] = tempArray[count].myArray[checkX, checkY]; tempArray[count].myArray[checkX, checkY] = tempNum; tempArray[count].direction = vecD[i]; childArray.Add(tempArray[count]); count++; } } return(childArray); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); int[] nums = { -2, 0, 3, -5, 2, -1 }; NumArray obj = new NumArray(nums); int param_1 = obj.SumRange(2, 5); }
/* * Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. * * Example: * Given nums = [-2, 0, 3, -5, 2, -1] * sumRange(0, 2) -> 1 * sumRange(2, 5) -> -1 * sumRange(0, 5) -> -3 * * Note: * You may assume that the array does not change. * There are many calls to sumRange function. */ static void Main(string[] args) { var obj = new NumArray(new [] { -2, 0, 3, -5, 2, -1 }); obj.SumRange(0, 2); obj.SumRange(2, 5); obj.SumRange(0, 5); }
public void NumArrayTest() { var test = new NumArray(new[] { 1, 3, 5 }); Assert.AreEqual(9, test.SumRange(0, 2)); test.Update(1, 2); Assert.AreEqual(8, test.SumRange(0, 2)); }
public void Test1() { var sut = new NumArray(new int[] { -2, 0, 3, -5, 2, -1 }); Assert.AreEqual(1, sut.SumRange(0, 2)); Assert.AreEqual(-1, sut.SumRange(2, 5)); Assert.AreEqual(-3, sut.SumRange(0, 5)); }
public void NumArrayTest_1() { var test = new NumArray(new[] { -1 }); Assert.AreEqual(-1, test.SumRange(0, 0)); test.Update(0, 1); Assert.AreEqual(1, test.SumRange(0, 0)); }
public void Run() { var n = new NumArray(new int[] { -1 }); n.SumRange(0, 0); n.Update(0, 1); n.SumRange(0, 0); }
public NumMatrix(int[][] matrix) { _numArrays = new NumArray[matrix.Length]; for (int i = 0; i < matrix.Length; i++) { _numArrays[i] = new NumArray(matrix[i]); } }
public override void Run(string[] args) { var narr = new NumArray(new int[] { 1, 3, 5, 9, 4, 6 }); Debug.Assert(() => narr.SumRange(0, 2), 9); Debug.Assert(() => narr.SumRange(0, 5), 28); narr.Update(1, 2); Debug.Assert(() => narr.SumRange(0, 2), 8); }
static void Main(string[] args) { int[] arr = new int[] { 15, 13, 5, 3, 15 }; NumArray na = new NumArray(arr); Console.WriteLine(na.SumRange(0, 2)); Console.ReadKey(); }
public void NumArrayTest_2() { var test = new NumArray(new[] { 9, -8 }); test.Update(0, 3); Assert.AreEqual(-8, test.SumRange(1, 1)); Assert.AreEqual(-5, test.SumRange(0, 1)); test.Update(1, -3); Assert.AreEqual(0, test.SumRange(0, 1)); }
public void TestMethod303() { int[] input = { -2, 0, 3, -5, 2, -1 }; int expected = 1; NumArray test = new NumArray(input); //int[] sumList = test.NumArray(input); int actual = test.SumRange(0, 2); Console.WriteLine(actual); Assert.AreEqual(expected, actual); }
//블럭 배열 생성---------------------------------------------- void CreateBlock() { shuffleB = new EventBlock[blockLine, blockLine]; //초기 번호 배열 targetArray = gameObject.AddComponent <NumArray>(); targetArray.MakeLine(blockLine); for (int y = 0; y < blockLine; y++) { for (int x = 0; x < blockLine; x++) { int myNum = x + (blockLine * y) + 1; targetArray.MakeArray(x, y, myNum); } } numArray = gameObject.AddComponent <NumArray>(); numArray.MakeLine(blockLine); //분할 이미지 Texture2D[,] Image = SetImage.ImageBlock(myImage, blockLine); for (int y = 0; y < blockLine; y++) { for (int x = 0; x < blockLine; x++) { //Quad로 블럭 생성 GameObject myBlock = GameObject.CreatePrimitive(PrimitiveType.Quad); myBlock.transform.localPosition = new Vector2(-0.5f * (blockLine - 1), -0.5f * (blockLine - 1) - 1) + new Vector2(x, y); //이벤트 설정 eventBlock = myBlock.AddComponent <EventBlock>(); //좌표, 이미지 설정 eventBlock.Init(new Vector2(x, y), Image[x, y]); //셔플 배열에 블럭 넣기 shuffleB[x, y] = eventBlock; //번호 배열 정보 int myNum = x + (blockLine * y) + 1; //변화하는 번호 배열 numArray.MakeArray(x, y, myNum); //초기 비어있는 블럭 if (x == blockLine - 1 && y == 0) { myBlock.SetActive(false); emptyBlock = eventBlock; } //마우스 입력이 일어나면 eventBlock.BlockPressed += MoveBlock; } } }
public IActionResult Numbers() { NumArray numarray = new NumArray(); numarray.numarray = new int[] { 1, 2, 3, 4, 5, }; return(View(numarray)); }
public void Test1() { NumArray array = new NumArray(new int[] { -2, 0, 3, -5, 2, -1 }); int result = array.SumRange(0, 2); Assert.Equal(1, result); NumArray2 array2 = new NumArray2(new int[] { -2, 0, 3, -5, 2, -1 }); int result2 = array2.SumRange(2, 5); Assert.Equal(result2, -1); }
public void Test1() { //Given nums = [-2, 0, 3, -5, 2, -1] //sumRange(0, 2)-> 1 //sumRange(2, 5)-> - 1 //sumRange(0, 5)-> - 3 var nums = new int[] { -2, 0, 3, -5, 2, -1 }; var numArr = new NumArray(nums); Assert.AreEqual(1, numArr.SumRange(0, 2)); Assert.AreEqual(-1, numArr.SumRange(2, 5)); Assert.AreEqual(-3, numArr.SumRange(0, 5)); }
public void NumArrayTest_3() { var test = new NumArray(new[] { -28, -39, 53, 65, 11, -56, -65, -39, -43, 97 }); Assert.AreEqual(-56 - 65, test.SumRange(5, 6)); test.Update(9, 27); Assert.AreEqual(118, test.SumRange(2, 3)); Assert.AreEqual(-104, test.SumRange(6, 7)); test.Update(1, -82); test.Update(3, -72); Assert.AreEqual(-221, test.SumRange(3, 7)); Assert.AreEqual(-293, test.SumRange(1, 8)); test.Update(5, 13); test.Update(4, -67); }
int GetHCost(NumArray childArray) { int hCost = 0; for (int curY = 0; curY < blockLine; curY++) { for (int curX = 0; curX < blockLine; curX++) { int num = childArray.myArray[curX, curY]; int tarX = (num - 1) % blockLine; int tarY = (num - 1) / blockLine; hCost += (Mathf.Abs(tarX - curX) + Mathf.Abs(tarY - curY)); } } return(hCost); }
public void Main(string args) { string[] flds = args.Replace("[", "").Replace("]", "").Split(','); int[] nums = set_array_int(flds); Console.WriteLine("nums = " + output_array_int(nums)); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); NumArray obj = new NumArray(nums); Console.WriteLine("sumRange(0, 2) = " + obj.SumRange(0, 2)); Console.WriteLine("sumRange(2, 5) = " + obj.SumRange(2, 5)); Console.WriteLine("sumRange(0, 5) = " + obj.SumRange(0, 5)); sw.Stop(); Console.WriteLine("Execute time ... " + sw.ElapsedMilliseconds.ToString() + "ms\n"); }
private void NumArray_Main(string[] ope, string[] para) { if (ope.Length != para.Length) { return; } if (ope.Length <= 0 || para.Length <= 0) { return; } int[] nums = set_array_int(para[0].Split(',')); NumArray nm = new NumArray(nums); for (int n = 0; n < ope.Length; n++) { if (ope[n] == "NumArray") { Console.WriteLine("NumArray()"); } else if (ope[n] == "update") { string[] flds = para[n].Split(','); int i = int.Parse(flds[0]); int val = int.Parse(flds[1]); nm.Update(i, val); Console.WriteLine("update(" + i.ToString() + ", " + val.ToString() + ")"); } else if (ope[n] == "sumRange") { string[] flds = para[n].Split(','); int i = int.Parse(flds[0]); int j = int.Parse(flds[1]); int sum = nm.SumRange(i, j); Console.WriteLine("sumRange(" + i.ToString() + ", " + j.ToString() + ") = " + sum.ToString()); } } }
public void ParseStrings() { decimal num; foreach (var stringNum in StringArray) { if (Decimal.TryParse(stringNum, out num)) { Min = (num < Min) ? num : Min; Max = (num > Max) ? num : Max; Total += num; NumArray.Add(num); } else { ErrCheck = true; break; } } Average = Total / NumCount; }
public void NumArrayTests() { /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.SumRange(i,j); * * Given nums = [-2, 0, 3, -5, 2, -1] * * sumRange(0, 2) -> 1 * sumRange(2, 5) -> -1 * sumRange(0, 5) -> -3 */ int[] nums = new int[] { -2, 0, 3, -5, 2, -1 }; NumArray obj = new NumArray(nums); int param_1 = obj.SumRange(0, 2); param_1 = obj.SumRange(2, 5); param_1 = obj.SumRange(0, 5); }
public void TestMethod2(int maxLength, int maxValue, int maxOperationCount, int repeatCount) { Repeat(repeatCount, () => { var nums = GenerateIntegerArray(maxLength, maxValue); var numArray = new NumArray(nums); var numsCopy = nums.ToArray(); var operationCount = nums.Any() ? Random.Next(maxOperationCount) + 1 : 0; //System.Console.WriteLine(JsonConvert.SerializeObject(numsCopy)); for (var i = 0; i < operationCount; ++i) { switch(Random.Next(2)) { case 0: var index = Random.Next(nums.Length); var newValue = Random.Next(maxValue + 1); numArray.Update(index, newValue); numsCopy[index] = newValue; //System.Console.WriteLine(JsonConvert.SerializeObject(numsCopy)); break; default: var indexI = Random.Next(nums.Length); var indexJ = Random.Next(nums.Length); if (indexI > indexJ) { var t = indexI; indexI = indexJ; indexJ = t; } Assert.AreEqual(Sum(numsCopy, indexI, indexJ), numArray.SumRange(indexI, indexJ), $"i = {indexI} j = {indexJ} nums = {JsonConvert.SerializeObject(numsCopy)}"); break; } } }); }
public void RunProblem() { var t = new NumArray(new int[] { -2, 0, 3, -5, 2, -1 }); var temp = t.SumRange(0, 2); if (temp != 1) { throw new Exception(); } temp = t.SumRange(2, 5); if (temp != -1) { throw new Exception(); } temp = t.SumRange(0, 5); if (temp != -3) { throw new Exception(); } t = new NumArray(new int[] { }); }
void RetracePath(NumArray startNode, NumArray endArray) { List <Vector2> path = new List <Vector2>(); NumArray currentArray = gameObject.AddComponent <NumArray>(); currentArray = endArray; while (true) { int checkFin = 0; for (int y = 0; y < blockLine; y++) { for (int x = 0; x < blockLine; x++) { if (currentArray.myArray[x, y] == startNode.myArray[x, y]) { checkFin++; } } } if (checkFin == blockLine * blockLine) { break; } path.Add(currentArray.direction); currentArray = currentArray.parent; } path.Reverse(); Vector2 right = new Vector2(1, 0); Vector2 left = new Vector2(-1, 0); Vector2 up = new Vector2(0, 1); Vector2 down = new Vector2(0, -1); List <string> saveSol = new List <string>(); for (int i = 0; i < path.Count; i++) { if (path[i] == right) { saveSol.Add("Right "); print("right"); } if (path[i] == left) { saveSol.Add("Left "); print("left"); } if (path[i] == up) { saveSol.Add("Up "); print("up"); } if (path[i] == down) { saveSol.Add("Down "); print("down"); } } string solution = "Solution: "; foreach (string sol in saveSol) { solution = solution.ToString() + sol.ToString(); } solText.text = solution; }
Console.WriteLine(Z5(NumArray, strk).ToString());
public static void Run() { Console.WriteLine("Start: {0}", DateTime.Now); int[] nums = new int[] { 1, 2, 3, 4, 5}; NumArray numArray = new NumArray(nums); Console.WriteLine(numArray.SumRange(0, 1)); Console.WriteLine(numArray.SumRange(1, 2)); numArray.Update(1, 10); Console.WriteLine(numArray.SumRange(1, 2)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void runner() { NumArray test = new NumArray(new int[] { -2, 0, 3, -5, 2, -1 }); foreach ((int, int)c in new (int, int)[] { (0, 2), (2, 5), (0, 5) })
void FindPath(NumArray startArray, NumArray tar_Array) { print("FindPath Start!"); //시작 숫자 배열 startArray.gCost = 0; startArray.hCost = GetHCost(startArray); //타겟 숫자 배열 NumArray targetArray = tar_Array; //확인 필요한 배열들 List <NumArray> openSet = new List <NumArray>(); //확인 했던 배열들 //HashSet<NumArray> closedSet = new HashSet<NumArray>(); List <NumArray> closedSet = new List <NumArray>(); openSet.Add(startArray); while (openSet.Count > 0) { //openSet에서 가장 작은 fCost 찾기 NumArray currArray = openSet[0]; for (int i = 0; i < openSet.Count; i++) { if ((GetFCost(openSet[i]) < GetFCost(currArray)) || (GetFCost(openSet[i]) == GetFCost(currArray) && openSet[i].hCost < currArray.hCost)) { currArray = openSet[i]; } } //선택된 배열 openSet에서 삭제 openSet.Remove(currArray); //선택된 배열 closedSet에 삽입 closedSet.Add(currArray); int checkFin = 0; for (int y = 0; y < blockLine; y++) { for (int x = 0; x < blockLine; x++) { if (currArray.myArray[x, y] == targetArray.myArray[x, y]) { checkFin++; } } } //타겟과 일치하면 정지 if (checkFin == blockLine * blockLine) { print("FindPath End!"); RetracePath(startArray, currArray); return; } //이동 할 수 있는 방향으로 움직인 배열 List <NumArray> getChild = GetChild(currArray); foreach (NumArray childArray in getChild) { for (int i = 0; i < closedSet.Count; i++) { if (closedSet[i].myArray == childArray.myArray) { continue; } } bool check = true; for (int i = 0; i < openSet.Count; i++) { if (openSet[i].myArray == childArray.myArray) { check = false; break; } } if (check == true) { childArray.gCost = currArray.gCost + 1; childArray.hCost = GetHCost(childArray); childArray.parent = currArray; openSet.Add(childArray); } } } }