当前位置:网站首页>【每日一题】棋盘问题
【每日一题】棋盘问题
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
边栏推荐
- Number of nodes of complete binary tree
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- Lesson 24 analysis of classical problems
- Metalama简介4.使用Fabric操作项目或命名空间
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- AI 视频云 VS 窄带高清,谁是视频时代的宠儿
- 第二十五课 类的静态成员变量
- Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
- 九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
- Next. JS static data generation and server-side rendering
猜你喜欢
Win10 splash screen after startup
How do programmers finalize nucleic acid statistics with 130 lines of code
Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
Qt双缓冲绘图
XinChaCha Trust SSL Organization Validated
Plato Farm-以柏拉图为目标的农场元宇宙游戏
Force buckle - 1137 Nth teponacci number
Number of nodes of complete binary tree
The maximum number of remote desktop servers has been exceeded
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
随机推荐
A graphic designer's fantasy world | ones characters
AI video cloud vs narrowband HD, who is the darling of the video era
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
Qt一个进程运行另一个进程
Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind
Symmetric encryption, certificate encryption
Markdown语法学习
Lesson 25 static member variables of classes
Next.js 静态数据生成以及服务端渲染的方式
Lesson 24 analysis of classical problems
一个平面设计师的异想世界|ONES 人物
Pagoda panel command line help tutorial (including resetting password)
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
宝塔面板命令行帮助教程(包含重置密码)
Pre competition practice of TIANTI competition
外包干了五年,废了...
After a circle, I sorted out this set of interview questions..
Force buckle - 70 climb stairs
Qt双缓冲绘图