127 - "Accordian" Patience




 ``Accordian'' Patience 

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).

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.

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.发牌
2.根据匹配规则查找存储位置
  若找到存储位置,存储之,并更新由其导致的牌的移动
  若没找到,新建一个堆(用栈表示),新建末尾位置,并放到当前最后位置
3.重复1,2

代码:

#include <iostream>
#include <stack>
#include <vector>
#include <string>

using namespace std;


vector<stack<string>> stacks_vector;

void print_result()
{//输出结果
	int all_num=stacks_vector.size();
	cout<<all_num<<(all_num>1 ? " piles ":" pile ")<<"remaining:";
	for(size_t i=0;i<stacks_vector.size();++i){
		cout<<" "<<stacks_vector[i].size();
	}
	cout<<endl;
}

int get_save_pos(const string &data,int end_pos)
{//保存数据,返回数据的存储位置,失败返回-1
	int save_pos=end_pos;
	int is_find=false;
	while(save_pos>0){//寻找存储位置
		bool flag=false;
		if(save_pos-3>=0){
			if(stacks_vector[save_pos-3].top()[0]==data[0] || stacks_vector[save_pos-3].top()[1]==data[1]){
				save_pos-=3;
				is_find=true;
				flag=true;
				continue;
			}
		}
		if(save_pos-1>=0){
			if(stacks_vector[save_pos-1].top()[0]==data[0] || stacks_vector[save_pos-1].top()[1]==data[1]){
				save_pos-=1;
				is_find=true;
				flag=true;
				continue;
			}
		}
		if(!flag) break;
	}
	if(is_find)
	   return save_pos;
	return -1;
}

void update_vector(size_t start_pos)
{//移动数据后更新[start_pos,end_pos)栈中元素
	for(size_t i=start_pos;i<stacks_vector.size();++i){
		stack<string> &_stack=stacks_vector[i];
		const string _data=_stack.top();
		int pos=get_save_pos(_data,i);
		if(pos>=0){
			_stack.pop();
			if(_stack.empty()){
				stacks_vector.erase(stacks_vector.cbegin()+i);
			}
			stacks_vector[pos].push(_data);
			i=pos;
		}
	}
}

int main()
{
	string data;
	int num=0;
	while(cin>>data){
		if(data=="#") break;
		++num;
		int save_pos=get_save_pos(data,stacks_vector.size());
		if(save_pos>=0){//找到
			stacks_vector[save_pos].push(data);
			update_vector(save_pos+1);
		}else{
			stack<string> stk;
			stk.push(data);
			stacks_vector.push_back(stk);
		}
		if(num==52){
			print_result();//输出结果
			stacks_vector.clear();
			num=0;
		}
	}
	return 0;
}

/*
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
#
6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52
*/



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值