资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java九宫格拼图代码 九宫格自动拼图算法

求破解九九宫格JAVA代码

JAVA不熟,不过算法都差不多。

成都创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站设计、程序开发、网站优化、微网站、小程序制作等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站建设、成都网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。

等会给你个C/C++的。

我用C++做出来了.

基本思路说下:

先定义类

class JiuGong

{

public:

int table[9][9];//这个是九宫里的数学 不知道的用0代替

bool mark[9][9];//这个表示九宫状态,true表示已知的,可以不计算的

int x;

int y;//x,y表示当前正在试验的点

JiuGong *pre;//这是后面堆栈用的

bool check();//这函数进行九宫检查,包括行/列/小九宫的查错

bool finish();//这函数检查是否已经完成

bool find();//这函数用来找到下一个未知点(试验点)位置

}

再定义一个类 这是个堆栈

class JiuGongStack()

{

public:

JiuGong *top;

bool push(JiuGong *p);//压

bool pop(JiuGong *p);//弹

}

推算过程:

先声明一个JiuGong结构,导入初始值.

用成员函数find()找到第一个未知点.

以下循环:

{

在试验点数字加1

用成员函数finish()检查是否结束

Y- 成功 返回

N- 向下

检查数字9

Y-POP -POP失败 -题目错误 返回

N-向下

用成员函数check()查错

无错-PUSH

错-继续循环

}

完毕.

java 写一个九宫格的代码!不用很复杂,实现了基本的功能就行! 谢谢大家!我自己写的都晕了

/*

九宫格算法代码 C++版

*/

#include stdio.h

int map[9][9] = {0, 0, 3, 8, 1, 0, 0, 0, 9,

5, 0, 0, 4, 0, 0, 0, 8, 0,

0, 6, 0, 9, 0, 0, 1, 0, 0,

0, 0, 8, 0, 3, 0, 0, 0, 6,

0, 0, 0, 0, 0, 0, 0, 0, 0,

9, 0, 0, 6, 0, 0, 5, 0, 0,

0, 0, 6, 0, 0, 9, 0, 1, 0,

0, 1, 0, 0, 0, 5, 0, 0, 4,

2, 0, 0, 0, 4, 8, 7, 0, 0};

void display()

{

int i;

int j;

for (i = 0; i 9; i++)

{

for (j = 0; j 9; j++)

{

if(map[i][j])

{

printf(" %d ", map[i][j]);

}

else

{

printf(" ");

}

}

printf("\n");

}

}

int check(int x, int y, int *mark) //check函数为该格子可以选择的数字

{

int i;

int j;

int gi;

int gj;

int count = 0;

for (i = 1; i = 9; i++)

{

mark[i] = 0; //初始化

}

for (i = 0; i 9; i++)

{

mark[map[x][i]] = 1; //表示该行该数字已存在

mark[map[i][y]] = 1; //表示该列该数字已存在

}

gi = x / 3 * 3; // 九宫格把map分割成9个小块,如果该格子所在行列为(1,4),那么它所在子格子起始为(0,3)到(2,5)的九个小格子

gj = y / 3 * 3;

for (i = 0; i 3; i++)

{

for (j = 0; j 3; j++)

{

mark[map[gi + i][gj + j]] = 1; //此循环为表示所在子格子的九个小格子中已存在该数字

}

}

for (i = 1; i = 9; i++)

{

if(0 == mark[i]) //如果该数字不存在则++

{

count++;

}

}

return count;

}

void crack() //??

{

int i;

int j;

int mark[10]; //标志数字是否已存在

int min = 10; //记录最少可能数字数

int ci = -1; //ci,cj用来记录最少可能数字的格子所在行列

int cj;

for (i = 0; i 9; i++)

{

for (j = 0; j 9; j++)

{

if (map[i][j])

{

continue; //如果该格子已有数字则跳到下一个格子

}

int c = check(i, j, mark); //c为该格子可以选择的数字

if (0 == c)

{

return;

}

if (c min)

{

ci = i;

cj = j;

min = c;

}

}

}

if (-1 == ci)

{

printf("The answer is:\n");

display();

return;

}

check(ci, cj, mark);//这个语句的作用是把mark这个数组设置成最小可能格子缺少的数字,若缺少则为0

for (i = 1; i = 9; i++)

{

if (mark[i] == 0)

{

map[ci][cj] = i; //先填入该数字

crack(); //进行判断

}

map[ci][cj] = 0; //若这个数字不可以得到解则判断下一可能数字(这里类似0-1背包问题)

}

}

