标签:10196 int newx newy board && ........ uva 模拟
代码
// 11235.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <vector> #include <string> using namespace std; /* ..k..... ppp.pppp ........ .R...B.. ........ ........ PPPPPPPP K....... rnbqkbnr pppppppp ........ ........ ........ ........ PPPPPPPP RNBQKBNR rnbqk.nr ppp..ppp ....p... ...p.... .bPP.... .....N.. PP..PPPP RNBQKB.R ........ ........ ........ ........ ........ ........ ........ ........ Sample Output Game #1: black king is in check. Game #2: no king is in check. Game #3: white king is in check. */ vector<string> board; int horseX[8] = { 2,2,-2,-2,1,1,-1,-1 }; int horseY[8] = { 1,-1,1,-1,2,2,-2,-2 }; int slantX[4] = {-1,1,1,-1}; int slantY[4] = {-1,1,-1,1}; int lineX[4] = { 1,-1,0,0 }; int lineY[4] = { 0,0,1,-1 }; bool CheckCheck(int x, int y) { if (board[x][y] != 'k' && board[x][y] != 'K') return false; //检查王旁边有没马 for (int i = 0; i < 8; i++) { int newx = x + horseX[i]; int newy = y + horseY[i]; if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) { if (board[x][y] == 'k' && board[newx][newy] == 'H') { return true; } else if ( board[x][y] == 'K' && board[newx][newy] == 'h') { return true; } } } //检测斜线有无 for (int i = 0; i < 4; i++) { int newx = x; int newy = y; while (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) { newx = newx + slantX[i]; newy = newy + slantY[i]; int firstStep = 1; if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) { if (board[newx][newy] != '.') { if (board[x][y] == 'k' && (board[newx][newy] == 'Q' || board[newx][newy] == 'B')) { return true; } else if (board[x][y] == 'K' && (board[newx][newy] == 'q' || board[newx][newy] == 'b')) { return true; } if (firstStep == 1 && (board[newx][newy] == 'k' || board[newx][newy] == 'K')) return true; if (firstStep == 1 && board[x][y] == 'k' && board[newx][newy] == 'P' && (newx - x) == 1) { return true; } if (firstStep == 1 && board[x][y] == 'K' && board[newx][newy] == 'p' && (x - newx) == 1) { return true; } break; } firstStep = 0; } } } //检测横线有无 for (int i = 0; i < 4; i++) { int newx = x; int newy = y; while (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) { newx = newx + lineX[i]; newy = newy + lineY[i]; int firstStep = 1; if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) { if (board[newx][newy] != '.') { if (board[x][y] == 'k' && (board[newx][newy] == 'Q' || board[newx][newy] == 'R')) { return true; } else if (board[x][y] == 'K' && (board[newx][newy] == 'q' || board[newx][newy] == 'r')) { return true; } if (firstStep == 1 && (board[newx][newy] == 'k' || board[newx][newy] == 'K')) return true; if (firstStep == 1 && board[x][y] == 'k' && board[newx][newy] == 'P' && (newx - x) == 1) { return true; } if (firstStep == 1 && board[x][y] == 'K' && board[newx][newy] == 'p' && (x - newx) == 1) { return true; } break; } firstStep = 0; } } } return false; } int main() { int idx = 0; while (1) { string s = "no king is in check."; board.clear(); int isFinish = 1; for (int i = 0; i < 8; i++) { string s; cin >> s; board.push_back(s); if (s != "........") isFinish = 0; } if (1 == isFinish) break; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if(CheckCheck(i,j) == 1){ if (board[i][j] == 'K') { s = "white king is in check."; }else{ s = "black king is in check."; } goto RESULT; } } } RESULT: idx++; cout << "Game #" << idx << ": " << s << endl; } return 0; }
标签:10196,int,newx,newy,board,&&,........,uva,模拟 来源: https://www.cnblogs.com/itdef/p/13299281.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。