| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 2134 | Accepted: 1359 |
Description
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
Output
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
Source
总情况数也就2^(4*4)大概6W多种,暴力dfs一下就行了
如果再大点要么网络流(?)要么状态dp吧……
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,cnt;
char map[5][5];
int put[20];
struct Point
{
int x,y;
};
bool Check(int p,int q)
{
Point s,t;
int i;
s.x=p/n;s.y=p%n;
t.x=q/n;t.y=q%n;
bool row,col;
if (s.x!=t.x) row=false;
else
{
for (i=min(s.y,t.y)+1;i<max(s.y,t.y);i++)
{
if (map[s.x][i]=='X') break;
}
if (i==max(s.y,t.y)) row=true;
else row=false;
}
if (s.y!=t.y) col=false;
else
{
for (i=min(s.x,t.x)+1;i<max(s.x,t.x);i++)
{
if (map[i][s.y]=='X') break;
}
if (i==max(s.x,t.x)) col=true;
else col=false;
}
return (col | row);
}
void DFS(int t,int now)
{
int i;
if (t==n*n)
{
cnt=max(cnt,now);
return;
}
DFS(t+1,now);
if (map[t/n][t%n]=='X') return;
for (i=0;i<now;i++)
{
if (Check(put[i],t)) break;
}
if (i<now) return;
put[now]=t;
DFS(t+1,now+1);
}
int main()
{
int i,j;
while(1)
{
scanf("%d",&n);
if (n==0) break;
for (i=0;i<n;i++)
{
scanf("%s",map[i]);
}
cnt=0;
DFS(0,0);
printf("%d\n",cnt);
}
return 0;
}
397



被折叠的 条评论
为什么被折叠?



