思路:
注意到:题目样例二解释为诈骗,对于曹操来说,最优的方法就是朝一个方向走到头。
让我们来证明一下。即如果曹操经常换方向走,每个伏兵最差情况下都可以跟着曹操的方向走。如曹操往上走,其他伏兵跟着往上走,这样总体的相对差距是不变的。
但是伏兵并不会走最差策略,而是会走距离曹操最远的方向去一步步逼近曹操。所以如果曹操瞎走,每一步面临的情况都会比上一步更劣。所以曹操最优方案就是朝一个方向走。
接下来,我们只需要判断曹操朝那个方向走能逃出去。对于每个伏兵,他只能拦住距离曹操较近的那一个方向。
例如
f**
***
***
***
**c我们只看向上和向左的方向,容易发现,伏兵只能拦住距离曹操最近的向上的方向。
特殊的,如果伏兵距离曹操两个方向相等,那他每一步都能去拦曹操走的那个方向,即和曹操保持正方形对角线的形式去走。这样两个方向都能拦住。
例如
f** *f* f** ***
*** -> **c 或 *** -> f**
**c *** **c *c*至此,代码思路就说完了。
#include<bits/stdc++.h>
using namespace std;
int n,m,a,x,y,b,c;
bool 向左,向上,向右,向下;
int main(){
cin>>n>>m>>a>>x>>y;
for(int i=1;i<=a;i++){
cin>>b>>c;
if(abs(b-x)<=abs(c-y)){
if(c<y) 向上=true;
if(c>=y) 向下=true;
}
if(abs(b-x)>=abs(c-y)){
if(b<x) 向左=true;
if(b>=x) 向右=true;
}
}
if(向左&&向上&&向右&&向下) cout<<"No";
else cout<<"Yes";
return 0;
}以下为AI写的代码,我的评价是不如人类智慧
文心 235 行
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
// 检查某个方向是否安全
// dir: 0=Up, 1=Down, 2=Left, 3=Right
bool check_direction(ll n, ll m, ll x, ll y, const vector<pair<ll,ll>>& enemies, int dir) {
ll steps; // 曹操到达边界需要的步数
ll target_x, target_y; // 边界上的目标点(直线逃跑的终点)
if (dir == 0) { // Up
steps = x - 1;
target_x = 1; target_y = y;
} else if (dir == 1) { // Down
steps = n - x;
target_x = n; target_y = y;
} else if (dir == 2) { // Left
steps = y - 1;
target_x = x; target_y = 1;
} else { // Right
steps = m - y;
target_x = x; target_y = m;
}
// 如果步数为0,说明已经在边界,直接成功
if (steps <= 0) return true;
for (const auto& e : enemies) {
ll b = e.first;
ll c = e.second;
// 快速剪枝:如果伏兵到终点的曼哈顿距离都大于曹操的步数,
// 那么伏兵肯定追不上曹操到达终点。
// 但这不够,因为伏兵可能在半路拦截。
// 我们需要判断是否存在 t (1 <= t <= steps),使得伏兵能在 t 步内到达曹操在 t 时刻的位置。
// 曹操在 t 时刻的位置 P_t:
// Up: (x-t, y)
// Down: (x+t, y)
// Left: (x, y-t)
// Right: (x, y+t)
// 伏兵 E(b,c) 在 t 步内到达 P_t 的条件:
// 这是一个动态规划或方程求解问题。
// 但我们可以利用单调性。
// 对于向上跑:
// 曹操位置: (x-t, y).
// 伏兵需要到达 (x-t, y).
// 曼哈顿距离 D(t) = |x-t-b| + |y-c|.
// 如果 D(t) <= t,且满足奇偶性和停滞规则,则可能被抓住。
// 由于停滞规则的存在,实际所需步数 >= D(t)。
// 所以如果 D(t) > t,则绝对安全。
// 我们需要检查是否存在 t in [1, steps] 使得 D(t) <= t 是可能的。
// 更严格地说,如果 min_{t} (D(t) - t) <= 0,则有危险。
ll min_diff = 1e18;
// 为了简化,我们只检查最危险的点。
// 通常最危险的点是伏兵投影到路径上的点。
// 这里使用一个充分条件:
// 如果对于所有 t in [1, steps],都有 |x_t - b| + |y_t - c| > t,则安全。
// 这等价于 min_t (|x_t - b| + |y_t - c| - t) > 0.
// 以 Up 为例: f(t) = |x-t-b| + |y-c| - t.
// |y-c| 是常数 C_y.
// f(t) = |x-b-t| + C_y - t.
// 令 K = x-b. f(t) = |K-t| + C_y - t.
// 如果 K >= 0 (伏兵在曹操上方或同高):
// 当 t <= K: f(t) = K-t + C_y - t = K + C_y - 2t. 最小值在 t=K 处? 不,t越大越小。
// 当 t > K: f(t) = t-K + C_y - t = C_y - K.
// 如果 K < 0 (伏兵在曹操下方):
// t >= 1 > K. f(t) = t-K + C_y - t = C_y - K = C_y + |K|.
// 我们发现,对于直线运动,判断是否被拦截有一个简单的结论:
// 伏兵能拦截,当且仅当:
// 1. 伏兵在曹操逃跑方向的“前方”或“侧方”。
// 2. 伏兵的横向偏差 |dy| 足够小。
// 3. 伏兵的纵向位置允许它在曹操到达之前赶到。
// 由于精确推导复杂,且容易出错,对于 10^6 数据,
// 如果上述简单曼哈顿判断无法确定,通常意味着需要更复杂的逻辑。
// 但在此题中,大部分情况可以通过曼哈顿距离剪枝。
// 让我们尝试一个更宽松的判定:
// 如果伏兵到路径上最近点的曼哈顿距离 <= 曹操到达该点的时间,则视为危险。
ll dist_y = abs(y - c); // 横向固定距离
// 计算伏兵在纵向轴上投影到路径上的最近点所需的时间
// 以及曹操到达该点的时间
bool threatened = false;
if (dir == 0) { // Up: x decreases
// Path x: x-1, x-2, ... 1
// Enemy b.
// Closest longitudinal point is min(max(1, b), x-1)?
// Actually, we iterate t.
// Condition: |x-t-b| + dist_y <= t => |x-b-t| <= t - dist_y
// This requires t >= dist_y.
// And |x-b-t| <= t - dist_y.
if (steps >= dist_y) {
// Check if there exists t in [dist_y, steps] satisfying the inequality.
// Case 1: x-b-t >= 0 => t <= x-b.
// x-b-t <= t-dist_y => x-b+dist_y <= 2t => t >= (x-b+dist_y)/2.
// So we need max(dist_y, ceil((x-b+dist_y)/2)) <= min(steps, x-b).
// Case 2: x-b-t < 0 => t > x-b.
// -(x-b-t) <= t-dist_y => t-x+b <= t-dist_y => b <= dist_y?
// Wait, t-x+b <= t-dist_y => b+x? No.
// t - (x-b) <= t - dist_y => -(x-b) <= -dist_y => x-b >= dist_y => b <= x-dist_y.
// If this holds, then ANY t > x-b works?
// Let's re-evaluate: |K-t| <= t - C.
// If t >= K: t-K <= t-C => -K <= -C => K >= C => x-b >= dist_y.
// If this is true, then for all t >= max(K, C), it is threatened.
// Since we have t up to steps, if steps >= max(x-b, dist_y) and x-b >= dist_y, then threatened.
ll K = x - b;
ll C = dist_y;
// Check if interception is possible
if (K >= C) {
// Interception possible for t >= K.
if (steps >= K) threatened = true;
} else {
// K < C.
// Need t >= (K+C)/2 and t <= K? No, t>=K is case 1.
// If K < C, then for t >= K, condition is K >= C (False).
// So we must look at t < K?
// If t < K: K-t <= t-C => K+C <= 2t => t >= (K+C)/2.
// So we need exist t such that (K+C)/2 <= t < K.
// This requires (K+C)/2 < K => C < K. Contradiction.
// So if K < C, no solution in t >= K.
// What about t < K?
// We established t >= (K+C)/2.
// So we need ceil((K+C)/2) < K.
// And also t >= C (from t >= dist_y).
// So if ceil((K+C)/2) <= min(steps, K-1) AND ceil((K+C)/2) >= C.
ll min_t = (K + C + 1) / 2; // Ceiling division
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 1) { // Down: x increases
// Path x: x+1, ... n
// K = b - x.
// Similar logic.
ll K = b - x;
ll C = dist_y;
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 2) { // Left: y decreases
ll K = y - c;
ll C = abs(x - b);
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 3) { // Right: y increases
ll K = c - y;
ll C = abs(x - b);
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
if (threatened) {
return false; // This direction is blocked by this enemy
}
}
return true; // No enemy blocked this direction
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, m, a;
if (!(cin >> n >> m >> a)) return 0;
ll x, y;
cin >> x >> y;
vector<pair<ll,ll>> enemies(a);
for (ll i = 0; i < a; i++) {
cin >> enemies[i].first >> enemies[i].second;
}
bool can_escape = false;
// Check all 4 directions
if (check_direction(n, m, x, y, enemies, 0) || // Up
check_direction(n, m, x, y, enemies, 1) || // Down
check_direction(n, m, x, y, enemies, 2) || // Left
check_direction(n, m, x, y, enemies, 3)) { // Right
can_escape = true;
}
cout << (can_escape ? "Yes" : "No") << endl;
return 0;
}
豆包 141 行
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
// 检查某个方向是否安全
// dir: 0=Up, 1=Down, 2=Left, 3=Right
bool check_direction(ll n, ll m, ll x, ll y, const vector<pair<ll,ll>>& enemies, int dir) {
ll steps; // 曹操到达边界需要的步数
ll target_x, target_y; // 边界上的目标点(直线逃跑的终点)
if (dir == 0) { // Up
steps = x - 1;
target_x = 1; target_y = y;
} else if (dir == 1) { // Down
steps = n - x;
target_x = n; target_y = y;
} else if (dir == 2) { // Left
steps = y - 1;
target_x = x; target_y = 1;
} else { // Right
steps = m - y;
target_x = x; target_y = m;
}
// 如果步数为0,说明已经在边界,直接成功
if (steps <= 0) return true;
for (const auto& e : enemies) {
ll b = e.first;
ll c = e.second;
ll min_diff = 1e18;
ll dist_y = abs(y - c); // 横向固定距离
bool threatened = false;
if (dir == 0) {
if (steps >= dist_y) {
ll K = x - b;
ll C = dist_y;
// Check if interception is possible
if (K >= C) {
// Interception possible for t >= K.
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2; // Ceiling division
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 1) { // Down: x increases
ll K = b - x;
ll C = dist_y;
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 2) { // Left: y decreases
ll K = y - c;
ll C = abs(x - b);
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
else if (dir == 3) { // Right: y increases
ll K = c - y;
ll C = abs(x - b);
if (steps >= C) {
if (K >= C) {
if (steps >= K) threatened = true;
} else {
ll min_t = (K + C + 1) / 2;
if (min_t < K && min_t <= steps && min_t >= C) {
threatened = true;
}
}
}
}
if (threatened) {
return false; // This direction is blocked by this enemy
}
}
return true; // No enemy blocked this direction
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, m, a;
if (!(cin >> n >> m >> a)) return 0;
ll x, y;
cin >> x >> y;
vector<pair<ll,ll>> enemies(a);
for (ll i = 0; i < a; i++) {
cin >> enemies[i].first >> enemies[i].second;
}
bool can_escape = false;
// Check all 4 directions
if (check_direction(n, m, x, y, enemies, 0) || // Up
check_direction(n, m, x, y, enemies, 1) || // Down
check_direction(n, m, x, y, enemies, 2) || // Left
check_direction(n, m, x, y, enemies, 3)) { // Right
can_escape = true;
}
cout << (can_escape ? "Yes" : "No") << endl;
return 0;
}
deepseek 代码一 79 行
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, a;
cin >> n >> m >> a;
int x, y;
cin >> x >> y;
vector<int> fx(a), fy(a);
for (int i = 0; i < a; ++i) {
cin >> fx[i] >> fy[i];
}
// 四个方向:上、下、左、右
int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int d = 0; d < 4; ++d) {
int cx = x, cy = y;
vector<int> cfx = fx, cfy = fy;
bool ok = true;
while (true) {
// 曹操先走
cx += dirs[d][0];
cy += dirs[d][1];
bool at_edge = (cx == 1 || cx == n || cy == 1 || cy == m);
// 检查曹操移动后是否直接碰到伏兵
for (int i = 0; i < a; ++i) {
if (cfx[i] == cx && cfy[i] == cy) {
ok = false;
break;
}
}
if (!ok) break;
// 伏兵移动
for (int i = 0; i < a; ++i) {
int ddx = cfx[i] - cx;
int ddy = cfy[i] - cy;
int adx = abs(ddx);
int ady = abs(ddy);
if (adx > ady) {
if (ddx > 0) --cfx[i];
else if (ddx < 0) ++cfx[i];
} else if (ady > adx) {
if (ddy > 0) --cfy[i];
else if (ddy < 0) ++cfy[i];
}
// adx == ady 时按兵不动
}
// 检查伏兵移动后是否抓住曹操
for (int i = 0; i < a; ++i) {
if (cfx[i] == cx && cfy[i] == cy) {
ok = false;
break;
}
}
if (!ok) break;
// 曹操已到达边界且未被抓住,下一回合可以直接走出矩阵
if (at_edge) {
cout << "Yes\n";
return 0;
}
}
}
cout << "No\n";
return 0;
}deepseek 代码二 66 行
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a;
cin >> n >> m >> a;
int x, y;
cin >> x >> y;
bool up = true, down = true, left = true, right = true;
for (int i = 0; i < a; ++i) {
int b, c;
cin >> b >> c;
// 向上逃跑:威胁来自上方的伏兵 (b < x)
if (up && b < x) {
int u0 = x - b;
int v0 = abs(y - c);
if (u0 >= v0) {
int t = (u0 + v0 + 1) / 2;
if (t <= x - 1) up = false;
}
}
// 向下逃跑:威胁来自下方的伏兵 (b > x)
if (down && b > x) {
int u0 = b - x;
int v0 = abs(y - c);
if (u0 >= v0) {
int t = (u0 + v0 + 1) / 2;
if (t <= n - x) down = false;
}
}
// 向左逃跑:威胁来自左方的伏兵 (c < y)
if (left && c < y) {
int u0 = y - c;
int v0 = abs(x - b);
if (u0 >= v0) {
int t = (u0 + v0 + 1) / 2;
if (t <= y - 1) left = false;
}
}
// 向右逃跑:威胁来自右方的伏兵 (c > y)
if (right && c > y) {
int u0 = c - y;
int v0 = abs(x - b);
if (u0 >= v0) {
int t = (u0 + v0 + 1) / 2;
if (t <= m - y) right = false;
}
}
}
if (up || down || left || right)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}