时间限制:0.25s

空间限制:4M

题意:

在一个N*N(N <= 106)的棋盘上,有三个棋子:黑王、白王、黑白王,它们的行走方式一致,每秒向8个方向中的任意一个行走一步.

现在黑王和白王想要相遇(即占据相邻的格子),而黑白王需要阻止它们相遇,即抓住其中一个王(走到其中一个王所在的格子),

黑王和白王行走时总是走最短路径,而黑白王走的路径无法被缩短(即能够走斜向的时候,不会走Z字形),由于黑白王是隐身的,

所以黑王和白王无法看见黑白王的走向,但是他们知道有它存在,当黑王和白王走到黑白王所在的格子时不会有任何事情发生。

问黑白王是否有可能抓住其中一个王,如果有可能,输出最少步数;如果不可能,输出黑王和白王相遇总共的行走步数。行走顺序为白王、黑王、黑白王。


题解:

如果黑王和白王的y方向差值小于x方向差值,那么将三个棋子的x和y值分别交换。保证黑王和白王的y方向差值大于等于x方向差值,由于黑王和白王总是走他们能够相遇的最短路径,所以每次移动一定要保证在y方向朝对方移动一格,所以两个王的行走区域为朝着对方方向的一个“喇叭”形区域。

如图2所示,两个王的y方向坐标差值为T,黑王坐标(Bx, By),白王坐标(Wx, Wy),那么走i步后的黑王的y方向坐标为定值B+ i * bDir,其中bDir为黑王到白王的方向;x方向的可行区间为[B– i, B+ i]和[W– (T-i), W+ (T-i)]的区间交集,即图中两个“喇叭”形区域的交集就是黑王和白王的可行区域,最多T-1步后两人相遇。

图4

黑白王的运动区域更加简单,如图3所示,灰色的方形外框表示经过3步后黑白王有可能在的位置,即一个长度为2i + 1的空心正方形区域。

图5

于是,可以枚举步数,判断黑王和黑白王、白王和黑白王的第i步的区间区域是否有交集,如果一旦出现某个王在第i步的时候和黑白王的正方形区域有交集,则表明黑白王有可能在第i步抓住其中一个王,否则当2i >= T-1的时候还没有交集,表明黑王和白王已经相遇,总步数为T-1。总复杂度O(N)。

代码:

 #include<stdio.h>
#include<algorithm>
using namespace std;
int n, d, st, ans;
struct Point
{
int x, y;
Point() {}
Point (int x, int y) : x (x), y (y) {}
void scan() { scanf ("%d%d", &x, &y); }
void print() { printf ("%d %d\n", x, y); }
};
Point b, w, bw, bl, br, wl, wr;
char res[2][5] = {"NO", "YES"};
int dis (Point a, Point b)
{
return max (abs (a.x - b.x), abs (a.y - b.y) );
}
int fail (Point k)
{
int d0 = dis (bw, Point (k.x, k.y) );
if (d0 > st) return 1;
else if (d0 < st) return -1;
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen ("data.in", "r", stdin);
#endif
scanf ("%d", &n);
b.scan(), w.scan(), bw.scan();
if (abs (b.x - w.x) > abs (b.y - w.y) ) swap (b.x, b.y), swap (w.x, w.y), swap (bw.x, bw.y);
if (b.y < w.y) swap (b, w);
d = dis (b, w) - 1;
bl = br = b, wl = wr = w;
//b.print(),w.print(),bw.print(),printf("\n");
for (st = 0; st < (d + 1) / 2 && fail (bl) *fail (br) > 0 && fail (wl) *fail (wr) > 0; st++)
{
if (bl.x > 1 && w.x - bl.x + 1 < bl.y - w.y) bl.x--;
else if (w.x - bl.x + 1 > bl.y - w.y) bl.x++;
if (br.x < n && br.x - w.x + 1 < br.y - w.y) br.x++;
else if (br.x - w.x + 1 > br.y - w.y) br.x--;
bl.y--, br.y--;
if (wl.x > 1 && b.x - wl.x + 1 < b.y - wl.y) wl.x--;
else if (b.x - wl.x + 1 > b.y - wl.y) wl.x++;
if (wr.x < n && wr.x - b.x + 1 < b.y - wr.y) wr.x++;
else if (wr.x - b.x + 1 > b.y - wr.y) wr.x--;
wl.y++, wr.y++;
//bl.print(),br.print(),wl.print(),wr.print();
}
ans = (st < (d + 1) / 2);
printf ("%s\n%d\n", res[ans], ans ? st : d);
return 0;
}

  

SGU 147.Black-white king的更多相关文章

  1. sgu 147. Black-white king 思路 坑 难度:1

    147. Black-white king time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard i ...

  2. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  3. Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) A. The King's Race

    http://codeforces.com/contest/1075/problem/A On a chessboard with a width of nn and a height of nn, ...

  4. 【leetcode】1222. Queens That Can Attack the King

    题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...

  5. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. [SQL Server] 特殊字符、上标、下标处理

    今天遇到一个问题是往 SQL Server 中导入像m².m³这样的单位数据,可是在 SQL Server 中查看到的都是 m2.m3,于是在网上查了一下资料,顺便摘录下来供日后查阅. 一  Wind ...

  7. Codeforces Round #379 (Div. 2) A B C D 水 二分 模拟

    A. Anton and Danik time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #379 (Div. 2) 总结分享

    前言 初入acm的新手,打算在cf混.这几天没有比赛,就做了个最新的Virtual participation.虽然说div2比较简单,但还是被虐得体无完肤...Orz.两个小时,共6道题.最后只AC ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

随机推荐

  1. Samples DataBind FastJson循环引用问题

    Fastjson full support databind, it's simple to use. Encode import com.alibaba.fastjson.JSON; Group g ...

  2. HDNOIP201206施工方案

    HDNOIP201206施工方案 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 c国边防军在边境某处的阵地是由n个地堡组成的. ...

  3. hg 证书验证失败

    hg clone https://bitbucket.org/pygame/pygame 出现abort: error: _ssl.c:504: error:14090086:SSL routines ...

  4. 【Mongous】

    amark/mongous Mongous - 一个轻量级的nodejs mongodb驱动 mongous,是我不够懂你吗?关于mongous不支持objectId查询 mongous 不需要 _i ...

  5. Light OJ 1025 - The Specials Menu(区间DP)

    题目大意:     给你一个字符串,问有多少种方法删除字符,使得剩下的字符是回文串. 有几个规定: 1.空串不是回文串 2.剩下的字符位置不同也被视为不同的回文串.如:AA有三种回文串 A, A, A ...

  6. iOS应用架构浅谈

    (整理至http://www.cocoachina.com/ios/20150414/11557.html) 缘由 从事iOS工作一年多了,主要从事QQ钱包SDK开发和财付通app维护,随着对业务的慢 ...

  7. 根据标点符号分行,StringBuilder的使用;将字符串的每个字符颠倒输出,Reverse的使用

    一:根据标点符号分行,上图,代码很简单 二:代码 using System; using System.Collections.Generic; using System.ComponentModel ...

  8. Nodejs in Visual Studio Code 13.构建单页应用Scrat示例挖一挖

    1.开始 Scrat作者说要搞个很碉堡的示例,果然就搞出来了,如果要学习并使用Scrat,可以从官方示例开始,简直太方便了. 2.Scrat示例 目录 component_modules : 公共组件 ...

  9. cocos2d的框架思路

    这是我第一次写cocos的框架思路哈,虽然只是写完了一个程序,按理来说应该再多写一些,多积累一些经验了再来写这个框架的构成,但是我觉得还是把我这次写代码的所有想法先记下来哈,等到以后继续写cocos的 ...

  10. ios-简单算法

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...