如何用 C 在 10 行內寫出八皇后?

時間 2021-05-08 08:23:44

1樓:loach gray

python,兩行,其中一行還是匯入包。

from itertools import permutations

print([s for s in permutations(range(int('皇后數量:4'[-1]))) if all(map(lambda x: all((len(s[:

s.index(x)])-j != abs(x-i)) for j,i in enumerate(s[:

s.index(x)])), s)) ])

2樓:牛肉豆脯

打了一下暴力,20行。

#include

using namespace std;

typedef long long ll;

int vis[30],vis1[30],vis2[30],n;

#define so(i,nn) vis[i]^=1,vis1[i+nn]^=1,vis2[i-nn+n]^=1

int dfs(int nn,ll ans=0)for(int i=0;i>n) cout<

3樓:Qwerty

#include

intcounter=0

;void

foo(

inty

);for

(intx=

1;x!=

9;++x

)}}int

main

()20行的來湊熱鬧,好像跟前面幾位用位運算的大佬解法不一樣但我覺得我這個看起來簡單啊

Rank-Z/solve-EightQueens-in-20-lines-

4樓:

void queen8(A[0..n-1],row)

今天我用ReactJS 寫了,才開始學,寫的不太好,見笑:

class ChessDisplay extends React.Component

rowslt;/div>function Square(props)

let results=

function queen(arr,row)

{ if(row==arr.lengthconsole.log(row)

const result =arr.map((item)=>return itemresults.push(result);

returnfor(let col=0;col,document.getElementById("content"));

最後的結果:

5樓:

首先新建乙個檔案寫正經的八皇后

不要用main

儲存為.h

然後include它

在main裡呼叫它

include

main

嗯六行很棒棒

6樓:趙子天

#include 一行

using namespace std; 兩行

int main 三行

7樓:Cytosine

乙個優秀的object-oriented programming的class應該自帶所有method的

那麼解決問題只呼叫就好

( @vczh 式逃

import

Queens

;public

class

QueensSolver

}看,才6行:)

什麼?你說不存在這個method?

乙個優秀的oop class都寫不好的公司有什麼可去的=。=(逃x2

8樓:有限狀態機

int n=0;

void NQ(int l, int d, int r)

for (int v = ~(l|d|r); int c = v&-v; v-=c)

if (~(d|c) == 0)

n++;

else

NQ((l|c)<<1, d|c, (r|c)>>1);

int main()

NQ(0,-1<<8,0);

printf("%d", n);

這是我根據Matrix大神的程式,改寫成C語言的版本,N皇后(N<=32)問題的通解.

如果只算有內容的行,正好10行.

解釋一下:

所有的變數(n除外)都表示棋盤上的一行.變數中的一位表示棋盤上的一格.

l是上面幾行的皇后向左下發出的射線佔據的位置.

d是上面幾行的皇后向下發出的射線佔據的位置.

r是上面幾行的皇后向右下發出的射線佔據的位置.

l|d|r是被上面的皇后威脅的位置.

v = ~(l|d|r)是這行可放皇后的位置

c = v&-v從可以放皇后的位置中取出最右邊一位

v-=c去除遍歷過的位

~(d|c) == 0表示每一列都有皇后了,那就是找到乙個解.

NQ((l|c)<<1, d|c, (r|c)>>1);表示把向左下,下,右下的射線延伸一格,加上本行的皇后發出的射線.

NQ(0,-1<<8,0);表示一開始就標記了只有右邊8列是可用的.要求解N皇后問題,只需把8換成N.

9樓:空氣幣和韭菜鏈

== 1 :- I = 1..8.

== 1 :- J = 1..8.

:- >= 2, D = 2..2*8.

:- >= 2, D = 1-8..8-1.

answer set programming 寫4行,36個字元不知道誰能送我份實習啊:)

10樓:NekoFlan

#include using namespace std;

int A[16]=;

int main(int argc, char** argv)

11樓:

#include

void

dous

(intr,

intl

,ints,

intret

,int&qb

)}else

++qb

;}int

main

()我都不知道我為啥那麼無聊

#include

template

,intR,

intL

,int

D>struct

help_queen

;template

,intW,

intR

,intL,

intD

>struct

xunhuan

;template

,intR,

