[ create a new paste ] login | about

Link: http://codepad.org/qP23deHx    [ raw code | fork ]

C++, pasted on Apr 1:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <utility>
using namespace std;

string a[14][5];
int way;

pair<int,int> go(pair<int,int> s,int step) {
	if (way==0) {
		int id=s.first;
		for (int x=0;step>x;x++) {
			id++;
			if (id>13) id=1;
			while (1) {
				bool check=false;
				for (int y=1;4>=y;y++) {
					if (a[id][y]!="") check=true;
				}
				if (check)break;
				else {
					id++;
					if (id>13) id=1;
				}
			}
		}
		for (int i=1;4>=i;i++) {
			if (a[id][i]!="") return make_pair(id,i);
		}
	}
	else {
		int id=s.first;
		for (int x=0;step>x;x++) {
			id--;
			if (id<1) id=13;
			while (1) {
				bool check=false;
				for (int y=1;4>=y;y++) {
					if (a[id][y]!="") check=true;
				}
				if (check)break;
				else {
					id--;
					if (id<1) id=13;
				}
			}
		}
		for (int i=1;4>=i;i++) {
			if (a[id][i]!="") return make_pair(id,i);
		}
	}
}

pair<int,int> card(pair<int,int> s) {
	int x=s.first,y=s.second;
	if (a[x][y][0]=='A') {
		way=1-way;
		a[x][y]="";
		return go(s,2);
	}
	else if (a[x][y][0]=='J'||a[x][y][0]=='Q'||a[x][y][0]=='K'||a[x][y][0]=='T') {
		a[x][y]="";
		return go(s,2);
	}
	else {
		a[x][y]="";
		return go(s,3);
	}
}

bool check(pair<int,int> s) {
	if (a[s.first][s.second]=="AS") return true;
	else return false;
}

int solve() {
	pair<int,int> s;
	s=make_pair(1,1);
	way=0;
	for (int i=1;52>=i;i++) {
//		cout<<"i="<<i<<endl;
		if (check(s)) return i;
		s=card(s);
//		cout<<"i="<<i<<" : \n";
//		for(int x=1;13>=x;x++) {
//			for (int y=1;4>=y;y++) {
//				if (a[x][y]!="") cout<<a[x][y];
//				else cout<<"  ";
//				cout<<" ";
//			}
//			cout<<endl;
//		}
	}
}

int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin >> T;
	while (T--) {
		for (int x=1;4>=x;x++) {
			for (int y=1;13>=y;y++) {
				cin >> a[y][x];
			}
		}
		cout << solve() << endl;
		if (T!=0) cout<<endl;
	}
}

/*
2
TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
*/


Create a new paste based on this one


Comments: