2009年4月21日星期二

迷官

說明
由於迷宮的設計,老鼠走迷宮的入口至出口路徑可能不只一條,如何求出所有的路徑呢?

解法
求所有路徑看起來複雜但其實更簡單,只要在老鼠走至出口時顯示經過的路徑,然後退回上一格重新選擇下一個位置繼續遞迴就可以了,比求出單一路徑還簡單,我們的程式只要作一點修改就可以了。
演算法

Procedure GO(maze[]) [
VISIT(maze, STARTI, STARTJ);
]

Procedure VISIT(maze[], i, j) [
maze[i][j] = 1;

IF(i == ENDI AND j == ENDJ) [
// FIND A ROUTE, PRINT THE ROUTE
]

IF(maze[i][j+1] == 0)
VISIT(maze, i, j+1);
IF(maze[i+1][j] == 0)
VISIT(maze, i+1, j);
IF(maze[i][j-1] == 0)
VISIT(maze, i, j-1);
if(maze[i-1][j] == 0)
VISIT(maze, i-1, j);

maze[i][j] = 0;
]


  • Java program
public class Mouse {
private int startI, startJ; // 入口
private int endI, endJ; // 出口

public static void main(String[] args) {
int maze[][] = {{2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 2, 0, 2, 2, 0, 2},
{2, 0, 2, 0, 0, 2, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 2},
{2, 2, 0, 2, 2, 0, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2}};

System.out.println("顯示迷宮:");
for(int i = 0; i < maze.length; i++) {
for(int j = 0; j < maze[0].length; j++)
if(maze[i][j] == 2)
System.out.print("█");
else
System.out.print(" ");
System.out.println();
}

Mouse mouse = new Mouse();
mouse.setStart(1, 1);
mouse.setEnd(7, 7);

mouse.go(maze);
}

public void setStart(int i, int j) {
this.startI = i;
this.startJ = j;
}

public void setEnd(int i, int j) {
this.endI = i;
this.endJ = j;
}

public void go(int[][] maze) {
visit(maze, startI, startJ);
}

private void visit(int[][] maze, int i, int j) {
maze[i][j] = 1;

if(i == endI && j == endJ) {
System.out.println("\n找到出口!");
for(int m = 0; m < maze.length; m++) {
for(int n = 0; n < maze[0].length; n++) {
if(maze[m][n] == 2)
System.out.print("█");
else if(maze[m][n] == 1)
System.out.print("◇");
else
System.out.print(" ");
}
System.out.println();
}
}

if(maze[i][j+1] == 0)
visit(maze, i, j+1);
if(maze[i+1][j] == 0)
visit(maze, i+1, j);
if(maze[i][j-1] == 0)
visit(maze, i, j-1);
if(maze[i-1][j] == 0)
visit(maze, i-1, j);

maze[i][j] = 0;
}
}

2009年4月20日星期一

得分排行

說明

假設有一教師依學生座號輸入考試分數,現希望在輸入完畢後自動顯示學生分數的排行,當然學生的分數可能相同


解法

這個問題基本上要解不難,只要使用額外的一個排行陣列走訪分數陣列就可以了,直接使用下面的程式片段作說明:
for(i = 0; i <>
juni[i] = 1;
for(j = 0; j <>
if(score[j] > score[i])
juni[i]++;
}
}

printf("得分\t排行\n");
for(i = 0; i <>
printf("%d\t%d\n", score[i], juni[i]);


上面這個方法雖然簡單,但是反覆計算的次數是n^2,如果n值變大,那麼運算的時間就會拖長;改變juni陣列的長度為n+2,並將初始值設定為0,如下所示:

得分排行

接下來走訪分數陣列,並在分數所對應的排行陣列索引元素上加1,如下所示:
得分排行

將排行陣列最右邊的元素設定為1,然後依序將右邊的元素值加至左邊一個元素,最後排行陣列中的「分數+1」」就是得該分數的排行,如下所示:
得分排行
這樣的方式看起來複雜,其實不過在計算某分數之前排行的人數,假設89分之前的排行人數為x人,則89分自然就是x+1了,這也是為什麼排行陣列最右邊要設定為1的原因;如果89分有y人,則88分自然就是x+y+1,整個陣列右邊元素向左加的原因正是如此。

如果分數有負分的情況,由於C/C++或Java等程式語言無法處理負的索引,所以必須加上一個偏移值,將所有的分數先往右偏移一個範圍即可,最後顯示的時候記得減回偏移值就可以了。



import java.io.*;

public class ScoreRank {
public static void main(String[] args)
throws NumberFormatException, IOException {
final int MAX = 100;
final int MIN = 0;

int[] score = new int[MAX+1];
int[] juni = new int[MAX+2];

BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
int count = 0;

do {
System.out.print("輸入分數,-1結束:");
score[count++] =
Integer.parseInt(reader.readLine());
} while((score[count-1] != -1));

count--;

for(int i = 0; i < count; i++)
juni[score[i]]++;
juni[MAX+1] = 1;

for(int i = MAX; i >= MIN; i--)
juni[i] = juni[i] + juni[i+1];

System.out.println("得分\t排行");
for(int i = 0; i < count; i++) {
System.out.println(score[i] + "\t" +
juni[score[i]+1]);
}
}
}

2009年4月1日星期三

從Java應用程序存取Microsoft Excel檔案

感謝Apache,現在 利用POI有辦法提供Java應用程序存取Microsoft Excel文件(.xls ) POI 可以從apache.org 下載。 以下是小教程如何寫入Excel文件。 寫Excel文件步驟是:
  1. 創建輸出文件使用FileOutputStream
  2. 創建一個工作簿創建一個實例 HSSFWorkbook
  3. 創建一個新的資產負債表,建立的一個實例 HSSFSheet
  4. 創建字體對象援引法createFont ( )的 HSSFWorkbook
  5. 創建細胞風格對象援引法createCellStyle ( )的 HSSFWorkbook
  6. 創建數據格式對象援引法createDataFormat 階級 HSSFWorkbook
  7. 創建行致電方法createRow ( rowNumber )由實例HSSFSheet
  8. 收件簿到輸出文件。
這裡是一個簡單的程序來寫Excel文件包括2細胞:


import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import java.io.*;
public class WriteSimpleXLS{

public WriteSimpleXLS() {
try{
FileOutputStream fos = new FileOutputStream("output.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFDataFormat df = wb.createDataFormat();
HSSFFont f = wb.createFont();

//set font
f.setFontHeightInPoints((short) 12);
f.setColor( (short)HSSFFont.COLOR_RED );

//set cell style
cs.setFont(f);
//set data format
cs.setDataFormat(df.getFormat(”#,##0.0〃));
wb.setSheetName(0, “First Sheet”);

//create row
r = s.createRow(0);
//create cell
c = r.createCell((short)0);
c.setCellStyle(cs);
c.setCellValue("Nico");
//create another cell
c = r.createCell((short)1);
c.setCellStyle(cs);
c.setCellValue("Ganteng");

//now that we’re done creating workbook,it’s time to write it to output file
wb.write(fos);
fos.close();
}catch(IOException e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}

public static void main(String[] arg){
WriteSimpleXLS wsx = new WriteSimpleXLS();
}
}