← ↑ | 例: 8-Queenプログラム | → |
... int queens = 8; main(argc, argv) char **argv; { int i; for(i=0;i<queens;i++){ col[i] = qpos[i] = 0; } for(i= -queens;i<queens;i++){ up[i] = down[i] = 0; } extend(0); } extend(n) { int c; for(c=0;c<queens;c++){ if(!col[c] && !up[n+c] && !down[n-c]){ qpos[n] = c; if(n+1 >= queens) printqueens(); else { col[c] = up[n+c] = down[n-c] = 1; extend(n+1); col[c] = up[n+c] = down[n-c] = 0; } } } }