Help Me with the Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3630 | Accepted: 2319 |
Description
Your task is to read a picture of a chessboard position and print it in the chess notation.
Input
The input consists of an ASCII-art picture of a chessboard with chess pieces on positions described by the input. The pieces of the white player are shown in upper-case letters, while the black player's pieces are lower-case letters. The letters are one of "K" (King), "Q" (Queen), "R" (Rook), "B" (Bishop), "N" (Knight), or "P" (Pawn). The chessboard outline is made of plus ("+"), minus ("-"), and pipe ("|") characters. The black fields are filled with colons (":"), white fields with dots (".").
Output
The output consists of two lines. The first line consists of the string "White: ", followed by the description of positions of the pieces of the white player. The second line consists of the string "Black: ", followed by the description of positions of the pieces of the black player.
The description of the position of the pieces is a comma-separated list of terms describing the pieces of the appropriate player. The description of a piece consists of a single upper-case letter that denotes the type of the piece (except for pawns, for that this identifier is omitted). This letter is immediatelly followed by the position of the piece in the standard chess notation -- a lower-case letter between "a" and "h" that determines the column ("a" is the leftmost column in the input) and a single digit between 1 and 8 that determines the row (8 is the first row in the input).
The pieces in the description must appear in the following order: King("K"), Queens ("Q"), Rooks ("R"), Bishops ("B"), Knights ("N"), and pawns. Note that the numbers of pieces may differ from the initial position because of capturing the pieces and the promotions of pawns. In case two pieces of the same type appear in the input, the piece with the smaller row number must be described before the other one if the pieces are white, and the one with the larger row number must be described first if the pieces are black. If two pieces of the same type appear in the same row, the one with the smaller column letter must appear first.
The description of the position of the pieces is a comma-separated list of terms describing the pieces of the appropriate player. The description of a piece consists of a single upper-case letter that denotes the type of the piece (except for pawns, for that this identifier is omitted). This letter is immediatelly followed by the position of the piece in the standard chess notation -- a lower-case letter between "a" and "h" that determines the column ("a" is the leftmost column in the input) and a single digit between 1 and 8 that determines the row (8 is the first row in the input).
The pieces in the description must appear in the following order: King("K"), Queens ("Q"), Rooks ("R"), Bishops ("B"), Knights ("N"), and pawns. Note that the numbers of pieces may differ from the initial position because of capturing the pieces and the promotions of pawns. In case two pieces of the same type appear in the input, the piece with the smaller row number must be described before the other one if the pieces are white, and the one with the larger row number must be described first if the pieces are black. If two pieces of the same type appear in the same row, the one with the smaller column letter must appear first.
Sample Input
+---+---+---+---+---+---+---+---+ |.r.|:::|.b.|:q:|.k.|:::|.n.|:r:| +---+---+---+---+---+---+---+---+ |:p:|.p.|:p:|.p.|:p:|.p.|:::|.p.| +---+---+---+---+---+---+---+---+ |...|:::|.n.|:::|...|:::|...|:p:| +---+---+---+---+---+---+---+---+ |:::|...|:::|...|:::|...|:::|...| +---+---+---+---+---+---+---+---+ |...|:::|...|:::|.P.|:::|...|:::| +---+---+---+---+---+---+---+---+ |:P:|...|:::|...|:::|...|:::|...| +---+---+---+---+---+---+---+---+ |.P.|:::|.P.|:P:|...|:P:|.P.|:P:| +---+---+---+---+---+---+---+---+ |:R:|.N.|:B:|.Q.|:K:|.B.|:::|.R.| +---+---+---+---+---+---+---+---+
Sample Output
White: Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,a2,c2,d2,f2,g2,h2,a3,e4 Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6
Source

