import java.util.Vector;
/**
 * 输入一系列牌,例如“3 K”“4 A”“4 S”“5 D”“8 S”,
 * 花色只能为A、S、D、K中的一种,
 * 点数只能为1到10之间的数,
 * 输入的牌相互之间没有重复,例如在一次输入中不会有2张“4 A”。
 * 要求对输入的牌进行组合,使其剩余的牌数最少。
 * 组合的形式:
 * 1、同花顺:花色一样,点数相连,(10和1不算连),至少3张,多则不限。
 * 例如“3 K”“2 K”“1 K”
 * 2、炸弹:点数一样,花色不限,至少3张,多则不限。例如“7 K”“7 S”“7 D”
 */
public class PlayCards
{
    //结果
    private int result = 0;
    private int[][] result_cardsTable = new int[14][4];
    public PlayCards()
    {
    }    public int getresult(String[] cards)
    {
        if (cards == null || cards.length < 3)
        {
            return 0;
        }
        /**把输入的牌按牌型和点数存入二维数组中
         * 二维数组int [14][4],所有初始值为0,从第2行到第11行使用
         * 例如“3 K”就把数组的第4行第3列设置为1
         **/
        int[][] cardsTable = new int[14][4];//第0、1、12、13行不会被使用到,始终空缺
        int len = cards.length;
        for (int i = 0; i < len; i++)
        {
            String oneCards = cards[i];
            int rowIndex = Integer.parseInt(oneCards.substring(0, oneCards.indexOf(" ")));
            int colIndex = -1;
String colString = oneCards.substring(oneCards.indexOf(" ") + 1, oneCards.length());
            if (colString.equals("A"))
            {
                colIndex = 0;
            }
            else if (colString.equals("S"))
            {
                colIndex = 1;
            }
            else if (colString.equals("D"))
            {
                colIndex = 2;
            }
            else
            {
                colIndex = 3;
            }
            cardsTable[rowIndex + 1][colIndex] = 1;
        }
        /**首次清理二维数组并且赋初值
         * 判断哪些输入可以直接丢弃(不可能与其它输入构成符合条件的序列)
         * 对于可丢弃的,cardsTable中相应的位置置0
         * 既可以用于组成炸弹又可以组成同花顺,cardsTable中相应的位置置15
         * 只能用于炸弹的,cardsTable中相应的位置置3
         * 只能组成同花顺的,cardsTable中相应的位置置5
         * 既可用于炸弹、又可用于同花顺的牌成为关键牌,记录关键牌在数组中的位置
         **/
        Vector importantCards = new Vector();
        for (int i = 2; i < 12; i++)
        {
        if (cardsTable[i][0] + cardsTable[i][1] + cardsTable[i][2] + cardsTable[i][3] > 2)
            {
                cardsTable[i][0] *= 3;
                cardsTable[i][1] *= 3;
                cardsTable[i][2] *= 3;
                cardsTable[i][3] *= 3;
            }
            for (int j = 0; j < 4; j++)
            {
                if (cardsTable[i][j] * cardsTable[i + 1][j] * cardsTable[i + 2][j] != 0
                    || cardsTable[i][j] * cardsTable[i + 1][j] * cardsTable[i - 1][j] != 0
                   || cardsTable[i][j] * cardsTable[i - 1][j] * cardsTable[i - 2][j] != 0)
                {
                    cardsTable[i][j] *= 5;
                }
                //移除不可用的牌
                if (cardsTable[i][j] == 1)
                {
                    cardsTable[i][j] = 0;
                }
                //把关键牌保存入importantCards
                if (cardsTable[i][j] == 15)
                {
                    importantCards.add(new int[] { i, j });
                }
            }
        }
        getResult(cardsTable, 0, importantCards);
        printResult(result_cardsTable);
        return result;
    }    //迭代处理
    private void getResult(int[][] cards, int curPosition, Vector importantCards)
    {
        
        if (curPosition == importantCards.size())
        {
            getCount(cards);
            return;
        }
        int[] curCard = (int[]) importantCards.get(curPosition);
        //复制传入的cards
        int[][] tempCards_1 = new int[14][4];
        int[][] tempCards_2 = new int[14][4];
        for (int i = 2; i < 12; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                tempCards_1[i][j] = cards[i][j];
                tempCards_2[i][j] = cards[i][j];
            }
        }
        if (cards[curCard[0]][curCard[1]] != 15)
        {
            if (cards[curCard[0]][curCard[1]] == 5)
            {
                getResult(tempCards_1, curPosition, importantCards, false);
            }
            //(cards[curCard[0]][curCard[1]]==3)
            else
            {
                getResult(tempCards_1, curPosition, importantCards, true);
            }
        }
        else
        {
            getResult(tempCards_1, curPosition, importantCards, true);
            getResult(tempCards_2, curPosition, importantCards, false);
        }
    }    //迭代处理
    //true: 只能用于行
    //false: 只能用于列
    private void getResult(int[][] cards, int curPosition, Vector importantCards, boolean uesdInRow)
    {
        int[] curCard = (int[]) importantCards.get(curPosition);
        int row = curCard[0];
        int col = curCard[1];
        if (uesdInRow)
        {
            cards[row][col] = 0;
            for (int i = row - 2; i < row + 3; i++)
            {
                if (cards[i][col] >= 5)
                {
        if ((cards[i - 2][col] / 5) * (cards[i - 1][col] / 5) * (cards[i][col] / 5) == 0
           && (cards[i - 1][col] / 5) * (cards[i][col] / 5) * (cards[i + 1][col] / 5) == 0
          && (cards[i][col] / 5) * (cards[i + 1][col] / 5) * (cards[i + 2][col] / 5) == 0)
                    {cards[i][col] = cards[i][col] == 15 ? 3 : 0 ;}
                }
            }
            cards[row][col] = 5;
        }
        else
        {
            cards[row][col] = 5;
            int sum = cards[row][0] * cards[row][1] * cards[row][2] * cards[row][3];
            if (sum == 0 || sum % 27 != 0)
            {
cards[row][0] = cards[row][0] == 15 ? 5 : cards[row][0] == 3 ? 0 : cards[row][0];
cards[row][1] = cards[row][1] == 15 ? 5 : cards[row][1] == 3 ? 0 : cards[row][1];
cards[row][2] = cards[row][2] == 15 ? 5 : cards[row][2] == 3 ? 0 : cards[row][2];
cards[row][3] = cards[row][3] == 15 ? 5 : cards[row][3] == 3 ? 0 : cards[row][3];
            }
        }
        getResult(cards, curPosition + 1, importantCards);
    }

