#pragma once #include int n; int a1[20][20],a2[20][20],p[20][20],cn1[20],cn2[20]; int x[400],y[400]; int s; namespace 末項確定法 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Form1 の概要 /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクター コードを追加します // } protected: /// /// 使用中のリソースをすべてクリーンアップします。 /// ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::DataGridView^ dataGridView1; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column1; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column2; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column3; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column4; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column5; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column6; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column7; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column8; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column9; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column10; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column11; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column12; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column13; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column14; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column15; private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column16; private: System::Windows::Forms::Label^ label2; private: /// /// 必要なデザイナー変数です。 /// System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// デザイナー サポートに必要なメソッドです。このメソッドの内容を /// コード エディターで変更しないでください。 /// void InitializeComponent(void) { System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle1 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle3 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle4 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle5 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle6 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle7 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle8 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle9 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle10 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle11 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle12 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle13 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle14 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle15 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle16 = (gcnew System::Windows::Forms::DataGridViewCellStyle()); this->button1 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView()); this->Column1 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column2 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column3 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column4 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column5 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column6 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column7 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column8 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column9 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column10 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column11 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column12 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column13 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column14 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column15 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->Column16 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn()); this->label2 = (gcnew System::Windows::Forms::Label()); (cli::safe_cast(this->dataGridView1))->BeginInit(); this->SuspendLayout(); // // button1 // this->button1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 21.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->button1->Location = System::Drawing::Point(22, 17); this->button1->Margin = System::Windows::Forms::Padding(5, 4, 5, 4); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(94, 40); this->button1->TabIndex = 0; this->button1->Text = L"実行"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(137, 9); this->label1->Margin = System::Windows::Forms::Padding(5, 0, 5, 0); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(202, 32); this->label1->TabIndex = 1; this->label1->Text = L"TextBoxに3以上10以下の\r\n整数を入力して下さい。\r\n"; // // textBox1 // this->textBox1->Location = System::Drawing::Point(222, 44); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(100, 23); this->textBox1->TabIndex = 3; // // dataGridView1 // this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize; this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(16) {this->Column1, this->Column2, this->Column3, this->Column4, this->Column5, this->Column6, this->Column7, this->Column8, this->Column9, this->Column10, this->Column11, this->Column12, this->Column13, this->Column14, this->Column15, this->Column16}); this->dataGridView1->Location = System::Drawing::Point(22, 73); this->dataGridView1->Name = L"dataGridView1"; this->dataGridView1->RowTemplate->Height = 21; this->dataGridView1->Size = System::Drawing::Size(593, 948); this->dataGridView1->TabIndex = 4; // // Column1 // dataGridViewCellStyle1->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column1->DefaultCellStyle = dataGridViewCellStyle1; this->Column1->HeaderText = L"A"; this->Column1->Name = L"Column1"; this->Column1->Width = 30; // // Column2 // dataGridViewCellStyle2->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column2->DefaultCellStyle = dataGridViewCellStyle2; this->Column2->HeaderText = L"B"; this->Column2->Name = L"Column2"; this->Column2->Width = 30; // // Column3 // dataGridViewCellStyle3->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column3->DefaultCellStyle = dataGridViewCellStyle3; this->Column3->HeaderText = L"C"; this->Column3->Name = L"Column3"; this->Column3->Width = 30; // // Column4 // dataGridViewCellStyle4->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column4->DefaultCellStyle = dataGridViewCellStyle4; this->Column4->HeaderText = L"D"; this->Column4->Name = L"Column4"; this->Column4->Width = 30; // // Column5 // dataGridViewCellStyle5->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column5->DefaultCellStyle = dataGridViewCellStyle5; this->Column5->HeaderText = L"E"; this->Column5->Name = L"Column5"; this->Column5->Width = 30; // // Column6 // dataGridViewCellStyle6->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column6->DefaultCellStyle = dataGridViewCellStyle6; this->Column6->HeaderText = L"F"; this->Column6->Name = L"Column6"; this->Column6->Width = 30; // // Column7 // dataGridViewCellStyle7->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column7->DefaultCellStyle = dataGridViewCellStyle7; this->Column7->HeaderText = L"G"; this->Column7->Name = L"Column7"; this->Column7->Width = 30; // // Column8 // dataGridViewCellStyle8->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column8->DefaultCellStyle = dataGridViewCellStyle8; this->Column8->HeaderText = L"H"; this->Column8->Name = L"Column8"; this->Column8->Width = 30; // // Column9 // dataGridViewCellStyle9->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column9->DefaultCellStyle = dataGridViewCellStyle9; this->Column9->HeaderText = L"I"; this->Column9->Name = L"Column9"; this->Column9->Width = 30; // // Column10 // dataGridViewCellStyle10->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column10->DefaultCellStyle = dataGridViewCellStyle10; this->Column10->HeaderText = L"J"; this->Column10->Name = L"Column10"; this->Column10->Width = 30; // // Column11 // dataGridViewCellStyle11->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column11->DefaultCellStyle = dataGridViewCellStyle11; this->Column11->HeaderText = L"K"; this->Column11->Name = L"Column11"; this->Column11->Width = 30; // // Column12 // dataGridViewCellStyle12->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column12->DefaultCellStyle = dataGridViewCellStyle12; this->Column12->HeaderText = L"L"; this->Column12->Name = L"Column12"; this->Column12->Width = 30; // // Column13 // dataGridViewCellStyle13->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column13->DefaultCellStyle = dataGridViewCellStyle13; this->Column13->HeaderText = L"M"; this->Column13->Name = L"Column13"; this->Column13->Width = 30; // // Column14 // dataGridViewCellStyle14->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column14->DefaultCellStyle = dataGridViewCellStyle14; this->Column14->HeaderText = L"N"; this->Column14->Name = L"Column14"; this->Column14->Width = 30; // // Column15 // dataGridViewCellStyle15->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column15->DefaultCellStyle = dataGridViewCellStyle15; this->Column15->HeaderText = L"O"; this->Column15->Name = L"Column15"; this->Column15->Width = 30; // // Column16 // dataGridViewCellStyle16->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleCenter; this->Column16->DefaultCellStyle = dataGridViewCellStyle16; this->Column16->HeaderText = L"P"; this->Column16->Name = L"Column16"; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(347, 9); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(0, 16); this->label2->TabIndex = 5; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(10, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->AutoScroll = true; this->ClientSize = System::Drawing::Size(649, 1033); this->Controls->Add(this->label2); this->Controls->Add(this->dataGridView1); this->Controls->Add(this->textBox1); this->Controls->Add(this->label1); this->Controls->Add(this->button1); this->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->Margin = System::Windows::Forms::Padding(5, 4, 5, 4); this->Name = L"Form1"; this->Text = L"Form1"; (cli::safe_cast(this->dataGridView1))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { label2->Text=L""; DateTime^ hj=DateTime::Now; n=int::Parse(textBox1->Text); int h=1; if(n<3 || n>=12){ label2->Text=L"TextBoxには3以上11以下の整数\r\nを入力し再度実行ボタンを\r\n押して下さい。"; h=0; } if(h==1){ s=0; if(n%2==1)g1(n); if(n%2==0)g2(n); syokika(); if(n==3 || n==4)srand(0); if(n==5)srand(5); if(n==6)srand(28); if(n==7)srand(31); if(n==8)srand(597); if(n==9)srand(3154); if(n==10)srand(1114); if(n==11)srand(3375); f1(0); array^ w=gcnew array(16); int i; w[10]=n.ToString();w[11]=L"次";w[12]=L"魔";w[13]=L"方";w[14]=L"陣";w[15]=s.ToString(); for(i=0;i<9;i++)w[i]=L""; dataGridView1->Rows->Add(w); DateTime^ ow=DateTime::Now; TimeSpan sa=ow->Subtract(*hj); w[10]=L"時";w[11]=L"間";w[12]=L"計";w[13]=L"則";w[14]=L":";w[15]=(sa.TotalSeconds).ToString(); for(i=0;i<10;i++)w[i]=L""; dataGridView1->Rows->Add(w); } } void syokika(){ int i,j; for(i=0;i=n && i<2*n-1){ x[i]=2*n-i-1; y[i]=i-n; if(x[i]<=y[i]){ x[i]--; y[i]++; } } for(j=1;j=2*n-1+(2*n-j-2)*(j-1)) && (i<3*n-3+(2*n-j-3)*(j-1))){ y[i]=j-1; x[i]=i-(2*n-1+(2*n-j-2)*(j-1))+j; if(x[i]>=n-y[i]-1)x[i]++; } } for(j=1;j=3*n-3+(2*n-3-j)*(j-1)) && (i<2*n-1+(2*n-3-j)*j)){ x[i]=j-1; y[i]=i-(3*n-3+(2*n-3-j)*(j-1))+j; if(x[i]>=n-y[i]-1)y[i]++; } } for(j=1;j=3*n-3+m+(2*n-m-3)*(m-1)+(2*m+2-j)*(j-1)) && (i<3*n-3+2*m+(2*n-m-3)*(m-1)+(2*m+1-j)*(j-1))){ y[i]=j+m-1; x[i]=i-(3*n-3+m+(2*n-m-3)*(m-1)+(2*m+2-j)*(j-1))+j+m; } } for(j=1;j=3*n-3+2*m+(2*n-m-3)*(m-1)+(2*m+1-j)*(j-1)) && (i<3*n-3+m+(2*n-m-3)*(m-1)+(2*m+1-j)*j)){ x[i]=j+m-1; y[i]=i-(3*n-3+2*m+(2*n-m-3)*(m-1)+(2*m+1-j)*(j-1))+j+m; } } } } void g2(int n){ int i,j,m; m=n/2; for(i=0;i=n && i<2*n){ x[i]=2*n-i-1; y[i]=i-n; } for(j=1;j=2*n*j-j*j-j+2) && (i<2*n*j+n-j*j-2*j+1)){ y[i]=j-1; x[i]=i-(2*n*j-j*j-j+2)+j; if(x[i]>=n-y[i]-1)x[i]++; } } for(j=1;j=2*n*j+n-j*j-2*j+1) && (i<2*n*j+2*n-j*j-3*j)){ x[i]=j-1; y[i]=i-(2*n*j+n-j*j-2*j+1)+j; if(x[i]>=n-y[i]-1)y[i]++; } } for(j=1;j=3*m*m+m+(j-1)*(n-j)) && (i<3*m*m+j*(n-j))){ y[i]=j+m-1; x[i]=i-(3*m*m+m+(j-1)*(n-j))+j+m; } } for(j=2;j=3*m*m+(j-1)*(n-(j-1))) && (i<3*m*m+m+(j-1)*(n-j))){ x[i]=j+m-2; y[i]=i-(3*m*m+(j-1)*(n-(j-1)))+j+m-1; } } } } void f1(int g){ if(s==100)return; int i,j,h,wa,kk,kkk,m,sa; if(g==n-1){ wa=0; for(j=0;jn-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; f1(g+1); cn1[sa]--; return; } if(y[g]==n-1 && x[g]==0){ wa=0; for(j=0;jn-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; f1(g+1); cn1[sa]--; return; } if(y[g]==0 && x[g]==n-2){ wa=0; for(j=0;jn-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; f1(g+1); cn1[sa]--; return; } if(y[g]==n-2 && x[g]==0){ wa=0; for(j=0;jn-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; f1(g+1); cn1[sa]--; return; } if(y[g]>0 && y[g]n-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; f1(g+1); cn1[sa]--; return; } if(x[g]>0 && x[g]n-1)return; if(cn1[sa]>n-1)return; a1[y[g]][x[g]]=sa; cn1[sa]++; if(gn)h=0; if(h==1)f1(g+1); cn1[kkk]--; } } void f2(int g){ if(s==100)return; int i,j,k,h,wa,kk,kkk,m,yy,hh,sa; yy=a1[y[g]][x[g]]; array^ w=gcnew array(16); if(g==n-1){ wa=0; for(j=0;jn-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; f2(g+1); cn2[sa]--; p[yy][sa]=0; return; } if(y[g]==n-1 && x[g]==0){ wa=0; for(j=0;jn-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; f2(g+1); cn2[sa]--; p[yy][sa]=0; return; } if(y[g]==0 && x[g]==n-2){ wa=0; for(j=0;jn-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; f2(g+1); cn2[sa]--; p[yy][sa]=0; return; } if(y[g]==n-2 && x[g]==0){ wa=0; for(j=0;jn-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; f2(g+1); cn2[sa]--; p[yy][sa]=0; return; } if(y[g]>0 && y[g]n-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; f2(g+1); cn2[sa]--; p[yy][sa]=0; return; } if(x[g]>0 && x[g]n-1)return; if(p[yy][sa]==1)return; if(cn2[sa]>n-1)return; a2[y[g]][x[g]]=sa; p[yy][sa]=1; cn2[sa]++; if(gRows->Add(w); } for(j=0;j<16;j++)w[j]=L""; dataGridView1->Rows->Add(w); s++; if(s==100)return; } cn2[sa]--; p[yy][sa]=0; return; } kk=rand()%n; m=n/2; for(i=0;in)h=0; hh=0; if(h==1){ if(p[yy][kkk]==0){ p[yy][kkk]=1; hh=1; } else{ h=0; } } if(h==1)f2(g+1); if(hh==1)p[yy][kkk]=0; cn2[kkk]--; } } }; }