`
yhz61010
  • 浏览: 558067 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:12176
社区版块
存档分类
最新评论

(Original) Nine Palace - Java Implementation - (原创) Java 九宫图 实现

    博客分类:
  • Java
阅读更多
    Some days ago, my girlfriend wanted me to write a program about Nine Palace in C#. Although I was extremely busy during that time, I still wrote for her in C# and Java(I like Java too). Because I can't stand her lovely begging.

    OK, let's get down to business.
    There're two method in this class:
    1: Type a dimension, one of the solutions will be displayed.
    2: Give your solution, algorithm will tell you whether your solution is correct.

    Here is the Java source code. You can copy it to your Java IDE and run it directly.

    I'll appreciate your reply with your better algorithm.

/*
 * Michael Leo
 * 2009/05/11/1
 */
package com.NinePalacePlans;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class NinePalacePlans
{
	public static void p()
	{
		System.out.println();
	}

	public static void p1(Object o)
	{
		System.out.print(o);
	}

	public static void p(Object o)
	{
		System.out.println(o);
	}

	public NinePalacePlans()
	{
		dimension = 3;
	}

	public NinePalacePlans(int dimension)
	{
		this.dimension = dimension;
	}

	private int dimension;

	private int[][] area;

	private int total;

	public boolean manualNinePalacePlans(int[][] paraArea)
	{
		int dimension = paraArea.length;
		int centerNum = (dimension * dimension + 1) / 2;
		// Calculate the each line sum
		int total = centerNum * dimension;
		int tempSum = 0;
		for (int i = 0; i < dimension; i++)
		{
			// Check horizontal
			tempSum = 0;
			for (int j = 0; j < dimension; j++)
			{
				tempSum += paraArea[i][j];
			}
			if (tempSum != total)
			{
				return false;
			}
			// Check vertical
			tempSum = 0;
			for (int j = 0; j < dimension; j++)
			{
				tempSum += paraArea[j][i];
			}
			if (tempSum != total)
			{
				return false;
			}
		}
		// Check diagonal
		int tSumLeftDiagonal = 0;
		int tSumRigthDiagonal = 0;
		for (int i = 0; i < dimension; i++)
		{
			tSumLeftDiagonal += paraArea[i][i];
			tSumRigthDiagonal += paraArea[(dimension - 1) - i][(dimension - 1)
					- i];
		}
		if (tSumLeftDiagonal != total || tSumRigthDiagonal != total)
		{
			return false;
		}
		// All checked
		return true;
	}

	public void createNinePalacePlans()
	{
		area = new int[dimension][dimension];
		// Get the center number
		int centerNum = (dimension * dimension + 1) / 2;
		// Calculate the each line sum
		total = centerNum * dimension;
		// Set first number 1 position
		int preNumx = (dimension + 1) / 2 - 1;
		int preNumy = 0;
		area[preNumy][preNumx] = 1;
		// Set loop times
		int loop = dimension * dimension;
		int nextx = preNumx;
		int nexty = preNumy;
		for (int i = 1; i < loop; i++)
		{
			// Calculate next number position
			nextx += 1;
			nexty -= 1;
			// nexty position is out-of-range
			if (nexty < 0)
			{
				nexty = dimension - 1;
			}
			// nextx position is out-of-range
			if (nextx >= dimension)
			{
				nextx = 0;
			}
			// nextx, nexty position has already had a number
			if (area[nexty][nextx] != 0)
			{
				nextx = preNumx;
				nexty = preNumy + 1;
			}
			// Fill the number
			area[nexty][nextx] = i + 1;

			// Keep the previous position
			preNumx = nextx;
			preNumy = nexty;
		}
	}

	public void printNinePalace()
	{
		for (int i = 0; i < dimension; i++)
		{
			for (int j = 0; j < dimension; j++)
			{
				p1(area[i][j] + "\t");
			}
			p();
		}
	}

	public int getTotal()
	{
		return total;
	}

	public void setTotal(int total)
	{
		this.total = total;
	}

	public int getDimension()
	{
		return dimension;
	}

	public void setDimension(int dimension)
	{
		this.dimension = dimension;
	}

	public static void main(String[] args)
	{
		NinePalacePlans nine = new NinePalacePlans();
		p1("Please input the dimension(odd number): ");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String inStr;
		int num = 0;
		try
		{
			inStr = in.readLine();
			num = Integer.valueOf(inStr);
			// Only odd number is available
			while (num % 2 == 0)
			{
				p("Dimension must be odd number.");
				p1("Please input the dimension(odd number): ");
				inStr = in.readLine();
				num = Integer.valueOf(inStr);
			}
			nine.setDimension(num);
		}
		catch (Exception e)
		{
			nine.setDimension(3);
			p("Entered dimension error. Using default dimesion: 3.");
		}

		nine.createNinePalacePlans();
		nine.printNinePalace();
		p();
		p("The total sum of each line: " + nine.getTotal());

		p();
		p("=== Manual NinePalacePlans ===");
		int[][] paraArea = { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } };
		if (nine.manualNinePalacePlans(paraArea))
		{
			p("All checked.");
		}
		else
		{
			p("Not correct.");
		}
	}
}
1
1
分享到:
评论

相关推荐

    original-spark-examples-2.4.3.jar.zip

    这个模块允许Spark作业直接读写HBase表,通过HBaseRDD(Resilient Distributed Dataset for HBase)实现。然而,由于版本差异,直接使用预编译的Spark库可能无法与HBase2无缝对接。 2. **问题的出现** 当尝试使用...

    original-mqmeter-2.1.1.jar

    jmeter插件,用于进行IBM MQ测试。 部署插件之后,可进行简单配置,即可发送消息到mq通道中 使用方法参见 https://testerhome.com/notes/2193

    WSO2-JAVA开发集成

    ### WSO2-JAVA开发集成知识点详解 #### 一、WSO2-ESB与JAVA集成概述 在现代企业环境中,不同系统之间的交互变得日益复杂。为了实现高效的数据交换和服务集成,WSO2 Enterprise Service Bus (ESB) 成为一个重要的...

    5-Java数组与方法

    在Java编程语言中,数组是一种特殊的数据结构,用于存储同类型元素的集合。它们提供了一种方式来组织和管理大量数据,使得我们可以高效地访问和操作这些数据。本章将深入探讨数组的基本概念、定义以及如何在实际编程...

    original-mybatis-3.4.1-SNAPSHOT.jar

    1.资源名:original-mybatis-3.4.1-SNAPSHOT.jar 2.获取方式:2016.06.17在git上下载的mybatis的源码,通过maven编译生成。 3.备注:该jar包相关的解释没有找到,暂时先上传上去,要用mybatis的jar包...

    04-2017-Interoperability-ORIGINAL-White-Paper-Final-Musa-and-Shashank-1.pdf

    04-2017-Interoperability-ORIGINAL-White-Paper-Final-Musa-and-Shashank-1.pdf

    采用java实现图片水印

    本篇文章将详细探讨如何使用Java实现图片水印的添加,以及相关的知识点。 首先,我们需要了解Java中的图像处理库。Java标准库提供了`java.awt`和`javax.imageio`包,它们包含了处理图像的基本功能。`java.awt`包中...

    使用背景框为图像添加遮罩效果 - Java - 下载.zip

    本教程将详细解释如何通过Java实现这一功能,特别是利用Java的AWT和Swing库。 首先,我们需要了解什么是遮罩。在图像处理中,遮罩通常是一个二值图像,其中黑色代表透明,白色代表不透明。我们可以将遮罩应用于原始...

    Android代码-java-various-examples

    java-various-examples (applicable only to projects prefixed with [CI]) Development Standards This project contains the following examples of using Java and Java-based technologies: Spring Boot 2 ...

    java实现CRC16校验功能

    在Java中实现CRC16校验功能可以帮助开发者确保数据的完整性和一致性,尤其在文件传输、网络通信或者存储系统中。CRC16的基本原理是通过一个固定长度的校验和来验证数据是否在传输或处理过程中发生了错误。 CRC16的...

    java实现图片的缩小和放大

    根据提供的文件信息,本文将详细解释Java中如何实现图片的缩小与放大功能。这涉及到对图像处理的理解,以及具体的Java编程技巧。 ### 图片放大的实现 #### 方法一:通过`BufferedImage`类进行放大 在Java中,可以...

    java语言实现的修改图片大小

    在Java编程环境中,修改图片大小是一项常见的任务,尤其在处理图像数据或开发图形用户界面时。这个主题主要涉及Java的图像处理技术,包括使用Java AWT(Abstract Window Toolkit)和Swing库。以下是对这个话题的详细...

    Java编写的图片上加文字Demo

    这个"Java编写的图片上加文字Demo"项目利用Eclipse 4.2.1作为开发环境,实现了在本地图片上动态添加文字的功能,允许用户自定义字体样式,从而制作出具有个性化的水印。下面我们将深入探讨这一技术实现的关键知识点...

    original-jupiter-all-1.2.10.jar

    java运行依赖jar包

    Android代码-java-dotenv

    java-dotenv A Java port of the Ruby dotenv project (which loads environment variables from a .env file). java-dotenv also offers a Kotlin DSL. From the original Ruby project: &gt;Storing ...

    java图片裁剪

    本文将深入探讨Java中的图片裁剪技术,包括基本概念、常用库以及具体的实现步骤。 1. **基本概念**: - **坐标系统**:在Java图像处理中,图片被看作是一个二维平面,每个像素都有一个x和y坐标。 - **裁剪区域**...

    java 实现图片90.180.270.360°旋转

    ### Java实现图片90°、180°、270°、360°旋转的知识点 在本文中,我们将深入探讨如何使用Java来实现图片的旋转功能,特别是90°、180°、270°以及360°的旋转。通过解析给定的文件内容,我们可以了解到该功能...

    Java实现图片等比例缩略图

    本篇文章将深入探讨如何使用Java实现图片的等比例缩略图,并提供相关代码示例。 首先,我们需要了解等比例缩略图的概念。等比例缩略图是指在保持原图宽高比的基础上,根据指定的最大尺寸进行缩小或放大。这样可以...

    original-dubbo-2.8.4.jar

    java运行依赖jar包

    java中处理图片水印

    在Java编程语言中,处理图片水印涉及到图像处理和图形绘制技术。`Graphics2D`是Java 2D API的一部分,它提供了丰富的功能,可以用来在图像上添加文本、图像等元素,实现水印效果。下面我们将深入探讨如何使用`...

Global site tag (gtag.js) - Google Analytics