解决方案 »

  1.   

    private void getCount(int[][] cards)
        {
            int tempResult = 0;
            for (int i = 2; i < 12; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (cards[i][j] != 0)
                    {
                        tempResult++;
                    }
                }
            }
            if (tempResult > result)
            {
                result_cardsTable = cards;
                result = tempResult;
            }
        }    //打印最后结果
        private void printResult(int[][] cards)
        {
            for (int i = 2; i < 12; i++)
            {
                String rr = "  ";
                String ss = "";
                for (int j = 0; j < 4; j++)
                {
                    if (cards[i][j] != 0)
                    {
                        rr = rr + (i - 1) + " " + getSuit(j) + " , ";
                    }
                    else
                    {
                        rr = rr + "0   , ";
                    }
                    ss = ss + cards[i][j] + "   , ";
                }
                rr = rr + "  |||   " + ss;
                System.out.println(rr);
            }
            System.out.println("");
            for (int i = 2; i < 12; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    String rr = "  ";
                    if (cards[i][j] == 3)
                    {
                        cards[i][j] = 0;
                        rr = rr + (i - 1) + " " + getSuit(j) + " , ";
                        for (int m = j + 1; m < 4; m++)
                        {
                            if (cards[i][m] == 3)
                            {
                                cards[i][m] = 0;
                                rr = rr + (i - 1) + " " + getSuit(m) + " , ";
                            }
                        }
                        System.out.println(rr);
                    }
                    if (cards[i][j] == 5)
                    {
                        cards[i][j] = 0;
                        rr = rr + (i - 1) + " " + getSuit(j) + " , ";
                        for (int m = i + 1; m < 12; m++)
                        {
                            if (cards[m][j] == 5)
                            {
                                cards[m][j] = 0;
                                rr = rr + (m - 1) + " " + getSuit(j) + " , ";
                            }
                        }
                        System.out.println(rr);
                    }
                }
            }
            System.out.println("");
        }    //把数字转化为花色
        private String getSuit(int i)
        {
            if (i == 0)
            {
                return "A";
            }
            else if (i == 1)
            {
                return "S";
            }
            else if (i == 2)
            {
                return "D";
            }
            //(i == 3)
            else
            {
                return "K";
            }
        }    public static void main(String[] args)
        {
            PlayCards mm = new PlayCards();
            System.out.println(mm.getresult(new String[] { "3 S", "3 A", "3 D", "3 K", "5 S", "5 A", "5 D", "5 K", "4 S",
                    "4 A", "6 D", "4 D", "4 K" }));
        }
    }
      

  2.   

    Problem Statement
    ????
    A group of vertical blocks are placed densely one after another on the ground. The blocks each have a width of 1, but their heights may vary. For example, if the heights of the vertical blocks are given as {1,5,5,1,6,1}, the configuration would look like the following picture:
     
    Your task is to reproduce the exact shape of this structure using some number of non-intersecting rectangles. You will be given a vector <int> heights representing the heights of the vertical blocks from left to right. Return the minimal number of rectangles necessary to perform this task with the given configuration of blocks.
    Definition
    ????
    Class:
    BlockStructure
    Method:
    cover
    Parameters:
    vector <int>
    Returns:
    int
    Method signature:
    int cover(vector <int> heights)
    (be sure your method is public)
    ????Constraints
    -
    heights will have between 1 and 50 elements, inclusive.
    -
    Each element of heights will be between 1 and 1000, inclusive.
    Examples
    0)????
    {1,5,5,1,6,1}
    Returns: 3
     
    We can use rectangles with sizes 6x1, 2x4 and 1x5.
    1)????
    {2,2,2,4,4}
    Returns: 2
     
    We can use a 3x2 rectangle and a 2x4 rectangle.
    2)????
    {6,6,6,6,6,6}
    Returns: 1
    The structure is a rectangle.
    3)????
    {71,44,95,16,10,80,12,17,98,61}
    Returns: 10
    It's impossible to use less than 10 rectangles.
    4)????
    {100,100,97,100,100,100,97,98,99,99}
    Returns: 5一些图标没有画出来。解法大致如下:首先,从左到右查找“左下块”矩形,然后,剩余了左下块上部分的不规则图形,和剩余的图形。那么,剩余的两个图形,可以递归调用Cover过程了。
    static int getCover(int[]h, int f,int la){ 
    if(f==la) 
         return 1; int min=20000; 
    for(int i=f;i<=la;i++){ 
    if(h[i]<min) 
    min=h[i]; 
    } int count=1; 
    int start=-1; 
    for(int i=f;i<=la;i++){ 
    h[i]-=min; 
    if(h[i]==0){ 
    if(start!=-1){ 
    count+=getCover(h,start,i-1); 
    start = -1; 

    }  
    else{ 
    if(start==-1){ 
    start = i; 
    }
    if(i==la){ 
    count+=getCover(h,start,la); 
    break; 



    return count; 
    } static int getMinCover(int[] h){ 
    return getCover(h,0,h.length-1); 
    }