UVA 639 - Don't Get Rooked(深搜)
2013-08-09 14:59
363 查看
Don't Get Rooked |
4)
that can also contain walls through which rooks cannot move. The goal is to place as many rooks on a board as possible so that no two can capture each other. A configuration of rooks is legal provided that no two rooks are on the
same horizontal row or vertical column unless there is at least one wall separating them.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of rooks
in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a board, calculates the maximum number of rooks that can be placed on the board in a legal configuration.
Input
The input file contains one or more board descriptions, followed by a line containing the number 0 that signals the end of the file. Each boarddescription begins with a line containing a positive integer n that
is the size of the board; n will be
at most 4. The next n lines each describe
one row of the board, with a `.' indicating an open space and an uppercase `X'
indicating a wall. There are no spaces in the input file.
Output
For each test case, output one line containing the maximum number of rooks that can be placed on the board in a legal configuration.
Sample Input
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
Sample Output
5 1 5 2 4
==============================
从每一个可以放置的位置开始深搜
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int n,maxans; char map[10][10],vis[10][10]; bool check(int x, int y) { if(vis[x][y]==-1||vis[x][y]==1) return 0; for(int i=x+1;i<=n&&map[i][y]!='X';i++) if(vis[i][y]==-1) return 0; for(int i=x-1;i>=1&&map[i][y]!='X';i--) if(vis[i][y]==-1) return 0; for(int i=y+1;i<=n&&map[x][i]!='X';i++) if(vis[x][i]==-1) return 0; for (int i=y-1;i>=1&&map[x][i]!='X';i--) if(vis[x][i]==-1) return 0; return 1; } void dfs(int ans) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(check(i,j)) { vis[i][j]=-1; dfs(ans+1); vis[i][j]=0; } } } if(maxans<ans) { maxans=ans; } return; } int main() { while(~scanf("%d",&n)) { if(n==0) break; maxans=-1; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { scanf("%s",map[i]+1); for(int j=1;j<=n;j++) { if(map[i][j]=='X') vis[i][j]=1; } } dfs(0); printf("%d\n",maxans); } return 0; }
相关文章推荐
- uva 639 Don't Get Rooked 变形N皇后问题 暴力回溯
- UVa 639 Don't Get Rooked (DFS好题)
- uva 639 Don't Get Rooked
- UVA 639 Don't Get Rooked
- UVA 639 - Don't Get Rooked
- uva 639 Don't Get Rooked (暴力回溯 )
- uva - 639 - Don't Get Rooked
- uva 639 - Don't Get Rooked
- UVa 639 - Don't Get Rooked, 类八皇后问题
- UVa 639 & HOJ 1086 & POJ 1315 - Don't Get Rooked
- Don't Get Rooked -uva
- 639 - Don't Get Rooked(搜索回溯)
- Hoj 1086 Don't Get Rooked
- uva 639 - Don't Get Rooked
- [回溯]Don't Get Rooked UVA 639
- UVA - 639 Don't Get Rooked
- UVA 639 - Don't Get Rooked
- UVA 639 - Don't Get Rooked
- UVa 639 Don't Get Rooked
- UVA 639 Don't Get Rooked