- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注意下面的评论。由于 GCC 中的错误,它无法在 UVA 上编译。
#include <cstdio>
#include <cstring>
#include <cctype>
#include <map>
#include <stdexcept>
class Board {
public:
bool read(FILE *);
enum Colour {none, white, black};
Colour check() const;
private:
struct Index {
size_t x;
size_t y;
Index &operator+=(const Index &) throw(std::range_error);
Index operator+(const Index &) const throw(std::range_error);
};
const static std::size_t size = 8;
char data[size][size];
// Cannot be compiled on GCC 4.1.2 due to GCC bug 29993
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29993
typedef bool CheckFunction(Colour, const Index &) const;
CheckFunction pawn, knight, bishop, king, rook;
bool queen(const Colour c, const Index &location) const {
return rook(c, location) || bishop(c, location);
}
static char get_king(Colour c) {
return c == white ? 'k' : 'K';
}
template<std::size_t n>
bool check_consecutive(Colour c, const Index &location, const Index (&offsets)[n]) const {
for(const Index *p = offsets; p != (&offsets)[1]; ++p) {
try {
Index target = location + *p;
for(; data[target.x][target.y] == '.'; target += *p) {
}
if(data[target.x][target.y] == get_king(c)) return true;
} catch(std::range_error &) {
}
}
return false;
}
template<std::size_t n>
bool check_distinct(Colour c, const Index &location, const Index (&offsets)[n]) const {
for(const Index *p = offsets; p != (&offsets)[1]; ++p) {
try {
Index target = location + *p;
if(data[target.x][target.y] == get_king(c)) return true;
} catch(std::range_error &) {
}
}
return false;
}
};
int main() {
Board board;
for(int d = 1; board.read(stdin); ++d) {
Board::Colour c = board.check();
const char *sp;
switch(c) {
case Board::black:
sp = "white";
break;
case Board::white:
sp = "black";
break;
case Board::none:
sp = "no";
break;
}
std::printf("Game #%d: %s king is in check.\n", d, sp);
std::getchar(); // discard empty line
}
}
bool Board::read(FILE *f) {
static const char empty[] =
"........"
"........"
"........"
"........"
"........"
"........"
"........"
"........"; // 64 dots
for(char (*p)[size] = data; p != (&data)[1]; ++p) {
std::fread(*p, size, 1, f);
std::fgetc(f); // discard new-line
}
return std::memcmp(empty, data, sizeof data);
}
Board::Colour Board::check() const {
std::map<char, CheckFunction Board::*> fp;
fp['P'] = &Board::pawn;
fp['N'] = &Board::knight;
fp['B'] = &Board::bishop;
fp['Q'] = &Board::queen;
fp['K'] = &Board::king;
fp['R'] = &Board::rook;
for(std::size_t i = 0; i != size; ++i) {
for(std::size_t j = 0; j != size; ++j) {
CheckFunction Board::* p = fp[std::toupper(data[i][j])];
if(p) {
Colour ret;
if(std::isupper(data[i][j])) ret = white;
else ret = black;
if((this->*p)(ret, (Index){i, j}/* C99 extension */)) return ret;
}
}
}
return none;
}
bool Board::pawn(const Colour c, const Index &location) const {
const std::ptrdiff_t sh = c == white ? -1 : 1;
const Index offsets[] = {
{sh, 1},
{sh, -1}
};
return check_distinct(c, location, offsets);
}
bool Board::knight(const Colour c, const Index &location) const {
static const Index offsets[] = {
{1, 2},
{2, 1},
{2, -1},
{1, -2},
{-1, -2},
{-2, -1},
{-2, 1},
{-1, 2}
};
return check_distinct(c, location, offsets);
}
bool Board::bishop(const Colour c, const Index &location) const {
static const Index offsets[] = {
{1, 1},
{1, -1},
{-1, -1},
{-1, 1}
};
return check_consecutive(c, location, offsets);
}
bool Board::rook(const Colour c, const Index &location) const {
static const Index offsets[] = {
{1, 0},
{0, -1},
{0, 1},
{-1, 0}
};
return check_consecutive(c, location, offsets);
}
bool Board::king(const Colour c, const Index &location) const {
static const Index offsets[] = {
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1}
};
return check_distinct(c, location, offsets);
}
Board::Index &Board::Index::operator+=(const Index &rhs) throw(std::range_error) {
if(x + rhs.x >= size || y + rhs.y >= size) throw std::range_error("result is larger than size");
x += rhs.x;
y += rhs.y;
return *this;
}
Board::Index Board::Index::operator+(const Index &rhs) const throw(std::range_error) {
Index ret = *this;
return ret += rhs;
}
最佳答案
不编译的代码只是一个快捷方式,用于将 pawn、knight、bishop、king 和 rook 声明为常量函数,这些函数采用 Color 和对 Index 的 const 引用,返回 bool。只需替换:
typedef bool CheckFunction(Colour, const Index &) const; CheckFunction pawn, knight, bishop, king, rook;
与:
bool pawn(Colour, const Index &) const;bool knight(Colour, const Index &) const;bool bishop(Colour, const Index &) const;bool king(Colour, const Index &) const;bool rook(Colour, const Index &) const;
坦率地说,我很惊讶有人如此懒惰以至于他们使用前者而不是后者。恕我直言,后者明显比前者更具可读性,而后者也没有那么长。
关于c++ - 如何在 UVA 上编译以下内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2532788/
这个问题在这里已经有了答案: standalone parentheses in javascript [duplicate] (5 个答案) 关闭 8 年前。 我在学习JavaScript,有时会
我是mysql新手,我必须减少以下更新查询的执行时间 UPDATE temp_countcalculations, ( SELECT count(*) as insuffcounts,CRP_
def escape_html(s): for (i, o) in (("&","&"),(">", ">"),(" "变成 ">"等。 关于python - 以下 for 循环
if (read(read(cin, data1), data2)) 问题是C++ Primer 5th Edition 的练习。 read 函数定义如下: std::istream &read(st
我想创建两个宏。其中一个将扩展到函数原型(prototype)和函数内容,另一个将扩展到仅函数原型(prototype)。我正在考虑创建以下内容: #ifdef SOME_CONDITION #def
我正在使用 jongo API - org.jongo.MongoCollection 是类。 我有对象 ID 列表并转换为与 ObjectId[] 相同并尝试按如下方式查询 collection.f
有人可以解释以下正则表达式匹配什么吗? ^.*$ 谢谢! 最佳答案 或者整个字符串或者整行,取决于是否multiline mode被使用。 关于java - 以下 ^.*$ 正则表达式匹配什么?,我们
#include void main() { int a,b,c; for(b = c = 10; a = "- FIGURE?, UMKC,XYZHello Folks,TFy!QJ
我的代码段中的以下代码行被 Sonar 检测为问题。 代码段: final int Pending=1; Sonar 问题: Name 'Pending' must matc
Print name of all activities with neither maximum nor minimum number of participants 我尝试了以下查询,但出现错误:
这个问题在这里已经有了答案: What is this practice called in JavaScript? (7 个回答) 关闭8年前。 (function() { //do stuff
根据任务,我们必须通过 foldr 实现 foldl。通过比较函数签名和 foldl 实现,我得到了以下解决方案: myFoldl :: (a -> b -> a) -> a -> [b] -> a
这个问题在这里已经有了答案: Export an es6 default class inline with definition or at end of file? (1 个回答) 关闭 2 年
据我了解,以下是相同的: Person p{}; // Case 1 Person p = {}; // Case 1.5 我注意到 Person p = Person{}; // Case 2 产生
below i have given a javascript code picture `` can any one help me in this code. what do this code.
我想在标题和正文上搜索全文,并在答案计数上进行过滤。 我阅读了elasticsearch documentation for combining filters并构建了此查询。 "query": {
它是流动的 C 代码中的内存泄漏吗? #include int *a; int main() { a = malloc(sizeof(int)*10); return
这两个声明有什么区别: char (*ptr)[N]; 对比 char ptr[][N]; 谢谢。 最佳答案 (1)声明 char (*ptr)[N]; ptr 是指向大小为 N 的字符数组的指针 下
data II = I Int Int deriving (Show) instance II Show where show I a b = show (a+b) showt.hs:3:2: s
我从 clojuredoc 中阅读了关于 condp 的文档。在文档中我找到了以下代码: (condp 一些 [1 2 3 4] #{0 6 7} :>> 公司 #{4 5 9} :>> 十二月 #{
我是一名优秀的程序员,十分优秀!