4 Star 21 Fork 3

TangGuoNiuBi/AlogrithmVisualizer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

算法可视化

  • 本代码参考慕课网的liuyubobobo老师的一门课《7个经典应用诠释Java算法精髓》,并且基于课程中的参考代码做了一定的完善和个人定制。
  • 以Java语言主讲,通过7款经典好玩游戏,真正将算法用于实际开发。

可视化用到的语言和技术

Java Swing

可视化内容

1. 概率模拟算法

使用蒙特卡洛算法模拟计算PI

2. 排序算法

选择排序可视化

插入排序可视化

归并排序可视化

3. 迷宫自动求解迷宫

  • 注:迷宫自动求解游戏添加了键盘点击事件按对应的键盘按键将触发相应的操作

    • 按数字1:深度优先遍历算法生成迷宫
    • 按数字2:深度优先遍历非递归算法生成迷宫
    • 按数字3:广度优先遍历算法生成迷宫
    • 按数字4:随机队列遍历算法生成迷宫
    • 按数字5:增强随机队列算法生成迷宫
    • 按数字7:深度优先遍历递归算法求解迷宫
    • 按数字8:深度优先遍历非递归算法求解迷宫
    • 按数字9:广度优先遍历算法求解迷宫
    • 按数字0:清屏,游戏回到迷雾笼罩的状态
  • 从文件中读取迷宫信息,并且进行可视化;

深度优先遍历递归写法+回溯法求解迷宫可视化;

深度优先遍历的非递归写法求解迷宫可视化;

广度优先遍历算法求解迷宫可视化;

4. 生成迷宫

  • 为了简单起见,在M行N列的迷宫中,先规定迷宫的入口是(1, 0),出口是(M-2, N-1)

迷宫生成前的准备工作

下面是61x91规格的迷宫,奇数行和奇数列相交的位置是通路(没有墙),生成迷宫前需要先产生一个这样的图。然后只需要深度优先遍历所有在奇数行和奇数列相交的路,然后打破两个路之前的墙即可产生一个迷宫。

image-20210326235007632

深度优先遍历递归算法生成一个迷宫

深度优先遍历非递归算法生成一个迷宫

广度优先遍历非递归算法生成一个迷宫

创建一个随机队列让迷宫的生成过程更随机

给未生成部分添加迷雾,让生成迷宫的过程更具备观赏性和神秘感

随机队列的代码请参考RandomQueue.java

随机迷宫的生成过程如下图所示:

随机迷宫的求解过程如下图所示:

增强生成迷宫的随机性

经过多次迷宫的生成和求解过程来看,迷宫的解路径大致趋势是从左上到右下的一条不是很曲折的曲线,随机性还不够强,所以优化了随机队列的随机存取元素的代码,参考EnhancedRandomQueue.java

增强随机性后迷宫的生成过程如下图所示:

增强随机性后迷宫的求解过程如下图所示:

5. 扫雷

扫雷游戏胜利与游戏结束

游戏结束

注:游戏结束后可按回车重新开局!

image-20210328180303684

胜利

image-20210328212853457

在游戏盘面中随机30枚雷

image-20210328141001452

随机算法好坏评估

  • 算法目标:将指定数量的雷随机放在游戏区域中的相同数量的格子中

  • 使用大量次数统计的方法对三种随机打乱算法,参考RandomAlgorithmAssessment.java

    • arr:待随机打算顺序的数组;mineNumber:雷的个数(小于数组长度)

    • 随机算法1:将每一个格子上的值和随机的格子上的值进行交换

      for (int i = 0; i < arr.length; i++) {
          int j = (int) (Math.random() * arr.length);
          swap(arr, i, j);
      }
      
    • 随机算法2:将每一个雷所在的格子上的值和随机的格子上的值进行交换

      for (int i = 0; i < mineNumber; i++) {
          int j = (int) (Math.random() * arr.length);
          swap(arr, i, j);
      }
      
    • knuth算法:从[i, n)区间中随机获取元素,然后和第i个元素交换

      for (int i = 0; i < arr.length; i++) {
          int j = (int) (Math.random() * (arr.length - i) + i);
          swap(arr, i, j);
      }
      
    • knuth算法的另一种写法,从[0, i+1)区间中随机获取元素,然后和第i个元素交换

      for (int i = arr.length - 1; i >= 0; i--) {
          int j = (int) (Math.random() * (i + 1));
          swap(arr, i, j);
      }
      
    • 各个算法在N=10000000,n=10,m=5的情况下(N为统计次数,n为数组长度,m为数组中的雷的个数),数组中的各个位置上出现雷的频率如下图所示:

      image-20210328160137277

      image-20210328155957042

6. 移动箱子游戏给出求解步骤

游戏介绍:

这是一个三消类推箱子游戏,跟传统的推箱子不一样,这个推箱子需要你在规定的步数内移动箱子,促使游戏面板中三个相同的箱子连成一行或者一列,可以达成消除的条件,另外游戏设置了重力系统,高出的箱子如果往左或者往右或者它下面的箱子被消除了,都会促使这个箱子向下坠落,坠落以后,又会检测是否出现新的可消除的连续的三个相连的箱子,游戏封面如下

image-20210329180005073

这是游戏中的某一个关卡:要求在移动一步的条件下消除所有的箱子。

image-20210329180302902

游戏物体符号化

现将上面关卡中的箱子的分布转换为一个二维字符数组,连同操作步数一起保存到文件中。

1
.....
..A..
..B..
..A..
..B..
.BAB.
.BAB.
.ABA.

然后通过代码来读取操作步数和二维字符数组,为了对齐每列的字符,空的位置用 点 表示,不同种类的箱子用不同的字母表示。

渲染符号化的游戏物体

用代码读取后渲染到页面上如下图所示,由于没有原游戏的箱子素材,我就在网上找了一些图片进行了替换。

image-20210329182751415

查看求解步骤

为了使得求解后的答案更直观,在每一个箱子中间写了一个坐标,代表目前这个箱子所在的行和列。

image-20210329181210898

根据控制台打印出的结果和图上的坐标,去游戏里面进行相应操作即可通过此关。

交换 (3, 2) 和 (4, 2)
游戏有解!

7. 绘制分形图

递归绘制同心圆

image-20210329193958960

在这里插入图片描述

递归绘制vicsek(矩形)分形图

可以按键盘上的0到6进行不同递归次数的分形

初始化默认递归次数为0,呈现出整个区域都是一个颜色

分别按下键盘1-6又可以绘制出如下分形图

递归绘制SierpinskiCarpet(矩形)分形图

递归绘制SierpinskiTriangle(三角形)分形图

递归绘制KochSnowflake(雪花)分形图

递归绘制二叉树分形图

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

7个有趣的算法可视化的 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CandyWall/alogrithm-visualizer.git
git@gitee.com:CandyWall/alogrithm-visualizer.git
CandyWall
alogrithm-visualizer
AlogrithmVisualizer
master

搜索帮助