UVa 167 - The Sultan's Successors
2014-08-01 19:24
357 查看
题目:八皇后问题,棋盘的每个格子里有一个数值,和最大的摆放方式。
分析:搜索。裸的八皇后,加上求和即可。
说明:╮(╯▽╰)╭貌似写的很不简洁啊。
分析:搜索。裸的八皇后,加上求和即可。
说明:╮(╯▽╰)╭貌似写的很不简洁啊。
#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int maps[8][8]; int smap[8][8]; int max_sum; void change( int x, int y, int dx, int dy ) { int id = x+1; while ( x < 8 && x >= 0 && y < 8 && y >= 0 ) { if ( !smap[x][y] ) smap[x][y] = id; else if ( smap[x][y] == id ) smap[x][y] = 0; x += dx; y += dy; } } void dfs( int x, int v ) { if ( x == 8 ) { if ( max_sum < v ) max_sum = v; return; } for ( int i = 0 ; i < 8 ; ++ i ) if ( !smap[x][i] ) { change( x, i, 1, 0 ); change( x, i, 1, 1 ); change( x, i, 1,-1 ); dfs( x+1, v+maps[x][i] ); change( x, i, 1, 0 ); change( x, i, 1, 1 ); change( x, i, 1,-1 ); } } int main() { int n; while ( ~scanf("%d",&n) ) for ( int t = 1 ; t <= n ; ++ t ) { for ( int i = 0 ; i < 8 ; ++ i ) for ( int j = 0 ; j < 8 ; ++ j ) { scanf("%d",&maps[i][j]); smap[i][j] = 0; } max_sum = 0; dfs(0, 0); printf("%5d\n",max_sum); } return 0; }
相关文章推荐
- uva 167 - The Sultan's Successors(典型的八皇后问题)
- Uva-167-The Sultan's Successors
- UVa 167 - The Sultan's Successors 递归回溯
- UVA - 167 The Sultan's Successors(回溯 八皇后问题改编)
- UVa 167 - The Sultan's Successors, 八皇后问题
- uva 167 - The Sultan's Successors
- UVa 167 The Sultan's Successors (八皇后问题)
- uva-167 - The Sultan's Successors-八皇后-回溯
- uva 167 - The Sultan's Successors
- uva167 -The Sultan's Successors(类八皇后)
- UVA_167_The Sultan's Successors
- uva 167 - The Sultan's Successors(典型的八皇后问题)
- uva 167 The Sultan's Successors
- UVa 167 - The Sultan's Successors
- 【权值八皇后】uva 167——The Sultan's Successors
- uva 167 The Sultan's Successors 搜索
- UVA 167 The Sultan's Successors
- UVA The Sultan's Successors
- UVaOJ167 - The Sultan's Successors
- 167 - The Sultan's Successors 回溯