5分dfs求救!!!

P1074 [NOIP2009 提高组] 靶形数独

@[zhong_jia_jun](/space/show?uid=97605) ``` 不知道 本蒟蒻刚学OI不会那什么dfs ``` ~~话说dfs到底是什么啊~~
by 离人怎挽 @ 2019-02-12 21:13:05


@[zhong_jia_jun](/space/show?uid=97605) 我的代码: ``` // luogu-judger-enable-o2 #include<iostream> using namespace std; int g[9][9],highscore=-1; bool hang[9][10],lie[9][10],gong[3][3][10],ok; int fen[9][9]= { {6,6,6,6,6,6,6,6,6}, {6,7,7,7,7,7,7,7,6}, {6,7,8,8,8,8,8,7,6}, {6,7,8,9,9,9,8,7,6}, {6,7,8,9,10,9,8,7,6}, {6,7,8,9,9,9,8,7,6}, {6,7,8,8,8,8,8,7,6}, {6,7,7,7,7,7,7,7,6}, {6,6,6,6,6,6,6,6,6} }; void dfs(int x,int y) { if(x==9) { int cnt=0; for(int i=0;i<9;i++) { for(int j=0;j<9;j++) { cnt+=g[i][j]*fen[i][j]; } } if(cnt>highscore) { highscore=cnt; } return; } if(g[x][y]) { if(y==8) { dfs(x+1,0); } else { dfs(x,y+1); } return; } for(int i=1;i<10;i++) { if(!hang[x][i]&&!lie[y][i]&&!gong[x/3][y/3][i]) { hang[x][i]=lie[y][i]=gong[x/3][y/3][i]=true; g[x][y]=i; if(y==8) { dfs(x+1,0); } else { dfs(x,y+1); } hang[x][i]=lie[y][i]=gong[x/3][y/3][i]=false; } } g[x][y]=0; } int main() { for(int i=0;i<9;i++) { for(int j=0;j<9;j++) { cin>>g[i][j]; if(g[i][j]) { hang[i][g[i][j]]=true; lie[j][g[i][j]]=true; gong[i/3][j/3][g[i][j]]=true; } } } dfs(0,0); cout<<highscore; return 0; } ```
by beargeng是女孩子 @ 2019-02-12 22:51:38


|