POJ 1315 Don't Get Rooked(DFS)
来源:互联网 发布:网络诈骗揭秘 编辑:程序博客网 时间:2024/05/17 01:36
Don't Get Rooked
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2290 Accepted: 1463
Description
In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 4x4) 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.
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 contains one or more board descriptions, followed by a line containing the number 0 that signals the end of the file. Each board description 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.
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......2XX.X3.X.X.X.X.3....XX.XX4................0
Sample Output
51524
和N皇后问题类似,这道题让求出棋盘在被墙分隔的情况下的最多能放的棋子的个数,使分隔后的每行每列仅有一个棋子。方法是从前到后dfs,同时用4个循环判断位置是否合法。
#include<queue>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN=1e2+50;char map[MAXN][MAXN];int vis[MAXN][MAXN],N,ans;int over(int x,int y){ if(x<0||x>=N||y<0||y>=N) return 1; return 0;}int judge(int x,int y){ for(int i=x-1; i>=0&&map[i][y]=='.'; i--) { if(vis[i][y]) { return 0; } } for(int i=x+1; i<N&&map[i][y]=='.'; i++) { if(vis[i][y]) { return 0; } } for(int i=y-1; i>=0&&map[x][i]=='.'; i--) { if(vis[x][i]) { return 0; } } for(int i=y+1; i<N&&map[x][i]=='.'; i++) { if(vis[x][i]) { return 0; } } return 1;}void dfs(int x,int y,int num){ if(over(x,y)) return ; ans=max(ans,num); for(int i=x*N+y+1; i<N*N; i++) { int xx=i/N; int yy=i%N; if(!vis[xx][yy]&&!over(xx,yy)&&map[xx][yy]=='.'&&judge(xx,yy)) { vis[xx][yy]=1; dfs(xx,yy,num+1); vis[xx][yy]=0; } }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE while(scanf("%d%*c",&N)!=EOF&&N) { ans=0; for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { scanf("%c",&map[i][j]); } getchar(); } memset(vis,0,sizeof(vis)); for(int i=0; i<N*N; i++) { int x=i/N,y=i%N; if(map[x][y]=='.') { vis[x][y]=1; dfs(x,y,1); vis[x][y]=0; } } printf("%d\n",ans); } return 0;}
0 0
- POJ 1315 Don't Get Rooked(DFS)
- poj 1315 Don't Get Rooked
- poj 1315 Don't Get Rooked
- POJ 1315 Don't Get Rooked 笔记
- Don't Get Rooked(poj1315,dfs)
- UVa 639 & HOJ 1086 & POJ 1315 - Don't Get Rooked
- poj 1315 Don't Get Rooked 暴力枚举
- hdu 1315 Don't Get Rooked--深搜
- UVa 639 Don't Get Rooked (DFS好题)
- UVA Don't Get Rooked (DFS回溯)
- PKU1315 Don't Get Rooked
- 639--Don't Get Rooked
- 639Don't Get Rooked
- 639 - Don't Get Rooked
- uva639 - Don't Get Rooked
- UVaOJ639---Don't Get Rooked
- UVA639 Don't Get Rooked
- POJ1315:Don't Get Rooked
- HDU 1237 简单计算器(栈)
- N的M次方最右边的数字
- 程序员必读书单 1.0
- POJ 1068 Parencodings(暴力枚举)
- 对角化原理和停机问题
- POJ 1315 Don't Get Rooked(DFS)
- ATT汇编
- Cognos报表参数值保存到Excel
- Linux-config-prefix
- 对cocos2d 之autorelease\ratain\release的理解
- MODIS数据
- html特殊字符
- C语言之函数调用06—彩球排列
- 截止2015-07-21日记录