当前位置:网站首页>[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
边栏推荐
猜你喜欢

在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决

mysql中 innoDB执行过程分析

【Redis 系列】redis 学习十三,Redis 常问简单面试题

Zero trust in network information security

网站首页文件被攻击篡改的形式有哪些

NPDP|产品经理如何做到不会被程序员排斥?

How do traditional enterprises cope with digital transformation? These books give you the answer

Analysis of InnoDB execution process in MySQL

面了一圈,整理了这套面试题。。

同态加密技术学习
随机推荐
Zero trust in network information security
BUUCTF WEB [BJDCTF2020]The mystery of ip
QT interprocess communication
【vulnhub靶场】-dc2
关于使用Go语言创建WebSocket服务浅谈
Idea code formatting plug-in save actions
How do traditional enterprises cope with digital transformation? These books give you the answer
AI video cloud vs narrowband HD, who is the darling of the video era
CGC: contractual graph clustering for community detection and tracking
Recommended programming AIDS: picture tool snipaste
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
Zigbee之CC2530最小系统及寄存器配置(1)
Pre competition practice of TIANTI competition
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
QT draw text
Plato Farm-以柏拉图为目标的农场元宇宙游戏
XinChaCha Trust SSL Organization Validated
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
SQL 练习(一)