intL

,int

D>struct

xunhuan

<0,W,R,

L,D>

;template

,intt2,

intPOS

,intW,

intR

,intL,

intD

>struct

switch_help_queen

;template

,int

ttt,

intW

,intR,

intL

,int

D>struct

switch_help_queen

<1,ttt,

POS,W,

R,L,

D>;template

,intW,

intR

,intL,

intD

>struct

switch_help_queen

<0,0,POS,W,

R,L,

D>;template

,intR,

intL

,int

D>struct

help_queen

;template

>struct

queen

;template

<>struct

queen

<2>;template

<>struct

queen

<1>;template

<>struct

queen

<0>;int

main

()還是元程式設計好玩

12樓:EAng

沒什麼奇技淫巧,只是把中間迴圈體裡的東西移到末尾迴圈體裡去了#include

void

foo(

const

unsigned

intscale

,unsigned

int*

answer

,unsigned

int&

solutions_count

,unsigned

intsums=0u

,unsigned

intdifferences=0u

,unsigned

intcolumns=0u

,unsigned

introw=0u

)int

main

(int

argc

,const

char

*argv)

13樓:劉正宇

#include

intbrd

,cnt=0

,row

[1000

],ld

[1000

],cd

[1000

],chess

[1000

];void

prt(

void

)putchar(10

);}}void

dfs(

intr

)for

(intc=

0;c<

brd;++c

)}}int

main

()return0;

}壓縮到乙個函式一行就是

#include

intbrd

,cnt=0

,row

[1000

],ld

[1000

],cd

[1000

],chess

[1000

];void

prt(

void

)putchar(10

);}}

void

dfs(

intr

)for

(intc=

0;c<

brd;++c

)else

}}int

main

()return0;}

我大概寫了25分鐘我覺得這個。。。 不難吧,一道很簡單的深搜題。程式競賽初賽題比水題稍微難的難度大概。

其實你就正常寫完然後強行縮行,把回車都刪掉,縮成乙個函式一行就可以了。雖然我寫的時候是直接在一行裡寫的。

所以我覺得這題。。比問給定乙個陣列,除乙個數字只出現過1次外所有數都出現過兩次,用O(1)的時間複雜度和空間複雜度找出只出現過1次的這個數字這種問題好多了吧,說極品就有點太。 不過面試過程中緊張的話會做的題做不出來也很正常。

14樓:

size_t

queen

(intn,

inty

,size_t

mask_v

,size_t

mask_l

,size_t

mask_r

)returnr;

}還算容易讀懂的版本……

15樓:

像我一樣搞過OI的人都應該知道八皇后的崇高地位,沒寫過都不好意思說會dfs。還有,用next_permutation列舉檢驗的全部打死,效率太低。

自己寫了乙個,十八行略微多了點,但是效率和可讀性都還行。

#include

using

namespace

std;

intans

;//answer counter

void

queen

(intk,

intc

,intd)

//reach the last line, a new answer found

for(

inti=1

;i&0xff;i

<<=1)

}int

main()

16樓:

lisp的話10行足夠了

(defun

queens(n

&optional(m

n))(if

(zeropn)

(list

nil)

(loop

forsolutionin(

queens(1-

n)m)

nconc

(loop

fornew-col

from1to

mwhen

(loop

forrow

from1to

nfor

colin

solution

always(/=

new-col

col(

+col

row)(-

colrow

)))collect

(cons

new-col

solution

)))))

如何用C 告白?

負一的平方根 class MyHeart switch You request try catch Failuree while You Love Me true true 養不熟的狼 第一次寫,你猜我輸出的是什麼呢 include include include using namespace s...

如何用C語言程式設計這個題?

tearing include intmain 霍工 include int main int salary,i,n 0,bill 5 clrscr printf Input salary scanf d salary for i 0 i 5 i n salary bill i salary sal...

如何用C 部署深度學習模型?

JuLec 在伺服器上部署,可以採用TensorRT加速 在CPU或FPGA上部署可以採用OpenVINO加速 或者也可以採用框架對應的C 庫,比如pytorch提供的libtorch庫 針對這些方案的部署方式可以參考我的文章。這些方式都可以採用C 實現,而且難度不大,效果又極好。對於C 的知識量而...