ACM Checkerboard Problem

Source: Internet
Author: User

Description

In a given shape of the chessboard (the shape may be irregular) on the top of the pieces, chess pieces no difference. If you need to place any of the two pieces in the same row or column in the chessboard, please program the chessboard with a given shape and size, and put all the feasible arrangement C for the K pieces.

Input

The input contains multiple sets of test data.
The first row of each set of data is two positive integers, n K, separated by a space, indicating that the chessboard will be described in a n*n matrix and the number of pieces to be placed. N <= 8, K <= N
When 1-1 indicates the end of the input.
The next n lines describe the shape of the chessboard: N characters per line, where # represents the board area. Represents an empty area (the data guarantees that no extra blank lines or blank columns appear).

Output

For each set of data, give a line of output, the output is placed in the number of programs C (Data Assurance c<2^31).

Sample Input

2 #.. #4 4...#. #.. #.. #...-1-1

Sample Output

2

1

Problem Solving Ideas:

The main idea of this topic is to give a chessboard and a given number of pieces we need to place, and then ask us how to put them in several ways. First we can make it clear that this is a topic of deep search, similar to the eight Queens question. We create a function Dfs is used to accumulate the number of feasible scenarios, we walk through a column we will mark it down the next time we can not be placed in this column (because the title is not allowed to be placed in the same row and the same column)

Then start looking for a viable place from the next line, until we have the same number of pieces as we were asked to put in the number of pieces, we will take the number of programs once + +, and then proceed to recursion.

Program code:

#include <cstdio>#include<iostream>#include<cstring>using namespacestd;intN,k,ans;Charmap[ A][ A];//Chess Boardintvis[ A];intDFS (intIintcur) {    if(cur>=k)//{ans++;//Number of programmes        return 0; }    intx, y;  for(x=i;x<n;x++)         for(y=0; y<n;y++)            if(!vis[y] && map[x][y]=='#') {Vis[y]=1;//MarkDFS (x+1, cur+1);//Recursivevis[y]=0; }    return 0;}intMain () { while(SCANF ("%d%d", &n,&k) &&n!=-1) {ans=0; memset (Map,0,sizeof(map)); memset (Vis,0,sizeof(VIS));  for(intI=0; i<n;i++) scanf ("%s", Map[i]); DFS (0,0); printf ("%d\n", ans); }    return 0;}

ACM Checkerboard Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.