Tic Tac Win:根据给定的棋盘,判断井字游戏的输赢。
传统的井字游戏就是3 x 3的,所以一共3 ^ 9 = 19683种状态,存下来直接查就好了,但是力扣上是N x N的棋盘,所以这种方法就不适用了。
只要依次检查每一行、每一列还有两条对角线就可以了。
class Solution {
public:
string tictactoe(vector<string>& board) {
char first;
size_t r, c;
r = 0;
for(r = 0; r < board.size(); r++)
{
first = board[r][0];
if(first == ' ') continue;
c = 1;
while(c < board[r].size()){
if(board[r][c] != first) break;
c++;
}
if(c == board[r].size()) return string(1, first);
}
for(c = 0; c < board[0].size(); c++){
first = board[0][c];
if(first == ' ') continue;
r = 1;
while(r < board.size()){
if(board[r][c] != first) break;
r++;
}
if(r == board.size()) return string(1, first);;
}
r = 0, c = 0;
first = board[r][c];
if(first != ' '){
while(r < board.size() && c < board[r].size()){
if(board[r][c] != first) break;
r++, c++;
}
if(r == board.size() && c == board[0].size()) return string(1, first);;
}
r = 0, c = board[0].size();
first = board[r][c - 1];
if(first != ' '){
while(r < board.size() && c > 0){
if(board[r][c - 1] != first) break;
r++, c--;
}
if(r == board.size() && c == 0) return string(1, first);
}
r = 0;
while(r < board.size()){
c = 0;
while(c < board[r].size()){
if(board[r][c] == ' ') return "Pending";
c++;
}
r++;
}
return "Draw";
}
};
但是代码写起来很难看,大部分情况下都会选择复制前面的代码然后改改下标,这样非常容易出错,所以应该借鉴迭代器模式进行访问。
迭代器模式提供了一种访问聚合元素的方法,使用迭代器定义的next()运算就可以顺序访问下一个元素。Iterator类构造函数传入迭代器的起始坐标、坐标的递增方式和坐标的有效范围。
class Solution {
public:
string tictactoe(vector<string>& board) {
vector<Iterator> vecIter;
for(size_t i = 0; i < board.size(); i++)
{
vecIter.push_back(Iterator(i, 0, 0, 1, board.size(), board[0].size()));
}
for(size_t j = 0; j < board[0].size(); j++)
{
vecIter.push_back(Iterator(0, j, 1, 0, board.size(), board[0].size()));
}
vecIter.push_back(Iterator(0, 0, 1, 1, board.size(), board[0].size()));
vecIter.push_back(Iterator(0, board.size() - 1, 1, -1, board.size(), board[0].size()));
for(Iterator iter : vecIter)
{
char first = board[iter.row][iter.col];
if(first == ' ') continue;
iter.next();
while(!iter.end()){
if(board[iter.row][iter.col] != first) break;
iter.next();
}
if(iter.end()) return string(1, first);
}
for(size_t i = 0; i < board.size(); i++)
{
for(size_t j = 0; j < board[i].size(); j++)
{
if(board[i][j] == ' ') return "Pending";
}
}
return "Draw";
}
private:
struct Iterator
{
int row, col;
int rowInc, colInc;
int rowBound, colBound;
Iterator(int row, int col, int rowInc, int colInc, int rowBound, int colBound) : row(row), col(col), rowInc(rowInc), colInc(colInc), rowBound(rowBound), colBound(colBound){}
void next()
{
row += rowInc;
col += colInc;
}
bool end()
{
return !(0 <= row && row < rowBound && 0 <= col && col < colBound);
}
};
};

被折叠的 条评论
为什么被折叠?



