ZOJ 1002
来源:互联网 发布:淘宝七天无条件退货 编辑:程序博客网 时间:2024/04/27 21:47
Don't Get Rooked
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 44) 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 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 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......2XX.X3.X.X.X.X.3....XX.XX4................0
Sample Output
51524
#include<cstdio>#include<cstring>using namespace std;int n,ans,ans_f,rst,rst_f;char board[6][6];bool vis[6][6],temp[6][6];bool Piace(int x,int y){ bool flag=1; if(board[x][y]=='X') flag=0; for(int i=x-1;flag&&i>=0;i--) if(vis[i][y]) { flag=0; break; } else if(board[i][y]=='X') break; for(int i=y-1;flag&&i>=0;i--) if(vis[x][i]) { flag=0; break; } else if(board[x][i]=='X') break; return flag;}void Search(int stt){ if(stt>=n) { rst_f=rst>rst_f?rst:rst_f; return; } for(int i=0;i<n;i++) { int t=0; memset(vis[stt],0,sizeof(vis[stt])); for(int j=i;j<n;j++) if(Piace(stt,j)) { rst++;t++; vis[stt][j]=1; } Search(stt+1); rst=rst-t; }}int main(){ while(scanf("%d",&n)) { if(!n)break; for(int i=0;i<n;i++) scanf("%s",board[i]); memset(vis,0,sizeof(vis)); rst=0;rst_f=0; Search(0); printf("%d\n",rst_f); } return 0;}
- ZOJ 1002
- ZOJ 1002
- zoj 1002
- ZOJ 1002
- ZOJ 1002
- zoj 1002
- zoj-1002
- zoj 1002
- ZOJ 1002
- ZOJ-1002
- ZOJ 1002
- ZOJ-1002
- ZOJ 1002
- ZOJ 1002
- zoj-1002
- ZOJ 1002
- ZOJ 1002
- ZOJ-1002
- centos配置ipv6地址
- 生平第一次玩blog
- 部署JForum 2.1.9遇到的问题及解决方法
- 学生管理系统中遇到的问题
- 河内之塔
- ZOJ 1002
- ASCII码规律
- 如何在windows下用CodeBlock编译Objective-C代码
- hdu(1032)
- source insight 下载地址及注册码
- HDOJ 4611 - Balls Rearrangement/2013多校联合第二场A 分情况处理
- hdu2610Sequence one
- ArraList,LinkList,List,Vector的区别
- QThread 与 QObject的关系