日期:2014-05-20  浏览次数:20874 次

这部份怎完成????求助。。


Java code


public class Test2 {
    public static void main(String args []){
        //Define Variable
        Scanner scan=new Scanner(System.in);
        final int SIZE=6;
        int[][] num=new int[SIZE][SIZE];

                
        //Prompt for input
        System.out.println("Enter the initial Grid: ");
        for(int i = 0; i < num.length; i++){
            for(int j = 0; j < 6; j++){
                num[j]=scan.nextInt();
            }
        }
             //Display Before
        System.out.println("\n"+"Before:");
        for(int i = num.length-1; i >= 0; i--){
            System.out.print(i+": ");
            for(int j = 0; j < SIZE; j++){
                System.out.print(num[j] +" ");
            }
            System.out.println();
        }
        System.out.print("   ");
        for(int k = 1; k <= 11; k++){System.out.print("=");}
        System.out.print("\n"+"   ");
        for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
        System.out.println("\n");
       
         //Display After

        for(int i = num.length-1; i >= 0; i--){
          System.out.print(i+": ");
          for(int j = 0; j < SIZE; j++){


        ..................................................
            .................这部份怎完成.....................
            ..................................................
                

                        //印X
                if(num[i][j] == -1){
                    System.out.print("X ");
                    continue;
                }else{
                    System.out.print(num[i][j]+" ");
                }
            
          }
          System.out.println();
    }
    System.out.print("   ");
    for(int k = 1; k <= 11; k++){System.out.print("=");}
    System.out.print("\n"+"   ");
    for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
    System.out.println("\n");


      }            
}





注:不使用类方法 ,还不懂

------解决方案--------------------
你这个应该是对对碰或者ls说的玛丽医生的算法吧。
我帮你写了一个,你看看怎么样。效率可能有点低,你自己优化一下吧~
注释写得比较详细了,你应该能看懂。
如果有语法元素不清楚的话就去查查书吧。
总之要明白:Java的数组都是对象,无论是几维的。
Java code

