c# 陈景润 15 子问题
初学编程时在 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 = ; //边长
private int X0 = ; //横向起始坐标
private int Y0 = ; //竖向起始坐标
private int Step = ; //按钮间距
private int CmWidth = ; //按钮宽度
private int MoveCount = ; //移动次数
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 == || differ == N)
{
ExChange(ref hiddenCm, cm);
}
if (GameOver())
{
MessageBox.Show("游戏结束");
Start();
}
}
#endregion #region Method
//初始化所有按钮
private void InitChessMan()
{
chessManList = new List<ChessMan>();
for (int i = ; i < N * N; i++)
{
ChessMan cm = new ChessMan();
cm.Text = (i + ).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 - ))
{
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 = ; i < MoveCount; i++)
{
int direction = rnd.Next();
switch (direction)
{
//上
case :
{
if (this.hiddenCm.RowIndex == )
{
break;
}
int index = this.hiddenCm.ColumnIndex + N * (this.hiddenCm.RowIndex - );
ChessMan cm = chessManList[index];
ExChange(ref this.hiddenCm, cm);
break;
}
//下
case :
{
if (this.hiddenCm.RowIndex == (N - ))
{
break;
}
int index = this.hiddenCm.ColumnIndex + N * (this.hiddenCm.RowIndex + );
ChessMan cm = chessManList[index];
ExChange(ref this.hiddenCm, cm);
break;
}
//左
case :
{
if (this.hiddenCm.ColumnIndex == )
{
break;
}
int index = this.hiddenCm.ColumnIndex - + N * (this.hiddenCm.RowIndex);
ChessMan cm = chessManList[index];
ExChange(ref this.hiddenCm, cm);
break;
}
//右
case :
{
if (this.hiddenCm.ColumnIndex == (N - ))
{
break;
}
int index = this.hiddenCm.ColumnIndex + + N * (this.hiddenCm.RowIndex);
ChessMan cm = chessManList[index];
ExChange(ref this.hiddenCm, cm);
break;
}
}
}
} //打乱所有按钮
//陈景润方法
//http://www.doc88.com/p-7092015263762.html
private void MoveCm2()
{
Random rnd = new Random();
List<int> list = new List<int>();
for (int i = ; i < N * N; i++)
{
list.Add(i);
}
List<int> list2 = new List<int>();
while (list.Count > )
{
int i = list[rnd.Next(list.Count)];
list.Remove(i);
list2.Add(i);
} //计算倒置数
int count = ;
for (int i = ; i < list2.Count; i++)
{
if (list2[i] != list2.Count - )
{
for (int j = i + ; j < list2.Count; j++)
{
if (list2[i] > list2[j])
{
count++;
}
}
}
}
//空白所在行号
int rowIndex = ;
for (int i = ; i < list2.Count; i++)
{
if (list2[i] == N * N - )
{
rowIndex = i / N;
break;
}
}
bool list2IsOdd = (count % == ) ? false : true;
bool rowIsOdd = (rowIndex % == ) ? true : false; //0行为奇数,1行为偶数
if (N % == ) //N为偶数:序列为偶置序列,空格必须在偶数行。序列为奇置序列,空格必须在奇数行。
{
if (!list2IsOdd && rowIsOdd) //偶置序列 空格行号为奇数
{
int temp = list2[list2.Count - ]; //交换后两个元素,改变奇偶性
list2[list2.Count - ] = list2[list2.Count - ];
list2[list2.Count - ] = temp;
}
else if (list2IsOdd && !rowIsOdd)//奇置序列 空格行号为偶数
{
int temp = list2[]; //交换前两个元素,改变奇偶性
list2[] = list2[];
list2[] = temp;
}
}
else //N为奇数,序列必须为偶置序列
{
if (list2IsOdd)
{
if (rowIsOdd)
{
int temp = list2[N]; //交换后两个元素,改变奇偶性
list2[N] = list2[N + ];
list2[N + ] = temp;
}
else
{
int temp = list2[]; //交换前两个元素,改变奇偶性
list2[] = list2[];
list2[] = temp;
}
}
} for (int i = ; i < chessManList.Count; i++)
{
chessManList[i].Text = (list2[i] + ).ToString();
if (list2[i] == chessManList.Count - )
{
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 = ; i < chessManList.Count; i++)
{
if (chessManList[i].Text != (i + ).ToString())
{
return false;
}
}
return true;
}
#endregion
}
}
c# 陈景润 15 子问题的更多相关文章
- [30分钟]MSSQL快速入门教程
1.什么是SQL语句 sql语言:结构化的查询语言.(Structured Query Language),是关系数据库管理系统的标准语言. 它是一种解释语言:写一句执行一句,不需要整体编译执行.语法 ...
- [转]史上最全的MSSQL复习笔记
阅读目录 1.什么是SQL语句 2.使用sql语句创建数据库和表 3.创建数据表 4.数据完整性约束 5.四中基本字符类型说明 6.SQL基本语句 7.类型转换函数 8.日期函数 9.数学函数 10. ...
- MSSQL学习笔记
阅读目录 1.什么是SQL语句 2.使用sql语句创建数据库和表 3.创建数据表 4.数据完整性约束 5.四中基本字符类型说明 6.SQL基本语句 7.类型转换函数 8.日期函数 9.数学函数 10. ...
- 1.5.8 语言分析器(Analyzer)
语言分析器(Analyzer) 这部分包含了分词器(tokenizer)和过滤器(filter)关于字符转换和使用指定语言的相关信息.对于欧洲语言来说,tokenizer是相当直接的,Tokens被空 ...
- mysql复习笔记
阅读目录 1.什么是SQL语句2.使用sql语句创建数据库和表3.创建数据表4.数据完整性约束5.四中基本字符类型说明6.SQL基本语句7.类型转换函数8.日期函数9.数学函数10.字符串函数11.联 ...
- Javascript 常用代码总结
1. document.referrer可以获得上一页的地址,使用document.anchors获得页面上面所有的链接元素,而不必使用 document.getElementsByTagName(' ...
- 通过memcached来实现对tomcat集群中Session的共享策略
近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...
- unity案例入门(二)(坦克大战)
1. 案例简述 这个案例实现一个简单的坦克对战游戏,两个玩家在一个地图上PK. 2. 控制坦克移动 与案例一中小球的移动方式不同,坦克在横向上不能是平移,因此横向按键控制的应该是坦克旋转. publi ...
- JavaScript 基础学习1-day14
JavaScript 基础学习1 知识预览JavaScript概述二 JavaScript的基础三 JavaScript的对象BOM对象DOM对象实例练习js扩展 JavaScript概述 JavaS ...
随机推荐
- Tkinter初体验
一.基本步骤 1.导入Tkinter模块 2.创建根窗口 3.填充组件 4.组件关联逻辑 5.进入主循环 二.Code #coding:utf-8 ''' 网关流量校验器 @author: Hongz ...
- 十、设计模式之代理(Proxy)模式
什么是代理模式 代理模式是对象的结构模式,为其他对象提供一种对象以控制对这个对象的访问. 代理模式的结构图如下:(源自大话设计模式) Subject:定义了RealSubject和Proxy的公共 ...
- Android面向切面编程(AOP)(转)
转自:https://www.jianshu.com/p/aa1112dbebc7 一.简述 1.AOP的概念 如果你用java做过后台开发,那么你一定知道AOP这个概念.如果不知道也无妨,套用百度百 ...
- mysql-alter语句常用操作
删除列 alter table table-name drop col-name 增加列(多列) alter table table-name add col-name col-type commen ...
- 如何用Word制作斜线表头?
如何用Word制作斜线表头?遇到这种问题,你一般是如何操作?本期企业网盘坚果云干货分享与大家分享有关斜线表头的制作方法. 斜线表头分单斜线表头和多斜线表头,下面分情况来了解相关的解决办法. 单斜线表头 ...
- postgres之使用python连接并操作
取一万个随机数,插入数据库 import random import psycopg2 import string conn=psycopg2.connect(database='postgres', ...
- 【leetcode】922. Sort Array By Parity II
题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...
- shell 输入输出重定向
1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...
- 【原理】scan
SCAN 命令的保证(guarantees) 同一个元素可能会被返回多次. 处理重复元素的工作交由应用程序负责, 比如说, 可以考虑将迭代返回的元素仅仅用于可以安全地重复执行多次的操作上. 如果一个元 ...
- layui-treeTable v2.0添加搜索功能
layui-treeTable 添加搜索功能 在树形表格头部加一个input框: <div class="layui-inline"> <input class= ...