Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
黑棋和白棋用不同的搜索顺序先记录棋盘,再对棋盘遍历,找出各棋子的位置,避免记录棋子的数量;对黑棋和白棋用不同的遍历顺序,避免对棋子位置排序。贴一下代码: #include <iostream> #include <map> #include <cstring> using namespace std; char upper(char c) { if (c >= 'a' && c <= 'z') return char(c - 'a' + 'A'); return c; } void display(char board[][8], char c, bool isWhite) { char tmp; int row; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { row = isWhite ? 7 - i : i; tmp = board[row][j]; if ( tmp == c) { if (c == 'k' || c == 'K') { std::cout << upper(c) << char('a' + j) << 8 - row; } else if (c == 'p' || c == 'P') { cout << "," << char('a' + j) << 8 - row; } else { cout << "," << upper(c) << char('a' + j) << 8 - row; } } } } } int main() { char board[8][8]; // (row, col) memset(board, 0, sizeof(board)); char c; int cnt = 0; char white[] = "KQRBNP"; char black[] = "kqrbnp"; do { c = getchar(); if (c == ':' || c == '.') { char tmp = getchar(); if(tmp != c) board[cnt / 8][cnt % 8] = tmp; cnt++; getchar(); } } while (c != EOF); cout << "White: "; for (int i = 0; i < 6; ++i) { display(board, white[i], true); } cout << endl; cout << "Black: "; for (int i = 0; i < 6; ++i) { display(board, black[i], false); } cout << endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator