当前位置:网站首页>[daily question] chessboard question
[daily question] chessboard question
2022-04-23 12:34:00 【Trabecular said code】
Instruction
On a chessboard of a given shape ( The shape may be irregular ) There are chess pieces on it , There is no difference in chess . It is required that any two pieces should not be placed in the same line or column in the chessboard , Please program to solve for the given shape and size of the chessboard , put k All the feasible placement plans of a piece C.
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 , Indicates that there will be a n*n The matrix of describes the chessboard , And the number of pieces placed . n <= 8 , k <= n
When it comes to -1 -1 Is the end of the input .
And then n Line describes the shape of the chessboard : Each row has n Characters , among # Represents the board area , . Indicates a blank area ( Data is guaranteed to be free of redundant blank rows or columns ).
Output
For each set of data , Give a line of output , Number of output placement schemes C ( Data assurance C<2^31).
Sample
Answer
import java.util.Scanner;
import javax.sound.midi.SoundbankResource;
/** * @author liangyuanshao * @date 2022/4/21 - 17:46 */
public class Main {
static Boolean[][] arr;
static int n,k;
static int count=0;
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
while (true){
n=s.nextInt(); k=s.nextInt();
if(n!=-1){
arr=new Boolean[n][n];
for(int i=0;i<n;i++){
String temp=s.next();
for(int j=0;j<temp.length();j++){
arr[i][j]=temp.charAt(j)=='#';
}
}
count=0;
dfs(0,new boolean[n],k);
System.out.println(count);
}else {
break;
}
}
s.close();
}
public static void dfs(int row,boolean[] col,int num){
if(num==0){
count++;
return;
}
if(row==n){
return ;
}
dfs(row+1,col,num);
for(int j=0;j<n;j++){
if(arr[row][j]&&!col[j]){
col[j]=true;
dfs(row+1,col,num-1);
col[j]=false;
}
}
}
}
Experience
Java Whether a function passes a value or a reference , The result of modification in the function , Will it affect the caller
for instance , Why does the code with red box exist in the following line ?
- The basic variable is the passed reference , Do not change the caller itself , image int,boolean,float etc.
- An object is a passed reference , Both the function and the caller are the same object of the operation . Because arrays are objects , So the top should be right col After array modification , Then modify it back
- however String More special , Although it's an object , But what is still passed is a reference , because String Yes, it is final modification , It is not modifiable by default . When a function is passed , In fact, it opens another space and transmits the same String object .
版权声明
本文为[Trabecular said code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231226089415.html
边栏推荐
- box-sizing
- Resolve disagrees about version of symbol device_ create
- XinChaCha Trust SSL Organization Validated
- IDEA设置版权信息
- Fastjson 2 来了,性能继续提升,还能再战十年
- Analysis of InnoDB execution process in MySQL
- 第二十四课 经典问题解析
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Intelligent multi line elastic cloud adds independent IP address. How to realize multi line function?
- SQL 练习(一)
猜你喜欢
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
程序员如何用130行代码敲定核酸统计
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Everything can be expected in the future | one 2022 campus recruitment officially opened
SSL证书退款说明
On lambda powertools typescript
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
SQL exercise (I)
AI video cloud vs narrowband HD, who is the darling of the video era
One way ANOVA of SPSS
随机推荐
c# 设置logo图标和快捷方式的图标
NBIOT的AT指令
AD20补充笔记3—快捷键+持续更新
Idea setting copyright information
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
RT-thread中关键词解释及部分API
Zigbee之CC2530最小系统及寄存器配置(1)
How much does software testing help reduce program bugs?
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
C set Logo Icon and shortcut icon
After a circle, I sorted out this set of interview questions..
Markdown语法学习
The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres
解决disagrees about version of symbol device_create
How does sqlserver insert or update the number of weeks of the day instead of text
Introduction to metalama 4 Use fabric to manipulate items or namespaces
BUUCTF WEB [GXYCTF2019]禁止套娃
免费试用一个月的服务器,并附上教程
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
Lesson 26 static member functions of classes