计算机算法设计与分析
实验报告
专 业: 测试技术10-01
学 号: 54101311
姓 名: XXX
指导老师: 宋 胜 利
实验一:棋盘覆盖
一、实验目的与要求
1、理解算法的概念
2、实现棋盘化以及棋盘覆盖
二、实验题:
1、编程任务:设计棋盘覆盖的一个简单算法
2、输入数据:输入特殊方格的行号和特殊方格的列号以及棋盘的大小 3、结果输出:将计算结果输出显示棋盘
三、实验代码:
package two;
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.util.*; /** *
* @author 黄建强 */
public class FenZi {
public static void main(String args[]) {
new MyChessBoard(); } }
class MyChessBoard extends JFrame implements ActionListener { int dimen; //棋盘规模 int x_pos; //特殊点横坐标 int y_pos; //特殊点竖坐标 Container p;
public MyChessBoard() { super(); x_pos = 0; y_pos = 0; dimen = 0;
setTitle(\棋盘覆盖\
setBackground(Color.YELLOW);
1
setBounds(300,150,510,480); centreOnScreen(); //setResizable(false); p = getContentPane();
JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu(\操作\
JMenuItem jitem1 = new JMenuItem(\开始\ JMenuItem jitem2 = new JMenuItem(\退出\ jm.add(jitem1); jm.add(jitem2); jmb.add(jm);
setJMenuBar(jmb);
jitem1.addActionListener(this); jitem2.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); }
public void centreOnScreen() { //使窗体显示在屏幕中央 Dimension displaySize = getToolkit().getScreenSize(); Dimension winSize = getSize();
int x = (displaySize.width - winSize.width) / 2; int y = (displaySize.height - winSize.height) / 2; if(x < 0) { x = 0; }
if(y < 0) { y = 0; }
setLocation(x, y); }
public void actionPerformed(ActionEvent e) { if(e.getActionCommand()==\退出\ System.exit(0); }
else if(e.getActionCommand()==\开始\ //在此输入方阵大小
JOptionPane jop = new JOptionPane(); jop.setVisible(true); do{
dimen = Integer.parseInt(JOptionPane.showInputDialog(p,\输入方阵的行数:\输入棋盘的行数:(提示是2的指数)\TION_MESSAGE));
if(dimen/2==0)
dimen=Integer.parseInt(JOptionPane.showInputDialog(p,\请你重新输入:\
2
输入棋盘的行数:(提示是2的指数)\TION_MESSAGE));
}while(dimen/2==0);
System.out.println(dimen);
x_pos = (int)(dimen*Math.random()); //随机生成特殊点位置 y_pos = (int)(dimen*Math.random());
p.setLayout(new GridLayout(dimen, dimen)); System.out.println(x_pos+\ ChessBoard cb = new ChessBoard(dimen); cb.coverMethod(0, 0, x_pos, y_pos, dimen); int[][] board = cb.getBoard(); JButton btn;
for(int i=0; i for(int j=0; j btn = new JButton(\ btn.setFont(new Font(\ btn.setForeground(Color.red); btn.setBackground(new Color(0, 0, 0)); } else { btn = new JButton(); btn.setBackground(new Color((board[i][j]*134)%5, //生成 0-255的值,并与board[i][j]建立关系 (board[i][j]*145)%5, (board[i][j]*178)%5)); } p.add(btn); } } } System.out.println(\ this.setVisible(true); } } class ChessBoard { int [][] board; int tile; ChessBoard(int k) { board = new int [k][k]; //k*k的数组,保存最终结果 tile = 0; } 3 void coverMethod(int tr,int tc,int dr,int dc,int size) { //棋盘覆盖 if(size == 1) return; int t = ++tile; int s = size/2; if(dr coverMethod(tr,tc,dr,dc,s); } else { board[tr+s-1][tc+s-1] = t; coverMethod(tr,tc,tr+s-1,tc+s-1,s); } if(dr coverMethod(tr,tc+s,dr,dc,s); } else { board[tr+s-1][tc+s] = t; coverMethod(tr,tc+s,tr+s-1,tc+s,s); } if(dr>=tr+s && dc coverMethod(tr+s,tc,dr,dc,s); } else { board[tr+s][tc+s-1] = t; coverMethod(tr+s,tc,tr+s,tc+s-1,s); } if(dr>=tr+s && dc>=tc+s) { coverMethod(tr+s,tc+s,dr,dc,s); } else { board[tr+s][tc+s] = t; coverMethod(tr+s,tc+s,tr+s,tc+s,s); } } public int[][] getBoard() { //返回覆盖结果 return board; } 四、实验结果 4 =tc+s) { 计算机算法设计与分析实验报告
热门排序
推荐文章