package com;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Test2 {

    enum Direct{UP, DOWN, LEFT, RIGHT};//一个枚举量,用来表示方向
    static final int MIN_CELL_SIZE = 3;//常量,表示至少连续多少个
    
    public static void main(String args[]) {
        // Define Variable
//        Scanner scan = new Scanner(System.in);
        //读文件更便于测试,这里暂时改成了读文件。文件里即要找的那个矩阵
        Scanner scan = null;
        try {
            scan = new Scanner(new File("data/matrix.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final int SIZE = 6;
        int[][] num = new int[SIZE][SIZE];

        // Prompt for input
        System.out.println("Enter the initial Grid: ");
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < 6; j++) {
                num[i][j] = scan.nextInt();
            }
        }
        
        // Display Before
        System.out.println("\n" + "Before:");
        for (int i = num.length - 1; i >= 0; i--) {
            System.out.print(i + ": ");
            for (int j = 0; j < SIZE; j++) {
                System.out.print(num[i][j] + " ");
            }
            System.out.println();
        }
        
        System.out.print("   ");
        for (int k = 1; k <= 11; k++) {
            System.out.print("=");
        }
        System.out.print("\n" + "   ");
        for (int k = 0; k <= 5; k++) {
            System.out.print(k + " ");
        }
        System.out.println("\n");

        //添加的函数,用来找出符合条件的单元
        ArrayList<MatchingCell> cellList = findMatchingCells(num);
        
        //添加的函数,设置相应的值
        setValue(num, cellList);
        
        // Display After
        System.out.println("\n" + "After:");
        for (int i = num.length - 1; i >= 0; i--) {
            System.out.print(i + ": ");
            for (int j = 0; j < SIZE; j++) {
                // 印X
                if (num[i][j] == -1) {
                    System.out.print("X ");
                    continue;
                } else {
                    System.out.print(num[i][j] + " ");
                }

            }
            System.out.println();
        }
        System.out.print("   ");
        for (int k = 1; k <= 11; k++) {
            System.out.print("=");
        }
        System.out.print("\n" + "   ");
        for (int k = 0; k <= 5; k++) {
            System.out.print(k + " ");
        }
        System.out.println("\n");

    }

    /**
     * 用来找出符合条件的单元
     * @param matrix 输入的矩阵
     * @return 包含所有符合条件的单元的容器
     */
    private static ArrayList<MatchingCell> findMatchingCells(int[][] matrix) {
        // TODO Auto-generated method stub
        final int lineSize = matrix.length;
        final int colSize = matrix[0].length;
        ArrayList<MatchingCell> cellList = new ArrayList<MatchingCell>();
        
        //以每一个元素为中心试探。
        for ( int i=0; i<lineSize; i++ ){
            for ( int j=0; j<colSize; j++ ){
                check(matrix, j, i, cellList);
            }
        }
        
        return cellList;
    }

    /**
     * 从上下左右四个方向查找某一个点是否符合要求,是则将其加入容器
     * @param matrix 输入的矩阵
     * @param i 元素横坐标
     * @param j 元素纵坐标
     * @param cellList 包含符合条件元素的容器
     */
    private static void check(int[][] matrix, final int i, final int j, 
            ArrayList<MatchingCell> cellList) {
        // TODO Auto-generated method stub
        int toBeCheck = matrix[i][j];
        int index = 0;
        int count = 0;
        MatchingCell tempCell = null;
        
        //Upwards
        index = i-1;
        count = 0;
        while(index >= 0){
            if( matrix[index][j] != toBeCheck )
                break;
            index--;
        }
        count = i - index;
        if (count >= MIN_CELL_SIZE){
            tempCell = new MatchingCell(i, j, count, Direct.UP);
            cellList.add(tempCell);
        }
        
        //Downwards
        index = i+1;
        count = 0;
        while(index < matrix.length){
            if( matrix[index][j] != toBeCheck )
                break;
            index++;
        }
        count = index - i;
        if (count >= MIN_CELL_SIZE){
            tempCell = new MatchingCell(i, j, count, Direct.DOWN);
            cellList.add(tempCell);
        }
        
        //Left
        index = j-1;
        count = 0;
        while(index >= 0){
            if( matrix[i][index] != toBeCheck )
                break;
            index--;
        }
        count = j - index;
        if (count >= MIN_CELL_SIZE){
            tempCell = new MatchingCell(i, j, count, Direct.LEFT);
            cellList.add(tempCell);
        }
        
        //Right
        index = j+1;
        count = 0;
        while(index < matrix[0].length){
            if( matrix[i][index] != toBeCheck )
                break;
            index++;
        }
        count = index - j;
        if (count >= MIN_CELL_SIZE){
            tempCell = new MatchingCell(i, j, count, Direct.RIGHT);
            cellList.add(tempCell);
        }
    }
    

    /**
     * 根据cellList的记录把相应的值改为-1
     * @param matrix 需要改的矩阵
     * @param cellList 存放所有单元的容器
     */
    private static void setValue(int[][] matrix, ArrayList<MatchingCell> cellList) {
        for ( MatchingCell tempCell: cellList){
            switch(tempCell.direct){
            case UP:
                for ( int i=tempCell.lineIndex; i>tempCell.lineIndex - tempCell.num; i-- ){
                    matrix[i][tempCell.colIndex] = -1;
                }
                break;
            case DOWN:
                for ( int i=tempCell.lineIndex; i<tempCell.lineIndex + tempCell.num; i++ ){
                    matrix[i][tempCell.colIndex] = -1;
                }
                break;
            case LEFT:
                for ( int i=tempCell.colIndex; i>tempCell.colIndex - tempCell.num; i-- ){
                    matrix[tempCell.lineIndex][i] = -1;
                }
                break;
            case RIGHT:
                for ( int i=tempCell.colIndex; i<tempCell.colIndex + tempCell.num; i++ ){
                    matrix[tempCell.lineIndex][i] = -1;
                }
                break;
            }
        }
    }

    /**
     * 用来封装每一个查找到的单元。
     * @author Michael
     *
     */
    static class MatchingCell {
        final int lineIndex;//横坐标
        final int colIndex;//纵坐标
        final int num;//元素个数
        final Direct direct;//方向
        
        MatchingCell(int i, int j, int n, Direct d){
            lineIndex = i;
            colIndex = j;
            num = n;
            direct = d;
        }
    }
}