题一:数独游戏
你一定听说过“数独”游戏。
如图,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。
数独的答案都是唯一的,所以,多个解也称为无解。
本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。
本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。
格式要求,输入9行,每行9个字符,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。
例如:
输入(即图中题目):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700
程序应该输出:
145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764
import java.util.ArrayList;
import java.util.Scanner;
public class lianXI {
/*
* dfs(table,x,y){
* if(x==9){
* print(table);
* System.exit(0);
* }
*
* if(table[x][y]=='0'){
* //选1-9之间合法的数字填到x,y这个位置
* for(i..9){
* boolean res = check(table,x,y,i);
* if(res){
* table[x][y] = (char)('0' + i); //转移到下一个状态
* dfs(table,x+(y+1)/9,(y+1)%9);
* }
* }
* table[x][y] = '0'; //回溯
* }else{
* //继续找下一个需要处理的位置
* dfs(table,x+(y+1)/9,(y+1)%9);
* }
* }
* */
public static void main(String[] args){
Scanner in = new Scanner(System.in);
char[][] table = new char[9][];
for(int i = 0; i < 9; i++){
table[i] = in.nextLine().toCharArray();
}
dfs(table,0,0);
}
public static void dfs(char[][]table, int x, int y){
if(x==9){
print(table);
System.exit(0);
}
if(table[x][y] == '0'){
for(int i = 1; i < 10 ;i++){
if(check(table, x, y, i)){
//f = false;
table[x][y] = (char) ('0' + i);
dfs(table, x+(y+1)/9, (y+1)%9);
}
}
table[x][y] = '0';
}else{
dfs(table, x+(y+1)/9, (y+1)%9);
}
}
public static void print(char[][] table){
for(int i = 0; i < 9; i++){
System.out.println(new String(table[i]));
}
}
public static boolean check(char[][] table, int i, int j, int k){
//检查同行和同列
for(int l = 0; l < 9; l++){
if(table[i][l] == (char)('0' + k)) return false;
if(table[l][j] == (char)('0' + k)) return false;
}
//检查小九宫格
for(int l = (i/3)*3; l<(i/3+1)*3; l++){
for(int m = (j/3)*3; m<(j/3+1)*3; m++){
if(table[l][m] == (char)('0' + k))
return false;
}
}
return true;
}
}
题二:部分和
给定整数序列a1,a2,…,an,判断是否可以从中选出若干数,使它们的和恰好为k.
1≤n≤20
-10^8≤ai≤10^8
-10^8≤k≤10^8
样例:
输入
n=4
a={1,2,4,7}
k=13
输出:
Yes (13 = 2 + 4 + 7)
import java.util.ArrayList;
import java.util.Scanner;
public class lianXI {
private static int kk;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++){
a[i] = in.nextInt();
}
int k = in.nextInt();
kk = k;
dfs(a, k, 0, new ArrayList<Integer>());
}
public static void dfs(int[]a, int k, int cur, ArrayList<Integer>ints){
if(k == 0){
System.out.print("Yes (" + kk + " = ");
int size = ints.size();
for(int i = 0; i < size; i++){
System.out.print(ints.get(i) + (i == size-1 ? "" : " + "));
}
System.out.print(")");
System.exit(0);
}
if(k < 0 || cur == a.length) return;
dfs(a, k, cur+1, ints); //不要cur这个元素
ints.add(a[cur]);
int index = ints.size() - 1;
dfs(a, k-a[cur], cur+1, ints);
ints.remove(index); //回溯
}
}
题三:水洼数目
有一个大小为 NM 的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出
园子里总共有多少水洼?(八连通指的是下图中相对 W 的的部分)
***
*W*
***
限制条件
N, M ≤ 100
样例:
输入
N=10, M=12
园子如下图('W’表示积水, '.'表示没有积水)
W… …WW.
.WWW…WWW
…WW…WW.
… …WW.
… …W…
…W…W…
.W.W…WW.
W.W.W…W.
.W.W…W.
…W… …W.
输出
3
import java.util.ArrayList;
import java.util.Scanner;
public class lianXI {
private static int n;
private static int m;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
char[][] a = new char[n][];
for(int i = 0; i<n; i++){
a[i] = in.next().toCharArray();
}
int cnt = 0;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
if(a[i][j] == 'w'){
dfs(a,i,j); //清除一个水洼
cnt++;
}
}
}
System.out.println(cnt);
}
public static void dfs(char[][] a, int i, int j){
a[i][j] = '.';
for(int k = -1; k<2; k++){
for(int l = -1; l<2; l++){
if(k==0 && l==0) continue;
if(i+k>=0 && i+k<=n-1 && j+l>=0 && j+l<=m-1){
if(a[i+k][j+l] == 'w'){
dfs(a, i+k, j+l);
}
}
}
}
}
}
题四 : n皇后问题
import java.util.Scanner;
public class lianXI {
static int n;
static int cnt;
static int[] rec;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
n = in.nextInt();
rec = new int[n];
dfs(0);
System.out.println(cnt);
}
public static void dfs(int row){
if(row == n){
cnt++;
return;
}
//依次尝试在某列上放入一个皇后
for(int col = 0; col < n; col++){
boolean ok = true;
//检验这个皇后是否和之前已经放置的皇后有冲突
for(int i = 0; i < row; i++){
if(rec[i] == col || i + rec[i] == row + col || rec[i] - i == col - row){
ok = false;
break;
}
}
if(ok){
rec[row] = col; //标记
dfs(row + 1); //继续找下一行
//rec[row]=0; // 恢复原状:这种解法这里是否恢复状态都行。
}
}
}
}
题五:素数环
输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,
输出时从整数1开始,逆时针排列。同一个环应恰好输出一次。
n<=16
如输入:6
输出:
1 4 3 2 5 6
1 6 5 2 3 4
import java.util.Scanner;
public class lianXI {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] r = new int[n];
r[0] = 1;
dfs(n, r, 1);
}
public static void dfs(int n, int[] r, int cur){
if(cur == n && isP(r[0] + r[n-1])){ //填到末尾了,还有首尾相加为素数
print(r);
return;
}
for(int i = 2; i <= n; i++){ //尝试用每个数字填到cur这个位置
if(check(r, i, cur)){ //r中没有i这个数,且和上一个数之和为素数
r[cur] = i; //试着将i放在cur位置,往前走一步
dfs(n, r, cur+1);
r[cur] = 0; //回溯
}
}
}
public static boolean check(int[] r, int i, int cur){
for(int e : r){
if(e == i || !isP(r[cur-1] + i))
return false;
}
return true;
}
public static void print(int[] r){
for(int i = 0; i < r.length; i++){
System.out.print(r[i] + (i==r.length-1 ? "" : " "));
}
System.out.println();
}
public static boolean isP(int n){
if(n<2)
return false;
for(int i = 2; i*i<=n; i++){
if(n % i == 0)
return false;
}
return true;
}
}
题六:困难的串
问题描述:如果一个字符串包含两个相邻的重复子串,则称它为容易的串,其他串称为困难的串,
如:BB,ABCDACABCAB,ABCDABCD都是容易的,A,AB,ABA,D,DC,ABDAB,CBABCBA都是困难的。
输入正整数n,L,输出由前L个字符(大写英文字母)组成的,字典序第n小的困难的串。
例如,当L=3时,前7个困难的串分别为:
A,AB,ABA,ABAC,ABACA,ABACAB,ABACABA
n指定为4的话,输出ABAC
import java.util.Scanner;
public class lianXI {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int l = in.nextInt();
dfs(l, n, "");
}
static int count;
public static void dfs(int l, int n, String prefix){
//尝试在prefix后追加第一个字符
for(char i = 'A'; i < 'A' + l; i++){
if(isHard(prefix, i)){ //是困难的串,就组合起来输出
String s = prefix + i;
System.out.println(s);
count++; //计数
if(count == n)
System.exit(0);
dfs(l, n, s);
}
}
}
private static boolean isHard(String prefix, char i) {
int count = 0; //截取的宽度
for(int j = prefix.length()-1; j >= 0; j -= 2){
final String s1 = prefix.substring(j, j + count + 1);
final String s2 = prefix.substring(j + count + 1) + i;
if(s1.equals(s2))
return false;
count++;
}
return true;
}
}