您的位置:首页 > 编程语言 > Java开发

UVAOJ 127-"Accordian " Patience | java实现

2015-02-06 08:00 363 查看
Time limit: 3.000 seconds

限时:3.000秒



Problem

问题

You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows:

模拟玩一个“手风琴”纸牌游戏,规则如下:

Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match
if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all
piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.

按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠。游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面。如果牌被移动后又出现了上述情况,则需再次向左移动。每叠牌只能移动最上面的一张。如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙。依次将整副牌都发完,并不断的向左合并。如果全部移动结束后整副牌都放成了一叠,则游戏胜利。

Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left,
move it three positions.

玩上几次你就会遇到一些状况。如果同时有两张牌都可以移动,你应该采取的策略是移动最左边的牌(当然必须是可以移动的)。当一张牌既可以移动到左边第一张,又可以移动到左边第三张时,应移动到左边第三张上面。



Input

输入

Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a # as its first character.
Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).

输入到程序中的数据指定了一副牌的顺序。每组输入包括两行,每行26张牌,牌与牌之间用空格隔开。以#号开头的一行作为输入的结束行。每张牌由两个字符表示,第一个字符是点数:(A、 2-9、T=10、J、Q, K);第二个字符是花色:(C=梅花、D=方块、H=红心、S=黑桃)。



Output

输出

One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing "Accordian patience" with the pack of cards
as described by the corresponding pairs of input lines.

每两行输入(定义了一副52张牌的顺序)必须产生一行输出。每行输出包括剩下的叠数以及每叠中的牌数。



Sample Input

输入示例

QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S

8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C

AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD

AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

#



Sample Output

输出示例

6 piles remaining: 40 8 1 1 1 1

1 pile remaining: 52

(译注:第一个数字为剩下的叠数,当叠数为1时,后面输出“pile remaining: ”,否则输出“piles remaining: ”。接下来从左至右输出各叠牌的数量)

分析:其实只要顺着思路做就可以了,每发一张牌,就向前检验能不能match,然后将这张牌,放到最后match到的堆上面。然后再向前一堆一堆的检验(因为这张牌放在别堆上以后,其他堆表面的第一张牌,就可能可以继续向前match)。最后52张牌发完,也就结束了。

算法过程使用java的动态链表,没有过多考虑效率问题,只是考虑了实现

class Node{
		String[] face;
		
		public Node(String a, String b){
			face = new String[2];
			face[0] = a;
			face[1] = b;
		}
	}


import java.lang.reflect.Array;
import java.util.LinkedList;

public class Test {	
	public boolean match(Node a,Node b){
		if(a.face[0].equals(b.face[0]) || a.face[1].equals(b.face[1])) return true;
		return false;		
	}
	
	
	public static void main(String[] args) {
		
		Test test = new Test(); 
		LinkedList<Node> card = new LinkedList<>();
		//String s = "QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S 8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C";
		String s = "3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS 2C AC";
		String[] sn = s.split(" "); 
		LinkedList<LinkedList<Node>> piles = new LinkedList<LinkedList<Node>>();//排堆
		//初始化数据,将排存到card里面
		for(int i=0; i<52;i++){
			String a = sn[i].substring(0, 1);
			String b = sn[i].substring(1, 2);
			Node c = new Node(a,b);
			card.add(c);		
		}
		
		//开始发牌
		for(int i=0; i<52;i++){
			//新发一张牌,自己为一堆,加入piles数组
			LinkedList<Node> tmp = new LinkedList<Node>();
			tmp.add(card.get(i));
			piles.add(tmp);
			
			int j,k;
			for(j=piles.size()-1;j>0;j--){//从最后一堆开始开向前match				
				Node node = piles.get(j).getLast();//获取该堆的最后一张牌
				
				for(k = j;k>0;k--){//向前match	
					if(k>=3){//如果有前三张						
						if(test.match(node,piles.get(j-3).getLast())){//判断是否与前三张match
							k = k-2;
							continue;
						}
					}
					
					if(!test.match(node,piles.get(j-1).getLast())){//如果不能跟前三张,前一张匹配,跳出循环																	
						break;
					}					
				}
				
				if(k!=j){//说明至少找到一个匹配,则调换位置
					piles.get(k).add(piles.get(j).getLast());//将该堆最后一张取出来,然后增加到匹配到的堆上
					piles.get(j).removeLast();//将该堆最后一张去掉
					if(piles.get(j).isEmpty()){//如果该堆为空
						piles.remove(j);//在堆数组中移除它
					}
				}
				
			}			
		}
		System.out.println("剩下"+piles.size()+"堆");
		System.out.println(piles.getLast().getLast().face[0]+piles.getLast().getLast().face[1]);			
	}	

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: