这道题试了好多次,首先是超时,把用到的三个函数删掉,替换成相应的代码后,居然不超时了,变为了Wrong Answer…
发现只考虑隔了一个棋子的情况,结果依然WA
一波手动打图例中的测试数据并debug后,发现原来写的用来判断某个方向是否有对方棋子的函数nearopst()有问题,最后终于AC
历时24…
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 10;
const int mov[8][2] = { -1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1 };
inline bool nearopst(int x, int y, char curplay, int dx, int dy, const char board[][MAXN])
{
if (x + dx >= 1 && x + dx <= 8 && y + dy >= 1 && y + dy <= 8 && board[x + dx][y + dy] != '-'
&& board[x + dx][y + dy] != curplay)
return true;
return false;
}
int main()
{
int t, cnt = 1;
cin >> t;
while (t-- > 0)
{
if (cnt++ > 1) { cout << endl; }
char board[MAXN][MAXN] = { 0 };
for (int i = 1; i <= 8; ++i)
{
for (int j = 1; j <= 8; ++j) {
cin >> board[i][j];
}
}
char curplay;
cin >> curplay;
string input;
while (cin >> input)
{
if (input[0] == 'L') {
vector<pair<int, int>>list;
for (int i = 1; i <= 8; ++i)
{
for (int j = 1; j <= 8; ++j)
{
for (int k = 0; k < 8; ++k)
{
int dis = 1;
while (board[i][j] == curplay && nearopst(i, j, curplay, dis*mov[k][0], dis*mov[k][1], board)) { ++dis; };
if( nearopst(i, j, curplay, mov[k][0], mov[k][1], board)&& board[i + dis * mov[k][0]][j + dis * mov[k][1]] == '-')
list.push_back(make_pair(i + dis * mov[k][0], j + dis * mov[k][1]));
}
}
}
sort(list.begin(), list.end());
if (list.size() != 0)
{
int prex = 0, prey = 0;
for (int i = 0; i < list.size(); ++i)
{
if (list[i].first == prex && list[i].second == prey) continue;
prex = list[i].first, prey = list[i].second;
if (i > 0) cout << ' ';
cout << "(" << list[i].first << "," << list[i].second << ")";
}
cout << endl;
}
else
cout << "No legal move.\n";
}
else if (input[0] == 'Q') {
for (int i = 1; i <= 8; ++i)
{
for (int j = 1; j <= 8; ++j)
cout << board[i][j];
cout << endl;
}
break;
}
else if(input[0]=='M'){
int x = input[1] - '0', y = input[2] - '0';
int cnt = 0;
for (int k = 0; k < 8; ++k)
{
int dis = 1;
while (nearopst(x, y, curplay, dis * mov[k][0], dis * mov[k][1], board)) { ++dis; }
if (board[x + dis * mov[k][0]][y + dis * mov[k][1]] == curplay && nearopst(x, y, curplay, mov[k][0], mov[k][1], board))
{
board[x][y] = curplay; ++cnt;
for (int m = dis; m >= 1; --m)
{
board[x + m*mov[k][0]][y + m*mov[k][1]] = curplay;
}
}
}
curplay = curplay == 'W' ? 'B' : 'W';
if (cnt == 0) {
for (int k = 0; k < 8; ++k)
{
int dis = 1;
while (nearopst(x, y, curplay, dis * mov[k][0], dis * mov[k][1], board)) { ++dis; }
if (board[x + dis * mov[k][0]][y + dis * mov[k][1]] == curplay && nearopst(x, y, curplay, mov[k][0], mov[k][1], board))
{
board[x][y] = curplay; ++cnt;
for (int m = dis; m >= 1; --m)
{
board[x + m * mov[k][0]][y + m * mov[k][1]] = curplay;
}
}
}
curplay = curplay == 'W' ? 'B' : 'W';
}
int black = 0, white = 0;
for (int i = 1; i <= 8; ++i)
for (int j = 1; j <= 8; ++j)
{
if (board[i][j] == 'B')++black;
if (board[i][j] == 'W')++white;
}
cout << "Black - " << setw(2) << black << ' ' << "White - " << setw(2) << white << endl;
}
}
}
}