当前位置:网站首页>【每日一题】棋盘问题
【每日一题】棋盘问题
2022-04-23 12:26:00 【小梁说代码】
Instruction
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证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函数是传值还是传引用的问题,函数里面修改的结果,会不会影响调用者
比如说,下面那行加红框的代码为什么要存在?
- 基本变量就是传递的引用,不改变调用者本身,像int,boolean,float等
- 对象就是传递的引用,函数和调用者都是操作的同一个对象。因为数组是对象,所以上面要对col数组修改后,再修改回来
- 但是String比较特别,虽然它是对象,但是仍然传递的是引用,因为String是用final修饰,默认不可修改。函数传递的时候,其实是另开了一个空间传递了相同的String 对象。
版权声明
本文为[小梁说代码]所创,转载请带上原文链接,感谢
https://liangyuanshao.blog.csdn.net/article/details/124332888
边栏推荐
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
- 【unity笔记】L4Unity中的基础光照
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
- 画结果图推荐网址
- 异步时钟亚稳态 的解决方案——多bit信号
- 1. Construction of electron development environment
- How do programmers finalize nucleic acid statistics with 130 lines of code
- After a circle, I sorted out this set of interview questions..
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
猜你喜欢
Qt绘制文字
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Pagoda panel command line help tutorial (including resetting password)
VMware virtual machines export hard disk vmdk files using esxi
Recommended programming AIDS: picture tool snipaste
How do traditional enterprises cope with digital transformation? These books give you the answer
Metalama简介4.使用Fabric操作项目或命名空间
IDEA设置版权信息
VMware虚拟机使用esxi 导出硬盘vmdk文件
1. Construction of electron development environment
随机推荐
关于使用Go语言创建WebSocket服务浅谈
Idea setting copyright information
Symmetric encryption, certificate encryption
传统企业如何应对数字化转型?这些书给你答案
第二十六课 类的静态成员函数
Metalama简介4.使用Fabric操作项目或命名空间
SSL证书退款说明
Castle. Dynamic proxy implements transaction unit control
Zigbee之CC2530最小系统及寄存器配置(1)
Running error: unable to find or load the main class com xxx. Application
运行报错:找不到或无法加载主类 com.xxx.Application
Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
对称加密、证书加密
Source code analysis of synchronousqueue
【unity笔记】L4Unity中的基础光照
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
How much does software testing help reduce program bugs?
Array---
Everything can be expected in the future | one 2022 campus recruitment officially opened
Relu function of activation function