uva 639
来源:互联网 发布:数据库里可不可以用int 编辑:程序博客网 时间:2024/06/01 08:00
Description
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
和书上八皇后问题很相像,只是多了墙的问题,我的做法是枚举所有情况,然后判断是否可以达到要求来统计,其中枚举我是利用的是二进制模拟,诶在这个上面犯了下2,&写成了&&调试一个小时才找到,这些都是需要很仔细的地方,整个题的思路就是枚举+回溯,回溯是为了还原到每一次状态的原来状态,这一点深有体会,对于初学的我来说这是一个很好的题,让我进一步明白回溯含义。
#include<stdio.h>#include<stdlib.h>#include<cstring>#include<math.h>#include<algorithm>using namespace std;int n,vis[6][6],ans;char maze[6][6];bool check(){int ok;for(int i=0;i<n;i++){//检查每一行是否满足要求 ok=0;for(int j=0;j<n;j++){if(vis[i][j]==1)ok++;if(vis[i][j]==2&&ok==1)ok--;if(ok==2)return false; }}for(int i=0;i<n;i++){//检查每一列是否满足要求 ok=0;for(int j=0;j<n;j++){if(vis[j][i]==1)ok++;if(vis[j][i]==2&&ok==1)ok--;if(ok==2)return false; }}return true;}void search(int cur,int sum){if(cur==n){if(check()) ans=max(ans,sum);return;} for(int i=0;i<pow(2,n);i++){ int num=0; for(int j=0;j<n;j++){ if((i&(1<<j))&&maze[cur][j]=='.'){//可以放车 vis[cur][j]=1; num++; } if(maze[cur][j]=='X')vis[cur][j]=2;//不能放车 } search(cur+1,sum+num); for(int j=0;j<n;j++)vis[cur][j]=0;//回溯初始还原 }}int main(){while(scanf("%d",&n)==1&&n){for(int i=0;i<n;i++){scanf("%s",maze[i]);getchar();} ans=0; search(0,0); printf("%d\n",ans); }return 0;}
- uva 639
- uva 639
- UVA 639
- uva 639
- uva 639
- uva 639
- UVA 639
- UVA 639 (13.08.25)
- uva 639(回溯)
- uva--639+dfs+回溯
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- SAP一句话入门:Financial & Controlling Accounting
- 字典树 Trie 的实现
- 标准C++中map容器的用法总结
- 在Mac OS X中配置Apache + PHP + MySQL
- PHP如何连接sqlserver2005数据库
- uva 639
- Android中各种onTouch事件
- 模仿人人网 拖动滚动条到页面底端时加载新消息[AJAX、jQuery、web service]
- 脚本整理
- Struts2中Action由自己与由Spring管理的区别
- telerik RadTreeList的用法
- cocos2d-x Lua 之 CCMoveTo 误区
- [leetcode刷题系列]Jump Game
- Collection:List、Set和Map:HashMap、HashTable区别