当前位置:网站首页>【每日一题】棋盘问题
【每日一题】棋盘问题
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
边栏推荐
- 万事有你 未来可期 | ONES 2022校园招聘正式开启
- Windows2008系统如何切换PHP版本
- Qt进程间通信
- SQLserver怎么插入或更新当天的星期数,bit而不是文本
- Lesson 26 static member functions of classes
- Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
- 宝塔面板命令行帮助教程(包含重置密码)
- Qt一个进程运行另一个进程
- Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
- [redis series] redis learning 13. Redis often asks simple interview questions
猜你喜欢

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings

亿级流量架构,服务器如何扩容?写得太好了!

Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years

QT draw image

Metalama简介4.使用Fabric操作项目或命名空间

如果你是一个Golang面试官,你会问哪些问题?

Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").

Here comes the detailed picture and text installation tutorial of H5 game

STM32控制步进电机(ULN2003+28byj)
随机推荐
Force buckle - 70 climb stairs
Lesson 25 static member variables of classes
Intelligent multi line elastic cloud adds independent IP address. How to realize multi line function?
第二十五课 类的静态成员变量
Metalama简介4.使用Fabric操作项目或命名空间
Markdown grammar learning
Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
运行报错:找不到或无法加载主类 com.xxx.Application
VMware virtual machines export hard disk vmdk files using esxi
Stacks and queues a
Pagoda panel command line help tutorial (including resetting password)
网络信息安全之零信任
[wechat applet] Z-index is invalid
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
XinChaCha Trust SSL Organization Validated
How does sqlserver insert or update the number of weeks of the day instead of text
Solution of asynchronous clock metastability -- multi bit signal
程序员如何用130行代码敲定核酸统计
IDEA 中 .properties文件的中文显示乱码问题的解决办法