初学编程时在 csdn 上写过一个陈景润 15 子问题的项目,https://blog.csdn.net/weixin_41628344/article/details/79171846

当时的主要精力都放在学习编程上,并未对陈景润的算法进行研究,今天故地重游,重新整理一下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace FifteenButtons
{class Program{static void Main(string[] args){(new FrmMain()).ShowDialog();}}public class ChessMan : Button{public int RowIndex { get; set; }public int ColumnIndex { get; set; }public int Index { get; set; }}class FrmMain : Form{private List<ChessMan> chessManList;private int N = 4;       //边长private int X0 = 20;    //横向起始坐标private int Y0 = 20;    //竖向起始坐标private int Step = 50;   //按钮间距private int CmWidth = 45;    //按钮宽度private int MoveCount = 200; //移动次数private ChessMan hiddenCm;public FrmMain(){Start();}#region Event//开始private void Start(){if (chessManList == null){InitChessMan();}//MoveCm();
            MoveCm2();}//点击按钮private void ChessMan_Click(object sender, EventArgs e){ChessMan cm = sender as ChessMan;int differ = Math.Abs(cm.Index - hiddenCm.Index);if (differ == 1 || differ == N){ExChange(ref hiddenCm, cm);}if (GameOver()){MessageBox.Show("游戏结束");Start();}}#endregion#region Method//初始化所有按钮private void InitChessMan(){chessManList = new List<ChessMan>();for (int i = 0; i < N * N; i++){ChessMan cm = new ChessMan();cm.Text = (i + 1).ToString();cm.RowIndex = i / N;cm.ColumnIndex = i % N;cm.Index = i;cm.Width = CmWidth;cm.Height = CmWidth;cm.Top = Y0 + cm.RowIndex * Step;cm.Left = X0 + cm.ColumnIndex * Step;if (i == (N * N - 1)){cm.Visible = false;this.hiddenCm = cm;}else{cm.Visible = true;}chessManList.Add(cm);cm.Click += new EventHandler(ChessMan_Click);this.Controls.Add(cm);}}//打乱所有按钮private void MoveCm(){Random rnd = new Random();for (int i = 0; i < MoveCount; i++){int direction = rnd.Next(4);switch (direction){//上case 0:{if (this.hiddenCm.RowIndex == 0){break;}int index = this.hiddenCm.ColumnIndex + N * (this.hiddenCm.RowIndex - 1);ChessMan cm = chessManList[index];ExChange(ref this.hiddenCm, cm);break;}//下case 1:{if (this.hiddenCm.RowIndex == (N - 1)){break;}int index = this.hiddenCm.ColumnIndex + N * (this.hiddenCm.RowIndex + 1);ChessMan cm = chessManList[index];ExChange(ref this.hiddenCm, cm);break;}//左case 2:{if (this.hiddenCm.ColumnIndex == 0){break;}int index = this.hiddenCm.ColumnIndex - 1 + N * (this.hiddenCm.RowIndex);ChessMan cm = chessManList[index];ExChange(ref this.hiddenCm, cm);break;}//右case 3:{if (this.hiddenCm.ColumnIndex == (N - 1)){break;}int index = this.hiddenCm.ColumnIndex + 1 + N * (this.hiddenCm.RowIndex);ChessMan cm = chessManList[index];ExChange(ref this.hiddenCm, cm);break;}}}}//打乱所有按钮       //陈景润方法//http://www.doc88.com/p-7092015263762.htmlprivate void MoveCm2(){Random rnd = new Random();List<int> list = new List<int>();for (int i = 0; i < N * N; i++){list.Add(i);}List<int> list2 = new List<int>();while (list.Count > 0){int i = list[rnd.Next(list.Count)];list.Remove(i);list2.Add(i);}//计算倒置数int count = 0;for (int i = 0; i < list2.Count; i++){if (list2[i] != list2.Count - 1){for (int j = i + 1; j < list2.Count; j++){if (list2[i] > list2[j]){count++;}}}}//空白所在行号int rowIndex = 0;for (int i = 0; i < list2.Count; i++){if (list2[i] == N * N - 1){rowIndex = i / N;break;}}bool list2IsOdd = (count % 2 == 0) ? false : true;bool rowIsOdd = (rowIndex % 2 == 0) ? true : false;         //0行为奇数,1行为偶数if (N % 2 == 0) //N为偶数:序列为偶置序列,空格必须在偶数行。序列为奇置序列,空格必须在奇数行。
            {if (!list2IsOdd && rowIsOdd) //偶置序列   空格行号为奇数
                {int temp = list2[list2.Count - 1];       //交换后两个元素,改变奇偶性list2[list2.Count - 1] = list2[list2.Count - 2];list2[list2.Count - 2] = temp;}else if (list2IsOdd && !rowIsOdd)//奇置序列   空格行号为偶数
                {int temp = list2[0];    //交换前两个元素,改变奇偶性list2[0] = list2[1];list2[1] = temp;}}else //N为奇数,序列必须为偶置序列
            {if (list2IsOdd){if (rowIsOdd){int temp = list2[N];       //交换后两个元素,改变奇偶性list2[N] = list2[N + 1];list2[N + 1] = temp;}else{int temp = list2[0];    //交换前两个元素,改变奇偶性list2[0] = list2[1];list2[1] = temp;}}}for (int i = 0; i < chessManList.Count; i++){chessManList[i].Text = (list2[i] + 1).ToString();if (list2[i] == chessManList.Count - 1){chessManList[i].Visible = false;hiddenCm = chessManList[i];}else{chessManList[i].Visible = true;}}}//交换按钮private void ExChange(ref ChessMan hiddenCm, ChessMan cm){string str = hiddenCm.Text;hiddenCm.Text = cm.Text;cm.Text = str;hiddenCm.Visible = true;cm.Visible = false;hiddenCm = cm;this.hiddenCm = hiddenCm;}//游戏结束private bool GameOver(){for (int i = 0; i < chessManList.Count; i++){if (chessManList[i].Text != (i + 1).ToString()){return false;}}return true;}#endregion}
}

转载于:https://www.cnblogs.com/aitong/p/10972588.html

c# 陈景润 15 子问题相关推荐

  1. 给陈景润之子陈由伟的一封公开信

    近日,我们读了您缅怀父亲陈景润的文章,深受感动. 袁萌是您父亲陈景润的数学.老朋友.1957年进入南京大学数学天文系学习抽象代数专业,现倡导非标准无穷小微积分下放中学. 我们想把陈景润早年(1957年 ...

  2. mysql 学习笔记15 子查询

    子查询定义: 单上子查询举例: 显示与 员工 关平 同一部门的员工, 但不包括关平 select * from staff where staff.stdepno = (select staff.st ...

  3. 通过memcached来实现对tomcat集群中Session的共享策略 .

    近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 但是实现了该集群后,发现登陆系统后,每次都会被拦截回登录页面,造成该现象的原因是Session ...

  4. mysql数据库建站教程视频_Mysql数据库零基础到精通视频教程(共6天)

    php教程 当前位置:主页 > php教程 > Mysql数据库零基础到精通视频教程(共6天) Mysql数据库零基础到精通视频教程(共6天) 教程大小:886MB   发布时间:2016 ...

  5. java 正则匹配 sql星号,18. 正则表达式:开头、结尾、任意一个字符、星号和加号匹配...

    Re: MySQL 查询与高级查询(多表.嵌套和正则表达式) (查询的能力,亦显示DBA的功力) ============================================= 数据库管理 ...

  6. 【Java】多线程相关复习—— 线程的创建、名字、运行情况以及顺序控制(join方法) 【一】...

    一.创建线程的三种方式 · 继承Thread类 · 实现Runnable接口 · 实现Callable接口 二. 线程状态 · 线程名字 getName() · 线程活动情况 isAlive() · ...

  7. 02-Unity深入浅出(二)

    一. Unity声明周期 Unity容器为我们提供了6种生命周期,便于我们根据项目需求来选择使用. (1). 瞬时.默认省略即为瞬时,无论单线程还是多线程,每次都重新创建对象.new Transien ...

  8. matlab五子棋_应用 | 五子棋游戏——没人能在我的程序里打败我

    最近看到一个新闻,8月11日第16届世界五子棋锦标赛的决赛战场上,两名中国棋手分别取得了决赛组和女子组的冠军. 毕竟是两年一届的世界最高水平的五子棋比赛,这样的成绩可以说是很优秀了. 看完后,会长也是 ...

  9. unity案例入门(二)(坦克大战)

    1. 案例简述 这个案例实现一个简单的坦克对战游戏,两个玩家在一个地图上PK. 2. 控制坦克移动 与案例一中小球的移动方式不同,坦克在横向上不能是平移,因此横向按键控制的应该是坦克旋转. publi ...

最新文章

  1. Centos7多内核情况下修改默认启动内核方法
  2. 【Qt】Qt5.12版本编译Oracle驱动教程
  3. Android开发技巧——自定义控件之自定义属性
  4. 如何改变请求的host以及referer抓取做了host以及referer限制的接口数据
  5. Qt之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)
  6. 七0二所与江南计算机研究所,江南大学:一所被低估的“211”大学,2个A+学科,丝毫不输985...
  7. 解决CentOS7本机时间与实际时间相差8小时的问题
  8. Java安全(一) : java类 | 反射
  9. c语言状态机_【C语言】有限状态机FSM
  10. ps制作20种特效文字_如何使用AE制作文字破碎动画?制作ae破碎文字特效教程分享...
  11. cbv继承view是哪个包_包你一眼就心动!两款帅爆国产轿车来袭,选谁都不错
  12. 【贪心】牛客网:把数组排成最小的数
  13. AndroidTv开发中常用的adb命令
  14. python的pyc反编译
  15. Spurious wakeup
  16. 众测、专属、渗透测试捡破烂小tips
  17. 手机成像技术简谈(测光篇)
  18. 单片机点亮LCD1602A液晶屏
  19. javafx 教程_Swing和JavaFX:使用JFXPanel
  20. 空气炸锅炸鸡腿多少度多少分钟最好(空气炸锅炸鸡腿多少度多长时间)

热门文章

  1. 平均 15189 元!2021 年 3 月程序员工资统计新出炉
  2. python学习(4)zip函数
  3. 美国高清晕渲地形图分享,每一幅都值得珍藏
  4. 数据回归方法(二)—— 多元回归
  5. 印象笔记文章转到kindle上文字显示小的解决办法
  6. java工程师 英文_java软件工程师英文简历模板
  7. 【EI快速稳定检索】第三届IEEE信息与计算机前沿技术国际学术会议(ICFTIC 2021)
  8. 士兵队列训练问题:某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的
  9. boston数据集预测房价
  10. VirtIO实现原理——PCI基础