Aklımda Kalası Kelimeler

* давайте работать вместе
* Zarf ve Mazruf, Zerafet(xHoyratlık) ile aynı kökten(za-ra-fe) gelir
* Bedesten
* Suç subuta ermiştir - Suç sabit olmuştur

26 Kasım 2009 Perşembe

Bu da benim 8 vezir problem çözümüm


Kaynak Kodu

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace waQueen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private delegate void DelAddQueen(int x, int y);

private DelAddQueen delAddQueen;

private Board board;
private void btnBasla_Click(object sender, EventArgs e)
{
try
{
board = new Board();

#region 1. QUEEN
Queen queen = new Queen(1, 1);

board.f_AddToBoard(queen);
#endregion

btnBasla.Enabled = false;
btnBitir.Enabled = true;

delAddQueen = new DelAddQueen(board.f_AddQueen);
delAddQueen.BeginInvoke(1, 2, null, null);
}
catch (Exception ex)
{
txtMemo.Text = "HATA:" + Environment.NewLine + ex.Message;
}
//board.f_AddQueen(1, 2);
}

private void btnBitir_Click(object sender, EventArgs e)
{
board.M_bBitir = true;
btnBitir.Enabled = false;
btnBasla.Enabled = true;
}

}



class Queen
{
public int X;
public int Y;
public List TehditHucreleri = new List();

public Queen(int _x, int _y)
{
X = _x;
Y = _y;
f_TehditHucreleriniOlustur();
}

public void f_TehditHucreleriniOlustur()
{
// Satırda tehditler...
for (int y = 1; y <= 8; y++)
{
if (!TehditHucreleri.Contains(new Point(this.X, y)))
TehditHucreleri.Add(new Point(this.X, y));
}

// Sutunda tehditler...
for (int x = 1; x <= 8; x++)
{
if (!TehditHucreleri.Contains(new Point(x, this.Y)))
TehditHucreleri.Add(new Point(x, this.Y));
}

// capraz tehditler
for (int x = this.X, y = this.Y; (this.Y > 1 || this.X > 1) && (x > 0 && y > 0); x--, y--)
{
if (!TehditHucreleri.Contains(new Point(x, y)))
TehditHucreleri.Add(new Point(x, y));
}
for (int x = this.X, y = this.Y; x <= 8 && y <= 8; x++, y++)
{
if (!TehditHucreleri.Contains(new Point(x, y)))
TehditHucreleri.Add(new Point(x, y));
}
for (int x = this.X, y = this.Y; (this.Y > 1 || this.X > 1) && (x < 9 && y > 0); x++, y--)
{
if (!TehditHucreleri.Contains(new Point(x, y)))
TehditHucreleri.Add(new Point(x, y));
}
for (int x = this.X, y = this.Y; (this.Y > 1 || this.X > 1) && (x > 0 && y < 9); x--, y++)
{
if (!TehditHucreleri.Contains(new Point(x, y)))
TehditHucreleri.Add(new Point(x, y));
}
}

}

class Board
{
private int m_iHiz = 0;
public bool M_bBitir;

public Board()
{
Form frm = Form1.ActiveForm as Form1;
try
{
m_iHiz = Convert.ToInt32(((TextBox)frm.Controls.Find("txtHiz", true)[0]).Text);
Thread.Sleep(m_iHiz);
}
catch (Exception ex)
{
((TextBox)frm.Controls.Find("txtMemo", true)[0]).Text += "HATA: " + Environment.NewLine + ex.Message;
}
}
string SBoard = "";
void fYazdir()
{
SBoard = "Bulunan çözüm sayısı: " + iBulunanCozumSayisi + Environment.NewLine;
for (int i = 1; i < 9; i++)
{
for (int j = 1; j < 9; j++)
{
Point p = new Point(i, j);
if (StckQueen.ToArray().Where(k => k.X.Equals(i) && k.Y.Equals(j)).FirstOrDefault() != null)
{
SBoard += " Q ";
continue;
}
if (StckQueen.ToArray().Where(k => k.TehditHucreleri.Contains(p)).FirstOrDefault() != null)
{
SBoard += " 1 ";
continue;
}
SBoard += " 0 ";
}
SBoard += Environment.NewLine;
}
SBoard += Environment.NewLine;
}

public Stack StckQueen = new Stack();

public bool f_Guvenlimi(int _yeniX, int _yeniY)
{
foreach (Queen queen in StckQueen)
{
if (queen.TehditHucreleri.Contains(new Point(_yeniX, _yeniY)))
return false;
}
return true;
}


public void f_RemoveQueen()
{
StckQueen.Pop();
}

public void f_AddQueen(int _i, int _j)
{
// Eğer bitirmemiz istenirse ilerleme...
if (M_bBitir)
{
return;
}

for (int x = _i; x < 9; x++)
{
if (f_Guvenlimi(x, _j))
{
f_AddToBoard(new Queen(x, _j));
//fYazdir();
f_QueenleriGoster();
if (StckQueen.Count == 8)
{
return;
}
else
{
Queen sonQueen = StckQueen.Peek();
f_AddQueen(1, sonQueen.Y + 1);
}
}
else
{
if (x == 8)
{
Queen sonQueen = StckQueen.Peek();
if (sonQueen.X == 8)
{
StckQueen.Pop();
sonQueen = StckQueen.Peek();
}
int i = sonQueen.X + 1;
int j = sonQueen.Y;
StckQueen.Pop();
//fYazdir();
f_QueenleriGoster();

f_AddQueen(i, j);
}
}
}
}

public void f_AddToBoard(Queen _queen)
{
StckQueen.Push(_queen);
}

private int iBulunanCozumSayisi = 0;
public void f_QueenleriGoster()
{
Form frm = Form1.ActiveForm as Form1;
Panel pnl = ((Panel)frm.Controls.Find("panel1", true)[0]);
//frm.Controls.Find()
int iSira = 0;

if (frm.InvokeRequired)
{
frm.Invoke(new MethodInvoker(delegate
{
((PictureBox)pnl.Controls.Find("q1", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q2", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q3", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q4", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q5", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q6", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q7", true)[0]).Location = new Point(-40, -40);
((PictureBox)pnl.Controls.Find("q8", true)[0]).Location = new Point(-40, -40);

}
));
}

foreach (Queen queen in StckQueen)
{
iSira++;

if (frm.InvokeRequired)
{
frm.Invoke(new MethodInvoker(delegate
{
PictureBox q = (PictureBox)pnl.Controls.Find("q" + iSira, true)[0];
q.Location = new Point(((queen.X - 1) * 40) + 16, ((queen.Y - 1) * 40) + 16);
}));
}
}
if (StckQueen.Count == 8)
{
Thread.Sleep(2000);
if (frm.InvokeRequired)
{
frm.Invoke(new MethodInvoker(delegate
{
if (StckQueen.Count == 8)
{
iBulunanCozumSayisi++;
fYazdir();
((TextBox)frm.Controls.Find("txtMemo", true)[0]).Text += SBoard;
}
}));
}
}
else
{
Thread.Sleep(m_iHiz);
}
}
}
}

Hiç yorum yok: