当前位置:网站首页>POJ1321 chessboard problem (detailed explanation)
POJ1321 chessboard problem (detailed explanation)
2022-07-30 04:37:00 【_rosy】
Title
Place chess pieces on a board of a given shape (the shape may be irregular) with no difference between the pieces.It is required that any two chess pieces cannot be placed in the same row or the same column in the chessboard. Please program to solve all feasible placement schemes C for placing k chess pieces for a chessboard of a given shape and size.
Input
The input contains multiple sets of test data.
The first line of each set of data is two positive integers, n k, separated by a space, indicating that the chessboard will be described in an n*n matrix and the number of pieces placed.n <= 8 , k <= n
When it is -1 -1, it means the end of input.
The next n lines describe the shape of the chessboard: each line has n characters, where # represents the chessboard area and . represents the blank area (the data guarantees that no extra blank rows or columns will appear).
Output
For each set of data, give a line of output, and output the number of solutions C (data guarantee C<2^31).
Sample
Inputcopy | Outputcopy |
---|---|
2 1#..#4 4...#..#..#..#...-1 -1 |
Ideas:
When I saw this question, the first thing that came to my mind was the Queen n question. For those who don't remember much, you can look at this article, they all continue to search for the next line if the conditions of this line are met.But this question is a bit different. The problem of n queens is that n rows and n queens must be filled, and this question is difficulty 1:n rows only need to be filledk is OK, so this is a difficult point.How to solve it?In the n queens, the condition is satisfied when the current number of rows is greater than the total n rows, then we are actually similar here. We need to create a new parameter m here, which represents the number of chess pieces we have placed, when the number of chess pieces is equal to the target k valueTime is a plan.Difficulty 2:Two pieces cannot be placed on the same row or column on the board.In fact, the same line is easy to handle, because we will not repeat it at all, because when there is a column in the current line that satisfies the condition, we directly search for the next line, so there will be no conflict.What about the same column?In fact, it is also simple, that is, we directly define an array to indicate whether the current column has been selected, because the same column, of course, the same column of all rows is represented by the same pos[j].
Code:
#include#include#include#include#include
边栏推荐
猜你喜欢
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
Database Design of Commodity Management System--SQL Server
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
DAY17:弱口令的探测与测试
Unity beginner 5 cameras follow, border control and simple particle control (2 d)
2.4希尔排序
Discourse Custom Header Links
05全局配置文件application.properties详解
The Complete Go Books - Beginner to Advanced and Web Development
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
随机推荐
Boss Rush (two-point answer + DP)
golang八股文整理(持续搬运)
swagger usage tutorial - quick use of swagger
BGP的简单实验
共建共享数字世界的根:阿里云打造全面的云原生开源生态
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
C. Travelling Salesman and Special Numbers (binary + combination number)
Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
网页元素解析a标签
厦门感芯科技MC3172(1):介绍和环境搭建
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
DAY17、CSRF 漏洞
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!
unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
MySQL 安装报错的解决方法
2.4 hill sorting
Become a qualified cybersecurity, do you know this?
Chapter8 Support Vector Machines