الگوریتم هشت وزیر به زبان پاسکال

the_king

مدیرکل انجمن
کد زیر مساله هشت وزیر را حل می کند :

کد:
program prg;
uses crt;
var
	i,j,flag,m,n,count:integer;
	v:array[1..8] of integer;
	c:char;
begin
	for i:=1 to 8 do
		v[i]:=1;
	clrscr;
	repeat
		repeat
			flag:=0;
			for i:=8 downto 1 do
			begin
				v[i]:=v[i]+1;
				if v[i]<9 then break;
				v[i]:=1;
				if i=1 then
				begin
					write('press any key to Exit  Count = ',count);
					halt;
				end;
			end;
			for i:=8 downto 2 do
			begin
				for j:=i-1 downto 1 do
				begin
					if v[i]=v[j] then
					begin
						flag:=1;
						break;
					end;
					if (v[i]-v[j]=i-j) or (v[j]-v[i]=i-j) then
					begin
						flag:=1;
						break;
					end;
				end;
				if flag=1 then break;
			end;
		until (flag=0);
		gotoxy(22,20);
		textcolor(15);
		textbackground(2);
		write(' Enter : continue   ESC : exit' );
		count:=count + 1;
		c:=readkey;
		if c=chr(27) then halt;
		textbackground(1);
		clrscr;
		textbackground(4);
		for i:=1 to 8 do
		begin
			for j:=1 to 8 do
			begin
				gotoxy(28+j*2,5+i);
				textcolor(12);
				if v[i]=j then
				begin
					textcolor(14);
					write (chr(219),chr(219));
				end
				else if (i+j) mod 2=0 then write (chr(176),chr(176))
				else write (chr(177),chr(177));
			end;
			writeln;
		end;
	until (false);
end.
 

جدیدترین ارسال ها

بالا