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 4
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 islegalprovided 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 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 integernthat
is the size of the board;nwill be at most 4. The nextnlines
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.
For each test case, output one line containing the maximum number of rooks that can be placed on the board in a legal configuration.
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
5
1
5
2
4
Miguel A. Revilla
2000-01-17
和八皇后类似,使用回溯,只是判断是否冲突的方法不一样。
#include<iostream>
#include<cstring>
using namespace std;
char grid[6][6],vis[6][6];
int maxnum,n,num;
bool judge(int posx,int posy)
{
int i,j;
for(i=posx;i<n;i++)
{
if(vis[i][posy]==1&&grid[i][posy]=='.') return false;
else if(grid[i][posy]=='X') break;
}
for(i=posx;i>=0;i--)
{
if(vis[i][posy]==1&&grid[i][posy]=='.') return false;
else if(grid[i][posy]=='X') break;
}
for(i=posy;i<n;i++)
{
if(vis[posx][i]==1&&grid[posx][i]=='.') return false;
else if(grid[posx][i]=='X') break;
}
for(i=posy;i>=0;i--)
{
if(vis[posx][i]==1&&grid[posx][i]=='.') return false;
else if(grid[posx][i]=='X') break;
}
return true;
}
void search(int posx,int posy)
{
int i,j;
num++;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(judge(i,j)&&grid[i][j]=='.'&&vis[i][j]==0)
{
vis[i][j]=1;
search(i,j);
num--;
vis[i][j]=0;
}
}
}
if(num>maxnum) maxnum=num;
}
int main()
{
while(cin>>n&&n)
{
int i,j,k;
memset(grid,0,sizeof(grid));
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>grid[i][j];
maxnum=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(grid[i][j]!='X')
{
memset(vis, 0, sizeof(vis));
vis[i][j]=1;
num=0;
search(i,j);
vis[i][j]=0;
}
}
}
cout<<maxnum<<endl;
}
return 0;
}
分享到:
相关推荐
THR2363 - Don’t get phished!.pptx
course don't load course don't load course don't load course don't load
THR2363R - Don’t get phished (REPEAT).pptx
Don’t Make Me Think田士庆
Don't Make Me Think.pdf 高清 书签版
《You Don't Know JS》是一套深度解析JavaScript语言核心机制的系列书籍,旨在帮助开发者深入理解这门广泛使用的编程语言。这套书全面覆盖了JavaScript的关键概念和技术,包括但不限于闭包、作用域链、ECMAScript 5...
Why don't you get her a scarfPPT教案学习.pptx
You probably don’t really know. Computers and the Internet have revolutionized the modern world, but if you’re like most people, you have no clue how these things work and don’t know the real ...
"Apache You don't have permission to access / on this server" 是一个经典的Apache服务器错误信息,它意味着用户尝试访问网站时遇到了权限问题。这个错误通常出现在Apache HTTP服务器的配置不当或者目录权限设置...
《不要让我思考》是Steve Krug撰写的一本关于网页可用性的设计书籍,它被广大用户界面(UI)和用户体验(UX)设计师奉为经典之作。该书的第二版包含了作者对现代网页设计的新见解和案例,旨在告诉读者如何通过常识性...
《不要让我思考》这本书是Web设计领域的一部经典之作,作者是Steve Krug。书中提出了许多关于网页设计的基本原则,这些原则对于设计师、产品经理、网页开发者等从事与Web界面设计相关工作的专业人士具有极大的指导...
本篇导学案聚焦于人教新目标版八年级英语下册Unit 8 "Why don’t you get her a scarf?"的第三课时,旨在帮助学生掌握一系列与建议和选择礼物相关的句型,同时提升他们的口语能力。以下是本单元的重点知识和教学流程...
No matter how much experience you have with JavaScript, odds are you don't fully understand the language. As part of the "You Don't Know JS" series, this concise yet in-depth guide focuses on new ...
《You Don't Know JS》是一套著名的JavaScript编程书籍,由Kyle Simpson撰写,旨在深入解析JavaScript的各个核心概念,帮助开发者真正理解这门语言的精髓。本资源提供了高清中文版的第1-3部分,分别是“3this与对象...
这篇内容主要涉及的是初中英语的学习,特别是八年级下册Unit 8 "Why don’t you get her a scarf?" 的基础知识练习。这个单元主要围绕送礼物和表达祝福的情境展开,涉及到词汇、语法、听力和口语等多个方面。 1. ...
《You Don't Know JS》是一套深度探讨JavaScript编程语言的权威书籍,包含了全面且深入的知识点,适合进阶学习者和专业开发者。这套书共有六本,每本都专注于JavaScript的不同方面,旨在帮助读者理解语言的微妙之处...
### "Don't Make Me Think 4":用户体验设计的核心原则 #### 一、书籍简介与核心价值 《Don't Make Me Think》(别让我思考)是用户体验设计领域中的一本经典著作,由Steve Krug撰写。这本书强调了简单、直观的...