int main()

{

printf("The game is:\n");

display();

crack();

return 0;

}

java编程九宫格问题

要求:根据输入的数字n,如:3,5,7...以矩阵显示n行n列数,这些数由1~n*n构成,要求矩阵的每行每列及对角线上n个数之和相等 预备知识: 在距阵中,1在第一行正中,随后的数字应放到上一个数字的右上方方格中,如果向上不行,就放到该列的最下方格子;如果向右不行,就放到该行的最左边;如果都不行,就放到上一个数字的正下方;如果目标格子中已经有数字,也放到上一个数字的正下方 思路: 1) 使用2维数组预备存储1~n*n这些数字 2) 1是放到第一行正中的,所以其索引号是:[0][(n-1)/2] 3) 随后的数字,其索引号原则如下 1 num的行索引为 num-1 的 (行索引-1) , num的列索引为 num-1 的 (列索引+1) 2如果发现num的行,列索引都越位(-1或n),则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 3如果发现num的行,列索引指向的位置已经有数字,则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 4如果发现num的行越位(-1),则 num的行索引为n-1 5如果发现num的列越位(n),则 num的列索引为0 import java.util.Scanner; public class JiuGong { public static void main(String[] args) { Scanner s = new Scanner(System.in); int x = s.nextInt(); //输入长度 int h = 0; //行 //在距阵中,1在第一行正中 int l = x / 2; //列 int[][] a = new int[x][x]; for (int i = 1; i = x * x; i++) { a[h][l] = i; //运行提示溢出 //随后的数字应放到上一个数字的右上方方格中 h--; l++; //3.如果都不行,就放到上一个数字的正下方 if (h 0 l = x) { //先返回上一个数字 h++; l--; //再下移一行 h++; } //1.如果向上不行,就放到该列的最下方格子 else if (h 0) { h = x - 1; } //2.如果向右不行,就放到该行的最左边 else if (l = x) { l = 0; } //4.如果目标格子中已经有数字,也放到上一个数字的正下方 else if (a[h][l] 0) { //先返回上一个数字 h++; l--; //再下移一行 h++; } } //打印九宫格 for (int j = 0; j x; j++) { for (int k = 0; k x; k++) { System.out.print(a[j][k] + " "); } //换行 System.out.println(); } } }

采纳哦

用java做个九宫格

定义了一个package名叫aloha

把下面的代码粘贴了,编译运行就可以了

不用谢我了!

/*

* NineGrid.java

* @author libai8723@qq.com

* Created on 2011-12-20, 13:21:36

*/

package aloha;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JOptionPane;

/**

*

* @author libai

*/

public class NineGrid extends javax.swing.JFrame implements ActionListener{

/** Creates new form NineGrid */

public NineGrid() {

initComponents();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

this.setSize(400, 400);

this.setTitle("Nine Grid");

this.setLocation((int)(d.getWidth() - 400)/2, (int)(d.getHeight() - 400)/2);

for(int i = 0;i 15;i++)

{

this.arr[i] = i+1;

}

this.arr[15] = -1;

this.arr[11] = -1;

this.arr[15] = 12;

for(int i = 0;i 15;i++)

{

int idx =(int) (Math.random() * 15);

int tmp = this.arr[7];

this.arr[7] = this.arr[idx];

this.arr[idx] = tmp;

}

for(int i = 0;i 4;i++)

{

for(int j = 0;j 4;j++)

{

if(this.arr[i * 4 + j] != -1)

{

this.Buttons[i][j] = new JButton("" + this.arr[i * 4 + j]);

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

}

else

{

this.Buttons[i][j] = new JButton("");

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

this.Buttons[i][j].setEnabled(false);

}

}

}

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"

private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

getContentPane().setLayout(new java.awt.GridLayout(4, 4));

pack();

}// /editor-fold

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NineGrid().setVisible(true);

}

});

}

