/// <summary> /// rotate 90 three time /// </summary> /// <param name="s"></param> /// <returns>new shape after rotate 270 degree</returns> public static shape Rotate270(shape s) { shape newShape = Rotate180(s); newShape = Rotate90(newShape); newShape.state = "R270"; return(newShape); }
public static shape generatePiece(shape s) { s.pieces[0] = s; s.pieces[1] = moveParallel.Rotate90(s); s.pieces[2] = moveParallel.Rotate180(s); s.pieces[3] = moveParallel.Rotate270(s); Parallel.Invoke(() => { s.pieces[4] = moveParallel.FlipFlopVertical(s); }, //close second Action () => { s.pieces[5] = moveParallel.FlipFlopHorzental(s); }, //close second Action () => { s.pieces[6] = moveParallel.FlipFlopHorzental(s.pieces[1]); s.pieces[6].state = "R90 + FH"; }, //close second Action () => { s.pieces[7] = moveParallel.FlipFlopVertical(s.pieces[1]); s.pieces[7].state = "R90 + FV"; }, //close second Action () => { s.pieces[8] = moveParallel.FlipFlopHorzental(s.pieces[2]); s.pieces[8].state = "R180 + FH"; }, //close second Action () => { s.pieces[9] = moveParallel.FlipFlopVertical(s.pieces[2]); s.pieces[9].state = "R180 + FV"; }, //close second Action () => { s.pieces[10] = moveParallel.FlipFlopHorzental(s.pieces[3]); s.pieces[10].state = "R270 + FH"; }, //close second Action () => { s.pieces[11] = moveParallel.FlipFlopVertical(s.pieces[3]); s.pieces[11].state = "R270 + FV"; }); return(s); }
/// <summary> /// send to our algorthim all combination for shapes /// make 2D array busyPostion have 5 postion (0-4) /// if Noshape 4 i will use 4 element only /// if Noshape 5 i will use all element in 2D /// col 1 have (0=>empty,1=>busy) /// </summary> /// <param name="matrix"></param> /// <param name="allshape"> array of shape have 4 /5 shape</param> /// public shape[] copyFun(shape[] allshape) { shape[] copy = new shape[NoShape]; for (int i = 0; i < allshape.Length; i++) { { copy[i] = allshape[i]; } } return(copy); }
} // end function public void Insert(int[,] matrix, shape s, int rowMatrix, int colMatrix) { for (int i = 0, row = rowMatrix; i < s.NoRow; i++, row++) { for (int j = 0, col = colMatrix; j < s.NoCol; j++, col++) { if (s.data[i, j] == 1) { matrix[row, col] = s.value; } } } }
public static shape FlipFlopHorzental(shape s) { shape newShape = new shape(s.NoRow, s.NoCol, s.value); for (int i = 0; i < s.NoRow; i++) { for (int j = 0; j < s.NoCol; j++) { int newRow = i; //new row equal old row int newCol = s.NoCol - 1 - j; // new col equal large col -1- old col newShape.data[newRow, newCol] = s.data[i, j]; } } newShape.state = "FH"; return(newShape); }
/// <summary> /// this function rotate shape 90 degree /// </summary> /// <param name="s"></param> /// <returns>new shape after rotate 90</returns> public static shape Rotate90(shape s) { shape newShape = new shape(s.NoCol, s.NoRow, s.value); for (int i = 0; i < s.NoRow; i++) { for (int j = 0; j < s.NoCol; j++) { int newRow = s.NoCol - 1 - j; // new row equal large col -1 - old row int newCol = i; // new col equal old row newShape.data[newRow, newCol] = s.data[i, j]; } } newShape.state = "R90"; return(newShape); }
public bool checkInsert(int[,] matrix, shape s) { bool flag = true; for (int row = 0; row < 4; row++) { if (4 - row >= s.NoRow) { for (int col = 0; col < 4; col++) { if (4 - col >= s.NoCol) { flag = true; for (int i = 0, rowMatrix = row; i < s.NoRow; i++, rowMatrix++) { for (int j = 0, colMatrix = col; j < s.NoCol; j++, colMatrix++) { if (s.data[i, j] == 1 && !(matrix[rowMatrix, colMatrix] == 0)) { flag = false; break; } } // end col shape loop if (!flag) { break; } } // end row shape loop if (flag) { Insert(matrix, s, row, col); return(true); } } // end check col } // end col matrix loop } // end if check row } // end row matrix loop return(false); } // end function
public static shape generatePiece(shape s) { s.pieces[0] = s; s.pieces[1] = move.Rotate90(s); s.pieces[2] = move.Rotate180(s); s.pieces[3] = move.Rotate270(s); s.pieces[4] = move.FlipFlopVertical(s); s.pieces[5] = move.FlipFlopHorzental(s); s.pieces[6] = move.FlipFlopHorzental(s.pieces[1]); s.pieces[6].state = "R90 + FH"; s.pieces[7] = move.FlipFlopVertical(s.pieces[1]); s.pieces[7].state = "R90 + FV"; s.pieces[8] = move.FlipFlopHorzental(s.pieces[2]); s.pieces[8].state = "R180 + FH"; s.pieces[9] = move.FlipFlopVertical(s.pieces[2]); s.pieces[9].state = "R180 + FV"; s.pieces[10] = move.FlipFlopHorzental(s.pieces[3]); s.pieces[10].state = "R270 + FH"; s.pieces[11] = move.FlipFlopVertical(s.pieces[3]); s.pieces[10].state = "R270 + FV"; return(s); }
//async Task public async Task perpar(int[,] matrix, shape[] allshape) { shape[] ConstShape = new shape[5]; int[][] busyPosition = new int[5][]; for (int i = 0; i < NoShape; i++) { ConstShape[i] = allshape[i]; // allshape[i] = new shape(1, 1, 0); busyPosition[i] = new int[1]; busyPosition[i][0] = 0; } for (int a = 0; a < NoShape; a++) { for (int i = 0; i < NoShape; i++) { allshape[i] = new shape(1, 1, 0); busyPosition[i][0] = 0; } allshape[a] = ConstShape[0]; busyPosition[a][0] = 1; Console.WriteLine("a is:" + a.ToString()); for (int b = a, IB = 0; IB < NoShape - 1; IB++, b++) { int pos = (b + 1) % NoShape; Console.WriteLine("b is:" + pos.ToString()); allshape[pos] = ConstShape[1]; busyPosition[pos][0] = 1; for (int c = 0; c < NoShape; c++) { if (busyPosition[c][0] == 0) { Console.WriteLine("c is:" + c.ToString()); busyPosition[c][0] = 1; allshape[c] = ConstShape[2]; for (int d = 0; d < NoShape; d++) { if (busyPosition[d][0] == 0) { Console.WriteLine("d is:" + d.ToString()); busyPosition[d][0] = 1; allshape[d] = ConstShape[3]; if (NoShape == 5) { for (int e = 0; e < NoShape; e++) { if (busyPosition[e][0] == 0) { allshape[e] = ConstShape[4]; shape[] copyarray = copyFun(allshape); List <Task> TaskList = new List <Task>(); TaskList.Add(OurAlgorthim(matrix, copyarray)); Task.WaitAll(TaskList.ToArray()); // OurAlgorthim(matrix, allshape); // ThreadPool.QueueUserWorkItem(state => OurAlgorthim(matrix, allshape)); } } } else { shape[] copyarray = copyFun(allshape); List <Task> TaskList = new List <Task>(); TaskList.Add(OurAlgorthim(matrix, copyarray)); Task.WaitAll(TaskList.ToArray()); } busyPosition[d][0] = 0; } } busyPosition[c][0] = 0; } } busyPosition[pos][0] = 0; } //end b busyPosition[a][0] = 0; } // end a }
/// <summary> /// send to our algorthim all combination for shapes /// make 2D array busyPostion have 5 postion (0-4) /// if Noshape 4 i will use 4 element only /// if Noshape 5 i will use all element in 2D /// col 1 have (0=>empty,1=>busy) /// </summary> /// <param name="matrix"></param> /// <param name="allshape"> array of shape have 4 /5 shape</param> public void perpar(int[,] matrix, shape[] allshape) { shape[] ConstShape = new shape[5]; int[][] busyPosition = new int[5][]; for (int i = 0; i < NoShape; i++) { ConstShape[i] = allshape[i]; //allshape[i] = new shape(1, 1, 0); busyPosition[i] = new int[1]; busyPosition[i][0] = 0; } for (int a = 0; a < NoShape; a++) { for (int i = 0; i < NoShape; i++) { allshape[i] = new shape(1, 1, 0); busyPosition[i][0] = 0; } allshape[a] = ConstShape[0]; busyPosition[a][0] = 1; Console.WriteLine("a is:" + a.ToString()); for (int b = a, IB = 0; IB < NoShape - 1; IB++, b++) { int pos = (b + 1) % NoShape; Console.WriteLine("b is:" + pos.ToString()); allshape[pos] = ConstShape[1]; busyPosition[pos][0] = 1; for (int c = 0; c < NoShape; c++) { if (busyPosition[c][0] == 0) { Console.WriteLine("c is:" + c.ToString()); busyPosition[c][0] = 1; allshape[c] = ConstShape[2]; for (int d = 0; d < NoShape; d++) { if (busyPosition[d][0] == 0) { Console.WriteLine("d is:" + d.ToString()); busyPosition[d][0] = 1; allshape[d] = ConstShape[3]; if (NoShape == 5) { for (int e = 0; e < NoShape; e++) { if (busyPosition[e][0] == 0) { Console.WriteLine("e is:" + e.ToString()); allshape[e] = ConstShape[4]; for (int i = 0; i < 4; i++) { Console.WriteLine(allshape[i].value); } Console.WriteLine("_________________"); OurAlgorthim(matrix, allshape); } } } else { OurAlgorthim(matrix, allshape); } busyPosition[d][0] = 0; } } busyPosition[c][0] = 0; } } busyPosition[pos][0] = 0; } //end b busyPosition[a][0] = 0; } // end a }
public ParallelForm() { InitializeComponent(); String text = FileParallel.ReadFile(); try { string[] lines = text.Split('\n'); int Noshape = int.Parse(lines[0]); Console.WriteLine(lines[0]); algorthimParallel algo = new algorthimParallel(Noshape); shape[] allshape = new shape[Noshape]; int prev_row = 1; for (int i = 0; i < Noshape; i++) { String[] splitLine2 = lines[prev_row].Split(' '); Console.WriteLine(splitLine2[0] + splitLine2[1]); int row = int.Parse(splitLine2[0]); int col = int.Parse(splitLine2[1]); shape sh = new shape(row, col, i + 1); for (int k = 0; k < row; k++) { string[] spiltRow = lines[prev_row + 1 + k].Split(' '); int[] binRow = new int[col]; for (int r = 0; r < col; r++) { binRow[r] = int.Parse(spiltRow[r]); } for (int j = 0; j < col; j++) { sh.data[k, j] = binRow[j]; Console.Write(sh.data[k, j]); } Console.WriteLine(""); } allshape[i] = sh; prev_row = prev_row + row + 1; } Parallel.For(0, Noshape, i => { allshape[i] = moveParallel.generatePiece(allshape[i]); }); int[,] matrix = new int[4, 4]; Parallel.For(0, 4, i => { Parallel.For(0, 4, j => { matrix[i, j] = 0; }); }); Stack <shape> CurrentNode; CurrentNode = new Stack <shape>(); algo.perpar(matrix, allshape); //ThreadPool.QueueUserWorkItem(state => algo.perpar(matrix, allshape)); List <ResultParallel> res = algorthimParallel.result; string[] shapeState = new String[Noshape]; foreach (ResultParallel x in algorthimParallel.result) { for (int i = 0; i < Noshape; i++) { shapeState[i] = x.shape[i].value.ToString() + " = " + x.shape[i].state; } CreateSquare(4, x.matrix); WriteState(Noshape, shapeState); /* Parallel.Invoke(() => * { * CreateSquare(4, x.matrix); * * }, // close first Action * * () => * { * WriteState(Noshape, shapeState); * * * }); */ } if (res.Count() == 0) { var noSol = new Label(); noSol.Text = "Sooorry No Solution :'("; noSol.SetBounds(140, y + 30, 350, 90); noSol.Font = new System.Drawing.Font("Mistral", 25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0))); noSol.ForeColor = Color.DarkGoldenrod; this.Controls.Add(noSol); System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox(); //rsz_2.jpg pictureBox1.Image = Image.FromFile("rsz_2.jpg"); pictureBox1.Location = new System.Drawing.Point(228, 180); pictureBox1.SetBounds(150, y + 130, 700, 700); pictureBox1.Name = "NoSol"; pictureBox1.Size = new System.Drawing.Size(300, 300); pictureBox1.TabIndex = 2; pictureBox1.TabStop = false; this.Controls.Add(pictureBox1); } else { var thanks = new Label(); thanks.Text = "( " + algorthimParallel.result.Count().ToString() + " ) Shape "; thanks.SetBounds(230, y, 250, 80); thanks.Font = new System.Drawing.Font("Mistral", 30F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0))); thanks.ForeColor = Color.PaleVioletRed; this.Controls.Add(thanks); var space = new Label(); space.Text = " "; space.SetBounds(230, y + 80, 50, 30); this.Controls.Add(space); } } catch { var noSol = new Label(); noSol.Text = "Enter vaild input please :'("; noSol.SetBounds(140, y + 30, 350, 90); noSol.Font = new System.Drawing.Font("Mistral", 25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0))); noSol.ForeColor = Color.DarkGoldenrod; this.Controls.Add(noSol); System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox(); //1649616.gif pictureBox1.Image = Image.FromFile("c.gif"); pictureBox1.Location = new System.Drawing.Point(228, 180); pictureBox1.SetBounds(150, y + 130, 700, 700); pictureBox1.Name = "NoSol"; pictureBox1.Size = new System.Drawing.Size(300, 300); pictureBox1.TabIndex = 2; pictureBox1.TabStop = false; this.Controls.Add(pictureBox1); } }