Test instructions: Similar to the 8 queen question, except that each lattice on the board has a score and the Queen's position is the one that can be obtained. Or the rule of the 8 Queen's question, and finally get the best possible score.
Idea: 8 Queen problem solution, after 8 Queens to judge the score on the line.
In the If statement judgment content that piece unexpectedly still write wrong once, not satisfied ~
You can also use vis[3][2*8]; array markers, and then quickly
Code:
#include <stdio.h>void dfs (int cur,int score), int chess[8][8];int bestscore;int c[8];int main () { //freopen (" 167.in "," R ", stdin); Freopen ("167.out", "w", stdout); int k; scanf ("%d", &k); while (k-->0) { for (int. i=0;i<8;++i) for (int j=0;j<8;++j) { scanf ("%d", &chess[i] [j]); } bestscore=0; DFS (0,0); printf ("%5d\n", Bestscore); } return 0;} void Dfs (int cur,int score) { if (cur==8) { bestscore=score>bestscore?score:bestscore; } else for (int i=0;i<8;++i)//try each column { c[cur]=i; int ok=1; for (int j=0;j<cur;++j)//The following if statement is cur instead of I ... if (C[cur]==c[j] | | | C[cur]+cur==c[j]+j | | C[CUR]-CUR==C[J]-J) {ok=0; break;} if (OK) { dfs (cur+1,score+chess[cur][c[cur]);} } }
UVa 167 successor of the Sudan