private JButton[][] Buttons = new JButton[4][4];

private int[] arr = new int[16];

private boolean isSucceed()

{

boolean flag = true;

for(int i = 0;i 4;i++)

{

for(int j = 0;j 4;j++)

{

if(!this.Buttons[i][j].getText().equals(""))

if(!this.Buttons[i][j].getText().equals(""+(i * 4 + j + 1)))

{

return false;

}

}

}

return true;

}

public void actionPerformed(ActionEvent e)

{

int i = 0,j = 0;

boolean in = false;

for(i = 0;i 4;i++)

{

for(j = 0;j 4;j++)

{

if(e.getSource() == this.Buttons[i][j])

{

in = true;

break;

}

}

if(in)

break;

}

if((i = 0 (j - 1) = 0)(!this.Buttons[i][j - 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j - 1].getText());

this.Buttons[i][j - 1].setText(tmp);

this.Buttons[i][j - 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i = 0 (j + 1) 4)(!this.Buttons[i][j + 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j + 1].getText());

this.Buttons[i][j + 1].setText(tmp);

this.Buttons[i][j + 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i - 1 = 0 j = 0)(!this.Buttons[i - 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i - 1][j].getText());

this.Buttons[i - 1][j].setText(tmp);

this.Buttons[i - 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i + 1 4 j = 0)(!this.Buttons[i + 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i + 1][j].getText());

this.Buttons[i + 1][j].setText(tmp);

this.Buttons[i + 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

}

// Variables declaration - do not modify

// End of variables declaration

}

新手紧急求助,编一个九宫格游戏的java程序

import java.util.HashMap;

/**

* 知识点1 静态方法的调用

* 知识点2 带参数的静态方法调用

* 知识点3 for循环

* 知识点4 HashMap的遍历

* 知识点5 格式化输出

* @author Administrator

* 补充要学习的知识点

* 1.Character 和 char的区别

* 2.HashMap对象中的常用方法

*/

public class Helper {

/**

* 主方法

* 获取下面两个静态方法的值按格式打印

* @param args

*/

public static void main(String[] args) {

//调用最下面的createCharArray()方法获得的结果负值给Character数组

Character[] charArray = createCharArray();

//调用下面的countLetters(Character[] charArray)带参数方法获得结果负值给HashMap对象

HashMaplt;Character, Integer counts = countLetters(charArray);

//匹配住所有的HashMap对象

for (Character c : counts.keySet()) {

//循环格式化打印显示

System.out.printf(quot;(%c,%d)quot;, c, counts.get(c));

}

}

/**

* 验证所有含元素次数的静态方法

* @param charArray

* @return HashMaplt;Character, Integer result

*/

private static HashMaplt;Character, Integer countLetters(

Character[] charArray) {

//声明一个HashMap对象

HashMaplt;Character, Integer result = new HashMaplt;Character, Integer();

//匹配住传进来的参数charArray中所有的元素

for (Character c : charArray) {

//非空验证

if (result.get(c) == null)

//空的话把匹配住的元素放进HashMap对象中 c为键 负值为1

result.put(c, 1);

else

//已经存在把获取的值取出并且重新复制 替代掉原由的

result.put(c, result.get(c) + 1);

}

//返回给调用者

return result;

}

/**

* 随即生成100个字母的静态方法

* @return Character[]chars

*/

private static Character[] createCharArray() {

//声明一个有100个元素的Character数组

Character[] chars = new Character[100];

//循环100次

for (int i = 0; i lt; chars.length; i++) {

//随即获取一个字母负值给char对象c

char c = (char) (#39;a#39; + Math.random() * (#39;z#39; - #39;a#39; + 1));

//把随即获得的值放入Character数组

chars[i] = c;

}

//返回给调用者

return chars;

}

}

希望对你的学习有所帮助


网站名称:java九宫格拼图代码 九宫格自动拼图算法
当前路径:http://cdkjz.cn/article/doopiij.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220