poj1315--Don't Get Rooked(DPS)

来源:互联网 发布:数据采集卡原理设计 编辑:程序博客网 时间:2024/06/07 00:07

Description

In chess, the rookis a piece that can move any number of squares vertically orhorizontally. In this problem we will consider small chess boards(at most 4x4) that can also contain walls through which rookscannot move. The goal is to place as many rooks on a board aspossible so that no two can capture each other. A configuration ofrooks is legal provided that no two rooks are on the samehorizontal row or vertical column unless there is at least one wallseparating them.

The following image shows five pictures of the same board. Thefirst picture is the empty board, the second and third picturesshow legal configurations, and the fourth and fifth pictures showillegal configurations. For this board, the maximum number of rooksin a legal configuration is 5; the second picture shows one way todo it, but there are several other ways.
poj1315--Don't <wbr>Get <wbr>Rooked(DPS)

Your task is to write a program that, given a description of aboard, calculates the maximum number of rooks that can be placed onthe board in a legal configuration.

Input

The input containsone or more board descriptions, followed by a line containing thenumber 0 that signals the end of the file. Each board descriptionbegins with a line containing a positive integer n that is the sizeof the board; n will be at most 4. The next n lines each describeone row of the board, with a '.' indicating an open space and anuppercase 'X' indicating a wall. There are no spaces in theinput.

Output

For each test case,output one line containing the maximum number of rooks that can beplaced 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<stdio.h>
struct node{
       int x,y;
};
struct node point[100];
char map[20][20];
int count,k,n;
int search(int step)
{
       int kx,ky,i;
       kx=point[step].x;
       ky=point[step].y;
       for(i=ky;i<n;i++)
       {
              if(map[kx][i]=='1')
                     return 0;
              else if(map[kx][i]=='X')
                     break;
       }
       for(i=ky;i>=0;i--)
       {
              if(map[kx][i]=='1')
                     return 0;
              else if(map[kx][i]=='X')
                     break;
       }
       for(i=kx;i<n;i++)
       {
              if(map[i][ky]=='1')
                     return 0;
              else if(map[i][ky]=='X')
                     break;
       }
       for(i=kx;i>=0;i--)
       {
              if(map[i][ky]=='1')
                     return 0;
              else if(map[i][ky]=='X')
                     break;
       }
       return 1;
}
void DFS(int step,int num)
{
       int i;
       for(i=step;i<=k;i++)
       {
              if(search(i))
              {
                     map[point[i].x][point[i].y]='1';
                     DFS(step+1,num+1);
                     map[point[i].x][point[i].y]='.';
              }
       }
       if(count<num)
              count=num;
}
 
int main()
{
       int i,j;
       while(scanf("%d",&n),n!=0)
       {
              for(i=0;i<n;i++)
                     scanf("%s",map[i]);
        k=0;
              for(i=0;i<n;i++)
                     for(j=0;j<n;j++)
                     {
                            if(map[i][j]=='.')
                            {
                                   point[k].x=i;
                                   point[k].y=j;
                                   k++;
                            }
                     }
              k--;
              count=0;
              DFS(0,0);
              printf("%d\n",count);
       }
       return 0;
}
 
 
0 0
原创粉丝点击