QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#175745#7180. Forward-Capturing Pawnsjsannemo#TL 267ms7276kbC++174.5kb2023-09-10 22:21:462023-09-10 22:21:47

Judging History

你现在查看的是最新测评结果

  • [2023-09-10 22:21:47]
  • 评测
  • 测评结果:TL
  • 用时:267ms
  • 内存:7276kb
  • [2023-09-10 22:21:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i,f,t) for (int i = f; i < t; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define trav(a,x) for (auto& a : x)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

struct Pos {
	bool white;
	int pr, pc, wr, wc, br, bc;

	int number() {
		int num = white;
		num = 8 * num + pr;
		num = 8 * num + pc;
		num = 8 * num + wr;
		num = 8 * num + wc;
		num = 8 * num + br;
		num = 8 * num + bc;
		return num;
	}

	void print() {
		cerr << (char)('a' + wc);
		cerr << 1 + wr << " ";
		cerr << (char)('a' + pc);
		cerr << 1 + pr << " ";
		cerr << (char)('a' + bc);
		cerr << 1 + br << " ";
		if (white) cout << "w" << endl;
		else cout << "b" << endl;
	}
};

bool won(const Pos& p) {
	return p.white && p.pr == 7;
}

bool in(int x) {
	return 0 <= x && x < 8;
}

bool valid(const Pos& p) {
	if (!in(p.pr) || !in(p.pc) || !in(p.wr) || !in(p.wc) || !in(p.br) || !in(p.bc)) return false;

	if (p.pr == p.wr && p.pc == p.wc) return false;
	if (p.pr == p.br && p.pc == p.bc) return false;
	if (abs(p.wr - p.br) <= 1 && abs(p.wc - p.bc) <= 1) return false;

	if (!p.white) return true;
	if (p.pr == 7 && (p.br == 7 || p.bc == p.pc)) return false;

	if (p.pc != p.bc) return true;
	if (p.br == p.pr + 1) return false;
	if (p.pr == 1 && p.br == 3) return false;
	return true;
}

bool valid2(const Pos& p) {
	if (!in(p.pr) || !in(p.pc) || !in(p.wr) || !in(p.wc) || !in(p.br) || !in(p.bc)) return false;

	if (p.pr == p.wr && p.pc == p.wc) return false;
	if (abs(p.wr - p.br) <= 1 && abs(p.wc - p.bc) <= 1) return false;

	if (!p.white) return true;
	if (p.pr == p.br && p.pc == p.bc) return true;
	if (p.pr == 7 && (p.br == 7 || p.bc == p.pc)) return false;

	if (p.pc != p.bc) return true;
	if (p.br == p.pr + 1) return false;
	if (p.pr == 1 && p.br == 3) return false;
	return true;
}

vi dx = {-1, -1, -1, 0, 1, 1,  1,  0};
vi dy = {-1,  0,  1, 1, 1, 0, -1, -1};

vector<Pos> before(Pos p) {
	vector<Pos> res;
	if (!p.white) {
		Pos np = p;
		np.white = true;
		// white king
		rep(i,0,8) {
			np.wr = p.wr + dx[i];
			np.wc = p.wc + dy[i];
			if (valid(np)) res.push_back(np);
		}
		np = p;
		np.white = true;
		// white pawn
		np.pr = p.pr - 1;
		if (valid(np)) res.push_back(np);
		if (p.pr == 3 && !(p.wr == 2 && p.wc == p.pc)) {
			np.pr = p.pr - 2;
			if (valid(np)) res.push_back(np);
		}
	} else {
		Pos np = p;
		np.white = false;
		// black king
		rep(i,0,8) {
			np.br = p.br + dx[i];
			np.bc = p.bc + dy[i];
			if (valid(np)) res.push_back(np);
		}
	}
	return res;
}

vector<Pos> gen(Pos p) {
	vector<Pos> res;
	if (p.white) {
		Pos np = p;
		np.white = false;
		// white king
		rep(i,0,8) {
			np.wr = p.wr + dx[i];
			np.wc = p.wc + dy[i];
			if (valid(np)) res.push_back(np);
		}
		np = p;
		np.white = false;
		// white pawn
		np.pr = p.pr + 1;
		if (valid(np)) res.push_back(np);
		if (p.pr == 1 && !(p.wr == 2 && p.wc == p.pc)) {
			np.pr = p.pr + 2;
			if (valid(np)) res.push_back(np);
		}
	} else {
		Pos np = p;
		np.white = true;
		// black king
		rep(i,0,8) {
			np.br = p.br + dx[i];
			np.bc = p.bc + dy[i];
			if (valid2(np)) res.push_back(np);
		}
	}
	return res;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int M = 2 * 8 * 8 * 8 * 8 * 8 * 8;
	vector<bool> whitewin(M);
	queue<Pos> p;


	rep(pc,0,8) {
		rep(wr,0,8)
		rep(wc,0,8)
		rep(br,0,8)
		rep(bc,0,8) {
			Pos pos {true, 7, pc, wr, wc, br, bc};
			if (!valid(pos)) continue;
			whitewin[pos.number()] = true;
			p.push(pos);
		}
	}

	while (!p.empty()) {
		Pos nx = p.front(); p.pop();
		//cerr << "win" << endl;
		if (!nx.white) {
			vector<Pos> b = before(nx);
			for (Pos& pos : b) {
				int num = pos.number();
				if (!whitewin[num]) {
					whitewin[num] = true;
					p.push(pos);
				}
			}
		} else {
			vector<Pos> b = before(nx);
			for (Pos& pos : b) {
				//pos.print();
				int num = pos.number();
				vector<Pos> opt = gen(pos);
				for (Pos& o : opt) {
					int onum = o.number();
					if (!whitewin[onum]) {
						//cerr << "saving pos:" << endl;
						//o.print();
						goto nowin;
					}
				}
				whitewin[num] = true;
				p.push(pos);
nowin:;
			}
		}
		//cerr << endl;
	}

	int N;
	cin >> N;
	rep(i,0,N) {
		string w, p, b, pl;
		cin >> w >> p >> b >> pl;
		Pos pos;
		if (pl == "w") pos.white = true;
		else pos.white = false;
		pos.wr = w[1] - '1';
		pos.br = b[1] - '1';
		pos.pr = p[1] - '1';
		pos.wc = w[0] - 'a';
		pos.bc = b[0] - 'a';
		pos.pc = p[0] - 'a';
		if (whitewin[pos.number()]) cout << "Win" << endl;
		else cout << "Draw" << endl;
	}
}

详细

Test #1:

score: 100
Accepted
time: 267ms
memory: 7276kb

input:

6
a2 d7 e7 w
b6 d7 e7 b
b6 d7 e7 w
b5 a2 b2 w
a6 a2 a4 b
g6 g7 h8 b

output:

Draw
Draw
Win
Win
Draw
Draw

result:

ok 6 lines

Test #2:

score: -100
Time Limit Exceeded

input:

332912
h8 h7 f8 b
h8 h7 f8 w
h8 h7 e8 b
h8 h7 e8 w
h8 h7 d8 b
h8 h7 d8 w
h8 h7 c8 b
h8 h7 c8 w
h8 h7 b8 b
h8 h7 b8 w
h8 h7 a8 b
h8 h7 a8 w
h8 h7 f7 b
h8 h7 f7 w
h8 h7 e7 b
h8 h7 e7 w
h8 h7 d7 b
h8 h7 d7 w
h8 h7 c7 b
h8 h7 c7 w
h8 h7 b7 b
h8 h7 b7 w
h8 h7 a7 b
h8 h7 a7 w
h8 h7 h6 b
h8 h7 h6 w
h8 h7 g...

output:

Draw
Draw
Draw
Win
Win
Win
Win
Win
Win
Win
Win
Win
Draw
Draw
Draw
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Draw
Win
Draw
Win
Draw
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win
Win...

result: