当前位置:网站首页>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
边栏推荐
- 使用EFR32作为Zigbee/Thread的sniffer的用法
- [Redis Master Cultivation Road] Jedis - the basic use of Jedis
- 需求设计文档和产品经理的角色改变
- 2.4希尔排序
- 四、Web开发
- JQ source code analysis (environment)
- Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b
- Go study notes (84) - Go project directory structure
- Discourse 自定义头部链接(Custom Header Links)
- Discourse Custom Header Links
猜你喜欢
Discourse 自定义头部链接(Custom Header Links)
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
Thymeleaf简介
DAY17、CSRF 漏洞
代码开源设计实现思路
2.6基数排序(桶排序)
Excellent MySQL interview questions, 99% must ask in preparation for August!I can't pass the interview
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
使用EFR32作为Zigbee/Thread的sniffer的用法
handler+message [message mechanism]
随机推荐
文件系统二
A must see for software testers!Database knowledge MySQL query statement Daquan
Android Studio 实现登录注册-源代码 (连接MySql数据库)
Discourse 自定义头部链接(Custom Header Links)
(题目练习)条件概率+权值线段树+FWT+后缀数组
Boss Rush (two-point answer + DP)
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
如何与墨西哥大众VW Mexico建立EDI连接
MySql 怎么查出符合条件的最新的数据行?
The Complete Go Books - Beginner to Advanced and Web Development
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?
模拟问题(下)
Get the local IP and Request's IP
【Redis高手修炼之路】Jedis——Jedis的基本使用
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
Shell script basic editing specifications and variables
MySQL 操作语句大全(详细)
MNIST of Dataset: MNIST (handwritten digital image recognition + ubyte.gz file) data set introduction, download, usage (including data enhancement) detailed guide