1 #include<stdio.h> 2 #include<string.h> 3 #include<ctype.h> 4 #include<math.h> 5 #include<algorithm> 6 using namespace std; 7 char st[40] , chess[10][40] ; 8 int a[9][9] ; 9 10 struct white 11 { 12 char ch , row , col; 13 int pow ; 14 }wh[100]; 15 16 struct black 17 { 18 char ch , row , col ; 19 int pow ; 20 }bl[100]; 21 22 bool cmp2 (white a , white b) 23 { 24 if (a.pow < b.pow) 25 return 1 ; 26 if (a.pow > b.pow) 27 return 0 ; 28 if (a.pow == b.pow ) { 29 if (a.row == b.row) 30 return a.col < b.col ; 31 else 32 return a.row < b.row ; 33 } 34 } 35 36 bool cmp1 (black a , black b) 37 { 38 if (a.pow < b.pow) 39 return 1 ; 40 if (a.pow > b.pow) 41 return 0 ; 42 if (a.pow == b.pow ) { 43 if (a.row == b.row) 44 return a.col < b.col ; 45 else 46 return a.row > b.row ; 47 } 48 } 49 50 int main () 51 { 52 // freopen ("a.txt" , "r" , stdin) ; 53 for (int i = 1 ; i <= 17 ; i++) { 54 if (i & 1) 55 gets (st) ; 56 else 57 gets (chess[i / 2]) ; 58 } 59 int k , f; 60 61 for (int i = 1 ; i <= 8 ; i++) { 62 k = 1 ; 63 for (int j = 2 ; j < 33 ; j += 4 , k++) { 64 switch ( chess[i][j] ) 65 { 66 case 'k' : a[i][k] = 1 ; break ; 67 case 'q' : a[i][k] = 2 ; break ; 68 case 'r' : a[i][k] = 3 ; break ; 69 case 'b' : a[i][k] = 4 ; break ; 70 case 'n' : a[i][k] = 5 ; break ; 71 case 'p' : a[i][k] = 6 ; break ; 72 case 'K' : a[i][k] = -1 ; break ; 73 case 'Q' : a[i][k] = -2 ; break ; 74 case 'R' : a[i][k] = -3 ; break ; 75 case 'B' : a[i][k] = -4 ; break ; 76 case 'N' : a[i][k] = -5 ; break ; 77 case 'P' : a[i][k] = -6 ; break ; 78 case ':' : a[i][k] = 0 ; break ; 79 case '.' : a[i][k] = 0 ; break ; 80 default : break ; 81 } 82 } 83 } 84 /* for (int i = 1 ; i <= 8 ; i++) { 85 for (int j = 1 ; j <= 8 ; j++) { 86 printf ("%d " , a[i][j]) ; 87 } 88 puts ("") ; 89 }*/ 90 k = 0 , f = 0 ; 91 for (int i = 1 ; i <= 8 ; i++) { 92 for (int j = 1 ; j <= 8 ; j++) { 93 if (a[i][j] > 0) { 94 bl[k].pow = a[i][j] ; 95 bl[k].row = '0' + 9 - i ; 96 bl[k].col = 'a' + j - 1 ; 97 switch (a[i][j]) 98 { 99 case 1 : bl[k].ch = 'K' ; break ; 100 case 2 : bl[k].ch = 'Q' ; break ; 101 case 3 : bl[k].ch = 'R' ; break ; 102 case 4 : bl[k].ch = 'B' ; break ; 103 case 5 : bl[k].ch = 'N' ; break ; 104 default : break ; 105 } 106 k++ ; 107 } 108 if (a[i][j] < 0) { 109 wh[f].pow = -a[i][j] ; 110 wh[f].row = '0' + 9 - i ; 111 wh[f].col = 'a' + j - 1 ; 112 switch (-a[i][j]) 113 { 114 case 1 : wh[f].ch = 'K' ; break ; 115 case 2 : wh[f].ch = 'Q' ; break ; 116 case 3 : wh[f].ch = 'R' ; break ; 117 case 4 : wh[f].ch = 'B' ; break ; 118 case 5 : wh[f].ch = 'N' ; break ; 119 default : break ; 120 } 121 f++ ; 122 } 123 } 124 } 125 126 sort (bl , bl + k , cmp1) ; 127 sort (wh , wh + f , cmp2) ; 128 129 printf ("White: ") ; 130 for (int i = 0 ; i < f ; i++) { 131 // printf ("wh[%d].pow = %d " , i , wh[i].pow) ; 132 if (wh[i].pow != 6) 133 printf ("%c" , wh[i].ch) ; 134 printf ("%c%c" , wh[i].col , wh[i].row ) ; 135 if (i != f - 1) 136 printf (",") ; 137 } 138 puts ("") ; 139 140 printf ("Black: ") ; 141 for (int i = 0 ; i < k ; i++) { 142 if (bl[i].pow != 6) 143 printf ("%c" , bl[i].ch) ; 144 printf ("%c%c" , bl[i].col , bl[i].row ) ; 145 if (i != k - 1) 146 printf (",") ; 147 } 148 puts ("") ; 149 return 0 ; 150 }