Cow Solitaire
Late summer on the farm is a slow time, very slow. Betsy has little to do but play cow solitaire. For self-evident reasons, cow solitaire is not so challenging as any number of solitaire games played by humans.
Cow solitaire is played using an N x N (3 <= N <= 7) grid of ordinary playing cards with four suits (Clubs, Diamonds, Hearts, and Spaces) of 13 cards (Ace, 2, 3, 4, ..., 10, Jack, Queen, King). Cards are named with two characters: their value (A, 2, 3, 4, ..., 9, T, J, Q, K) followed by their suit (C, D, H, S). Below is a typical grid when N = 4:
8S AD 3C AC (Eight of Spades, Ace of Diamonds, etc.)
8C 4H QD QS
5D 9H KC 7H
TC QC AS 2D
To play this solitaire game, Betsy starts in the lower left corner (TC) and proceeds using exactly 2*N-2 moves of 'right' or 'up' to the upper right corner. Along the way, she accumulates points for each card (Ace is worth 1 point, 2 is worth 2 points, ..., 9 is worth 9 points, T is worth 10 points, J is 11, Q is 12, and K is 13) she traverses. Her goal is to amass the highest score.
If Betsy's path was TC-QC-AS-2C-7H-QS-AC, her score would be 10+12+1+2+7+12+1=45. Had she taken the left side then top (TC-5D-8C-8S-AD-3C-AC), her score would be 10+5+8+8+1+3+1=36, not as good as the other route. The best score for this grid is 69 points (TC-QC-9H-KC-QD-QS-AC => 10+12+9+13+12+12+1).Betsy wants to know the best score she can achieve. One of the geek cows once told her something about "working from the end back to the beginning," but she didn't understand what they meant.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 lists the cards on row i (row 1 is the top row) using N space-separated card names arranged in the obvious order.
Output
* Line 1: A single line with an integer that is the best possible score Betsy can achieve.
Sample Input
4
8S AD 3C AC
8C 4H QD QS
5D 9H KC 7H
TC QC AS 2D
Sample Output
69
Source
Elite 2007 US Open Competition
代码:
#include<iostream>
#include<string>
using namespace std;
string a[100][100];
int b[100][100];
int mark[100][100];
int main()
{
int n,i,j;
cin>>n;
//memset是按字节进行赋值的
memset(mark,0,sizeof(mark));
memset(b,0,sizeof(b));
//按正序输入,那么目标位【1】【N】
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j][0]>=65) //if it is A,T,J,Q,K
{
switch (a[i][j][0])
{
case 'A': b[i][j]=1;break;
case 'T': b[i][j]=10;break;
case 'J': b[i][j]=11;break;
case 'Q': b[i][j]=12;break;
case 'K': b[i][j]=13;break;
}
}
else
b[i][j]=a[i][j][0]-48; //'0'的ASCII码值为48
}
}
for(i=n;i>=1;i--)
for(j=1;j<=n;j++)
{
if(mark[i][j-1]>=mark[i+1][j])
mark[i][j]=mark[i][j-1]+b[i][j];
else
mark[i][j]=mark[i+1][j]+b[i][j];
}
cout<<mark[1][n]<<endl;
return 0;
}
/*
//按反序输入,那么目标位【N】【N】
for(i=n;i>=1;i--)
for(j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j][0]>=65) //if it is A,T,J,Q,K
{
switch (a[i][j][0])
{
case 'A': b[i][j]=1;break;
case 'T': b[i][j]=10;break;
case 'J': b[i][j]=11;break;
case 'Q': b[i][j]=12;break;
case 'K': b[i][j]=13;break;
}
}
else
b[i][j]=a[i][j][0]-48;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(mark[i][j-1]>=mark[i-1][j])
mark[i][j]=mark[i][j-1]+b[i][j];
else
mark[i][j]=mark[i-1][j]+b[i][j];
}
cout<<mark[n][n]<<endl;
*/