This problem is a typical eight queen question, Rujia book has a specific explanation.
The implementation of the code is as follows:
#include <stdio.h> #include <string.h> #include <stdlib.h>int vis[100][100];//just started wrong because the array is small. , opened [8][8], thought can. Later saw [cur-i+8] realized that the array might be small. After the conversion, AC.int A[8][8];int c[10];int max_,tot;void search_ (int cur) {int i,j; if (cur==8) {int ans=0; tot++; for (int k=0;k<8;k++) {ans+=a[k][c[k]]; } if (Ans>max_) Max_=ans; } else for (i=0;i<8;i++) {if (!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+8]) { C[cur]=i; Vis[0][i]=vis[1][cur+i]=vis[2][cur-i+8]=1; Search_ (cur+1); vis[0][i]=vis[1][cur+i]=vis[2][cur-i+8]=0; }}}int Main () {int t; scanf ("%d", &t); while (t--) {for (int. i=0;i<8;i++) {for (int j=0;j<8;j++) {SCA NF ("%d", &a[i][j]); }} Max_=-1; tot=0; memset (vis,0,sizeof (VIS)); Search_ (0); printf ("tot =%d\n", tot); printf ("%5d\n", max_); } return 0;}
As a novice, you should find the wrong time to find out what you think is not good enough, rather than complaining about that.
You complain again that you are still the rookie of nothing, and the person you complain about will still be the great God.
No matter what kind of strange situation you encounter, calm down. Think more and don't complain.
UVA 167-the sultan& #39; s successors (typical eight-queen question)