Najkrótszy kod dla problemu osmiu hetmanów

0

Najkrótszy kod jaki udało mi się znaleźć jest w języku Icon:

procedure main()
	write(q(1), q(2), q(3), q(4), q(5), q(6), q(7), q(8))
end
procedure q(c)
	suspend place(1 to 8, c) # look for a row
end
procedure place(r, c)
	static up, down, row
	initial 
	{
		up := list(15, 0)
	    down := list(15, 0)
		row := list(8, 0)
	}
	if row[r] = down[r + c –1] = up[8 + r – c] = 0 # place if free
	then suspend row[r] <– down[r + c – 1] <– up[8 + r – c] <– r
end

 

Czy zna ktoś jakiś język w którym dało by się to krócej napisać?

0

Znalazłem też kod w haskellu:

 
  import Control.Monad
  queens boardSize = foldM (\y _ -> [ x : y | x <- [1..boardSize], safe x y 1]) [] [1..boardSize]
  safe x [] n = True
  safe x (c:y) n = and [ x /= c , x /= c + n , x /= c - n , safe x y (n+1)]
0

JayLanguage:

queens =: (](1&=@(>./@(+/,+//.,+//.@|.))"2#])@,/@:((],"_ _ 1=@i.@#@{.)"2)^:[(1 0&,$0:))
 
0

jakoś nie wieżę że w Pascalu da się to zapisać w jednej linijce...proszę o kod

0

"Daj mi 7 minut... - Patryk27 7 minut temu" time is over! :D

0

Dobra ale jak nie będzie to przelejesz na moje konto 100zł - zgoda ?

0

To w Pascalu znak znak nowej linii cokolwiek znaczy? (oczywiście poza stringami)

0

mam nadzieję że to będzie w Pascalu stworzonym przez Wirtha

0

już 10minut mineło.

0

Proszę:

Uses Crt;Type ChessBoard=Array[1..8,1..8]Of Integer;Var Board:ChessBoard;N:Integer;Procedure Initialize(Col:Integer);Var I,J:Integer;Begin For I:=1 To N Do For J:=Col To N Do Board[I, J]:=0;End;Procedure Display(Var Board:Chessboard;Size: Integer);Var I,J:Integer;Begin ClrScr;For I:=1 To N Do Begin For J:=1 To N do if Board[I,J]=1 Then Write('Q')Else Write(#254);WriteLn;End;End;Function Can_Place(Row,Col:Integer):Boolean;Var I,J:Integer;NoConflict:Boolean;Begin NoConflict:=True;
For J:= 1 To Col-1 Do if Board[Row,J]=1 Then NoConflict:=False;I:=Row;J:=Col;While(I>0)And(J>0)And NoConflict Do Begin Dec(I);Dec(J);if(Board[I,J]=1)Then NoConflict:=False;End;I:=Row;J:=Col;While(I<=N)And(J>0)And NoConflict Do Begin Inc(I);Dec(J);if(Board[I,J]=1)Then NoConflict:=False;End;Can_Place:=NoConflict;End;Procedure Place(var Board:ChessBoard;Row,Col:Integer);Begin if(Col>N)Then Display(Board,N)Else Repeat Begin Initialize(Col);if Can_Place(Row,Col)Then Begin Board[Row,Col]:=1;Place(Board,1,Col+1)End;Inc(Row)End;Until(Row>N);End;Begin N:=8;Place(Board,1,1);ReadLn;End.

Na ogromnej szerokości będzie to jedna linijka.
I działa!
W Delphi należy jeszcze dodać {$APPTYPE CONSOLE}

0

Patryk27 ile ja mam czekać na tą linijkę kodu? Mówisz 4min a już 20mineło i dalej nic. Przyznaj się że nie potrafisz tego napisać.

0

ale to nie o to w tym chodzi Patryk27 - to co pokazałeś to normalny kod. W taki sposób to można by napisać każdy program w jednej linijce.
Mi chodziło o zwięzłość kodu. To co podałeś nie jest zwięzłe.

0

ale mi chodziło w tym topicu o ilość instrukcji kodu...
Oj Patryk Patryk...

0

Co się czepiacie? Jedna linijka miała być i jest, więc Patryk27 spełnił żądanie. Sami sobie plujcie w brodę, że macie takie niedoprecyzowane żądania.

0

Czepiacie się ?

Przecież to co Patryk27 napisał to ma może ze 20 linijek. Mi chodziło o ilość instrukcji a nie formatowanie kodu.

0

Chodziło ci wyraźnie o linijki, a nie ilość instrukcji, tokenów, reguł składniowych itp

I chyba nie w każdym języku da się napisać bez enterów. Mam na myśli Pythona.

0

nie o to mi chodziło - wy mnie nie rozumiecie? Ale to nie moja wina że tacy jesteście.

2

Wiem o co ci chodziło. Wiem też że źle się wyraziłeś, a Patryk27 spełnił co do joty żądanie. Gdybyś wszedł do baru i zamówił coś do picia, to pewnie byś się oburzył, że barman podaje ci mineralną.

0

ej ale po co Ci to w ogóle?
myślisz że ilość linii kodu jest odwrotnie proprocjonalna do wielkości kutacza?

napisz sobie swój język programowania który będzie miał jedną instrukcję "a" która będzie rozwiązywać ten jeden problem, potem piszesz kod:

a

i voila... tylko co w tym fajnego? :/

0

skoro już jesteśmy przy dziwnych linijkach:

#include                                     <math.h>
#include                                   <sys/time.h>
#include                                   <X11/Xlib.h>
#include                                  <X11/keysym.h>
                                          double L ,o ,P
                                         ,_=dt,T,Z,D=1,d,
                                         s[999],E,h= 8,I,
                                         J,K,w[999],M,m,O
                                        ,n[999],j=33e-3,i=
                                        1E3,r,t, u,v ,W,S=
                                        74.5,l=221,X=7.26,
                                        a,B,A=32.2,c, F,H;
                                        int N,q, C, y,p,U;
                                       Window z; char f[52]
                                    ; GC k; main(){ Display*e=
 XOpenDisplay( 0); z=RootWindow(e,0); for (XSetForeground(e,k=XCreateGC (e,z,0,0),BlackPixel(e,0))
; scanf("%lf%lf%lf",y +n,w+y, y+s)+1; y ++); XSelectInput(e,z= XCreateSimpleWindow(e,z,0,0,400,400,
0,0,WhitePixel(e,0) ),KeyPressMask); for(XMapWindow(e,z); ; T=sin(O)){ struct timeval G={ 0,dt*1e6}
; K= cos(j); N=1e4; M+= H*_; Z=D*K; F+=_*P; r=E*K; W=cos( O); m=K*W; H=K*T; O+=D*_*F/ K+d/K*E*_; B=
sin(j); a=B*T*D-E*W; XClearWindow(e,z); t=T*E+ D*B*W; j+=d*_*D-_*F*E; P=W*E*B-T*D; for (o+=(I=D*W+E
*T*B,E*d/K *B+v+B/K*F*D)*_; p<y; ){ T=p[s]+i; E=c-p[w]; D=n[p]-L; K=D*m-B*T-H*E; if(p [n]+w[ p]+p[s
]== 0|K <fabs(W=T*r-I*E +D*P) |fabs(D=t *D+Z *T-a *E)> K)N=1e4; else{ q=W/K *4E2+2e2; C= 2E2+4e2/ K
 *D; N-1E4&& XDrawLine(e ,z,k,N ,U,q,C); N=q; U=C; } ++p; } L+=_* (X*t +P*M+m*l); T=X*X+ l*l+M *M;
  XDrawString(e,z,k ,20,380,f,17); D=v/l*15; i+=(B *l-M*r -X*Z)*_; for(; XPending(e); u *=CS!=N){
                                   XEvent z; XNextEvent(e ,&z);
                                       ++*((N=XLookupKeysym
                                         (&z.xkey,0))-IT?
                                         N-LT? UP-N?& E:&
                                         J:& u: &h); --*(
                                         DN -N? N-DT ?N==
                                         RT?&u: & W:&h:&J
                                          ); } m=15*F/l;
                                          c+=(I=M/ l,l*H
                                          +I*M+a*X)*_; H
                                          =A*r+v*X-F*l+(
                                          E=.1+X*4.9/l,t
                                          =T*m/32-I*T/24
                                           )/S; K=F*M+(
                                           h* 1e4/l-(T+
                                           E*5*T*E)/3e2
                                           )/S-X*d-B*A;
                                           a=2.63 /l*d;
                                           X+=( d*l-T/S
                                            *(.19*E +a
                                            *.64+J/1e3
                                            )-M* v +A*
                                            Z)*_; l +=
                                            K *_; W=d;
                                            sprintf(f,
                                            "%5d  %3d"
                                            "%7d",p =l
                                           /1.7,(C=9E3+
                              O*57.3)%0550,(int)i); d+=T*(.45-14/l*
                             X-a*130-J* .14)*_/125e2+F*_*v; P=(T*(47
                             *I-m* 52+E*94 *D-t*.38+u*.21*E) /1e2+W*
                             179*v)/2312; select(p=0,0,0,0,&G); v-=(
                              W*F-T*(.63*m-I*.086+m*E*19-D*25-.11*u
                               )/107e2)*_; D=cos(o); E=sin(o); } } 
#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
        _-_-_-_-_-_-_-_
            _-_-_-_
} 

http://pl.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest

0

a co to by był za język jakby rozwiązywał tylko dane problemy?
np. w haskellu kod quicksorta wygląda tak:

quicksort [] = []
quicksort (s:xs) = quicksort [x|x <- xs,x < s] ++ [s] ++ quicksort [x|x <- xs,x >= s]
0

Kod insertsorta w haskellu:

 
sort []  = []
sort [x] = [x]
sort (x:xs) = insert (sort xs)
    where insert [] = [x]
          insert (y:ys) | x <= y    = x : y : ys
                        | otherwise = y : insert ys
0

Osiem hetmanów w prologu:

 
solution([]).
 
solution([X/Y|Others]) :-
 solution(Others),
 member(Y, [1,2,3,4,5,6,7,8]),
 noattack(X/Y, Others).
 
noattack(_,[]).
 
noattack(X/Y,[X1/Y1|Others]) :-
 Y =\= Y1,
 Y1 - Y =\= X1 - X,
 Y1 - Y =\= X - X1,
 noattack(X/Y,Others).
 
member(Item,[Item|Rest]).
 
member(Item,[First|Rest]) :-
 member(Item,Rest).
 
template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8])
0

Haskell jest nieobiektowy (pośrednio w związku z tym nie ma np refleksji). Dlatego wolę Scalę. QuickSort w Scali: http://rosettacode.org/wiki/Quicksort#Scala

0

@offtop
Skoro jesteście tacy "mądrzy" to powiedzcie mi w jakim języku napisany był wirus użyty do zaatakowania irańskiego programu nuklearnego ?
Serio się pytam.
Jak myślicie.
Wirus - Stuxnet
http://www.epolonia.us/fakty/usa/komputerowy-wirus-mial-zniszczyc-iranski-program-nuklearny

0

"Stuxnet is unusually large at half a megabyte in size,[31] and written in several different programming languages (including C and C++) which is also irregular for malware"

0

Ze strony:
http://www.multiupload.com/BDNYSCY5PC

można sobie pobrać zdekompilowany kod wirusa Stuxnet.

0

to jest kod zdekompilowany - to nie jest szkodliwe.

0

Sorry nie mogłem się powstrzymać - taki mały example: // więcej nie chciało się zmieścić - wywalało error O_o - myślałem że ten wirus ożył... :D

//----- (10001000) --------------------------------------------------------
void *__thiscall sub_10001000(void *this)
{
  void *v2; // esi@1

  v2 = this;
  *(_DWORD *)this = off_1001C13C;
  sub_10001202((int)((char *)this + 4));
  *(_DWORD *)v2 = &off_1001C130;
  *((_DWORD *)v2 + 5) = sub_1000D6CB();
  return v2;
}
// 1001C130: using guessed type int (__stdcall *off_1001C130)(void *Src);
// 1001C13C: using guessed type int (*off_1001C13C[2])();

//----- (1000106C) --------------------------------------------------------
unsigned int __thiscall sub_1000106C(int this, char a2)
{
  int v2; // eax@1
  unsigned int v3; // esi@1

  v3 = this;
  v2 = sub_10001088(this);
  if ( a2 & 1 )
    sub_10011C3F(v2, v3);
  return v3;
}

//----- (10001088) --------------------------------------------------------
int __thiscall sub_10001088(int this)
{
  int result; // eax@1
  int v2; // esi@1
  int v3; // eax@1

  v2 = this;
  *(_DWORD *)this = &off_1001C130;
  sub_1000D701(*(_DWORD *)(this + 20));
  v3 = sub_10001316(v2 + 4);
  result = sub_1000129C(v3, v2 + 4, *(_DWORD *)(v2 + 8));
  *(_DWORD *)v2 = off_1001C13C;
  return result;
}
// 1001C130: using guessed type int (__stdcall *off_1001C130)(void *Src);
// 1001C13C: using guessed type int (*off_1001C13C[2])();

//----- (100010E8) --------------------------------------------------------
int __thiscall sub_100010E8(int this, const void *Src)
{
  int v2; // edi@1
  int v3; // esi@1
  int v4; // eax@1
  int v5; // ecx@1
  int v6; // eax@3
  char Dst; // [sp+8h] [bp-18h]@2
  int v9; // [sp+10h] [bp-10h]@1
  int v10; // [sp+1Ch] [bp-4h]@1

  v2 = this;
  v3 = *(_DWORD *)(this + 20);
  v4 = *(_DWORD *)v3;
  v5 = *(_DWORD *)(this + 20);
  v9 = v3;
  (*(void (__thiscall **)(int))v4)(v5);
  v10 = 0;
  if ( !sub_1000119C(v2, Src) )
  {
    memcpy(&Dst, Src, 6u);
    sub_10001277(v2 + 4, (int)&Dst);
  }
  v6 = *(_DWORD *)v3;
  v10 = -1;
  return (*(int (__thiscall **)(int))(v6 + 4))(v3);
}

//----- (10001156) --------------------------------------------------------
int __userpurge sub_10001156<eax>(int a1<ecx>, int a2<edi>, int a3)
{
  int v4; // ST00_4@1
  int v5; // edi@1
  int v6; // ST08_4@1

  v4 = a2;
  v5 = a1;
  v6 = *(_DWORD *)(a1 + 20);
  ((void (__thiscall *)(_DWORD, _DWORD))v6)(*(_DWORD *)(a1 + 20), v4);
  sub_10001367(a3, v5 + 4);
  return (*(int (__thiscall **)(int))(*(_DWORD *)v6 + 4))(v6);
}
// 10001156: could not find valid save-restore pair for edi
// 10001156: could not find valid save-restore pair for esi

//----- (1000119C) --------------------------------------------------------
char __thiscall sub_1000119C(int this, const void *Buf2)
{
  int v2; // esi@1
  int v3; // eax@1
  void *v4; // eax@5
  char v6; // [sp+4h] [bp-Ch]@1
  int v7; // [sp+8h] [bp-8h]@1
  void *Buf1; // [sp+Ch] [bp-4h]@1

  v2 = this;
  v3 = (_DWORD )(this + 8);
  v6 = 0;
  Buf1 = 0;
  v7 = v3;
  while ( 1 )
  {
    if ( Buf1 )
    {
LABEL_5:
      v4 = Buf1;
      goto LABEL_7;
    }
    if ( v7 == *(_DWORD *)(v2 + 8) )
      return 0;
    if ( Buf1 )
      goto LABEL_5;
    v4 = (void *)(v7 + 8);
LABEL_7:
    if ( !memcmp(v4, Buf2, 6u) )
      return 1;
    sub_1000153A((int)&v6);
  }
}

//----- (10001202) --------------------------------------------------------
int __thiscall sub_10001202(int this)
{
  int v2; // esi@1

  v2 = this;
  *(_DWORD *)this = 0;
  *(_DWORD *)(this + 4) = 0;
  *(_DWORD *)(this + 8) = 0;
  *(_DWORD *)(this + 12) = 0;
  sub_100012BB(this);
  return v2;
}

//----- (10001277) --------------------------------------------------------
int __thiscall sub_10001277(int this, int a2)
{
  int v3; // esi@1
  char v4; // [sp-10h] [bp-20h]@1
  int v5; // [sp-Ch] [bp-1Ch]@1
  int v6; // [sp-8h] [bp-18h]@1
  int v7; // [sp-4h] [bp-14h]@1
  char v8; // [sp+4h] [bp-Ch]@1

  v7 = a2;
  v3 = this;
  sub_10001B13(this, (int)&v4);
  return sub_100012D2(v3, (int)&v8, *(int *)&v4, v5, v6, v7);
}

//----- (1000129C) --------------------------------------------------------
int __userpurge sub_1000129C<eax>(int a1<eax>, int a2<ecx>, unsigned int a3)
{
  int v3; // ecx@1
  int result; // eax@2

  v3 = *(_DWORD *)(a2 + 12);
  if ( v3 )
    result = (*(int (__stdcall **)(unsigned int))(*(_DWORD *)v3 + 16))(a3);
  else
    result = sub_10011C3F(a1, a3);
  return result;
}

//----- (100012BB) --------------------------------------------------------
int __thiscall sub_100012BB(int this)
{
  int result; // eax@1
  int v2; // esi@1
  int v3; // eax@1
  int v4; // esi@1

  v2 = this;
  v3 = sub_1000134A(this);
  *(_DWORD *)(v2 + 4) = v3;
  *(_DWORD *)v3 = v3;
  v4 = *(_DWORD *)(v2 + 4);
  result = *(_DWORD *)v4;
  *(_DWORD *)(v4 + 4) = *(_DWORD *)v4;
  return result;
}

//----- (100012D2) --------------------------------------------------------
int __thiscall sub_100012D2(int this, int a2, int a3, int a4, int a5, int a6)
{
  int v6; // eax@1
  int v7; // ebx@1

  v7 = this;
  v6 = sub_1000134A(this);
  if ( v6 != -8 )
  {
    *(_DWORD *)(v6 + 8) = *(_DWORD *)a6;
    *(_WORD *)(v6 + 12) = *(_WORD *)(a6 + 4);
  }
  *(_DWORD *)v6 = a4;
  *(_DWORD *)(v6 + 4) = *(_DWORD *)(a4 + 4);
  (_DWORD )(a4 + 4) = v6;
  *(_DWORD *)(a4 + 4) = v6;
  ++*(_DWORD *)v7;
  *(_BYTE *)a2 = 0;
  *(_DWORD *)(a2 + 8) = 0;
  *(_DWORD *)(a2 + 4) = v6;
  return a2;
}

//----- (10001316) --------------------------------------------------------
int __thiscall sub_10001316(int this)
{
  int v2; // esi@1
  int v3; // ecx@1
  char v4; // [sp-18h] [bp-28h]@1
  unsigned int v5; // [sp-14h] [bp-24h]@1
  int v6; // [sp-10h] [bp-20h]@1
  char v7; // [sp-Ch] [bp-1Ch]@1
  int v8; // [sp-8h] [bp-18h]@1
  int v9; // [sp-4h] [bp-14h]@1
  char v10; // [sp+4h] [bp-Ch]@1

  v2 = this;
  v3 = *(_DWORD *)(this + 4);
  v7 = 0;
  v9 = 0;
  v8 = v3;
  sub_1000E80E(v2, (int)&v4);
  return sub_10001489((void *)v2, (int)&v10, v4, v5, v6, *(int *)&v7, v8, v9);
} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1