zoukankan      html  css  js  c++  java
  • CF1033A. King Escape的题解

    题目链接:http://codeforces.com/problemset/problem/1033/A

    Problem Description

    Alice and Bob are playing chess on a huge chessboard with dimensions n×nn×n. Alice has a single piece left — a queen, located at (ax,ay)(ax,ay), while Bob has only the king standing at (bx,by)(bx,by). Alice thinks that as her queen is dominating the chessboard, victory is hers.

    But Bob has made a devious plan to seize the victory for himself — he needs to march his king to (cx,cy)(cx,cy) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.

    Bob will win if he can move his king from (bx,by)(bx,by) to (cx,cy)(cx,cy) without ever getting in check. Remember that a king can move to any of the 88 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.

    Find whether Bob can win or not.

    Input

    The first line contains a single integer nn (3n10003≤n≤1000) — the dimensions of the chessboard.

    The second line contains two integers axax and ayay (1ax,ayn1≤ax,ay≤n) — the coordinates of Alice's queen.

    The third line contains two integers bxbx and byby (1bx,byn1≤bx,by≤n) — the coordinates of Bob's king.

    The fourth line contains two integers cxcx and cycy (1cx,cyn1≤cx,cy≤n) — the coordinates of the location that Bob wants to get to.

    It is guaranteed that Bob's king is currently not in check and the target location is not in check either.

    Furthermore, the king is not located on the same square as the queen (i.e. axbxax≠bx or aybyay≠by), and the target does coincide neither with the queen's position (i.e. cxaxcx≠ax or cyaycy≠ay) nor with the king's position (i.e. cxbxcx≠bxor cybycy≠by).

    Output

    Print "YES" (without quotes) if Bob can get from (bx,by)(bx,by) to (cx,cy)(cx,cy) without ever getting in check, otherwise print "NO".

    You can print each letter in any case (upper or lower).

    Examples
    Input
    8
    4 4
    1 3
    3 1
    Output
    YES
    Input
    8
    4 4
    2 3
    1 6
    Output
    NO
    Input
    8
    3 5
    1 2
    6 1
    Output
    NO
    Note

    In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.

    In the first case, the king can move, for instance, via the squares (2,3)(2,3)and (3,2)(3,2). Note that the direct route through (2,2)(2,2) goes through check.

    In the second case, the queen watches the fourth rank, and the king has no means of crossing it.

    In the third case, the queen watches the third file.

    思路:1.深度搜索。注意国王的移动规则。有八个方向。
         2.只需判断起点与终点是否在同一象限即可
    解法1.(缺省)
     1 #include<stdio.h>
     2 int move[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
     3 int i,j,k,t;
     4 int goal_x,goal_y;
     5 int n;
     6 int map[1010][1010];
     7 int laft;
     8 int x1,x2,y2,y1;
     9 void dfs(int x,int y)
    10 {
    11     if(x==goal_x&&y==goal_y)
    12     {
    13         laft=1;
    14         return;
    15     }
    16     int move_x,move_y;
    17     for(int k=0;k<8;k++)
    18     {
    19         move_x=x+move[k][0];
    20         move_y=y+move[k][1];
    21         if(move_x>n||move_x<1||move_y>n||move_y<1)
    22         {
    23             continue;
    24         }
    25         if(move_x+move_y!=x1+y1&&move_x-move_y+n!=x1-y1+n&&move_x!=x1&&move_y!=y1&&map[move_x][move_y]==0)
    26         {
    27             map[move_x][move_y]=1;
    28             dfs(move_x,move_y);
    29         }
    30     }
    31     return;
    32 }     
    33 int main()
    34 {
    35     scanf("%d",&n);
    36     scanf("%d%d",&x1,&y1);
    37     scanf("%d%d",&x2,&y2);
    38     scanf("%d%d",&goal_x,&goal_y);
    39     dfs(x2,y2);
    40     if(laft==0)
    41         printf("NO
    ");
    42     else if(laft==1)
    43         printf("YES
    ");        
    44 }
    View Code(深搜)
    解法2.由题意可知女王(ax,ay)所在的那一行和那一列国王(bx,by)都不能通过.这样我们就可以令(ax,ay)为原点建立平面直角坐标系,国王(bx,by)只能在其所在的象限移动,所以我们只需判断(bx,by)和(cx,cy)是否在同一象限即可。
    深度搜索也可以解出来,不过像我这样快多了。代码如下:
     1 #include<stdio.h>
     2 int map[1010][1010];
     3 int main()
     4 {
     5     int n,x1,y1,x2,y2,x,y,flag=0;
     6     scanf("%d",&n);
     7     scanf("%d%d",&x1,&y1);
     8     scanf("%d%d",&x2,&y2);
     9     scanf("%d%d",&x,&y);
    10     if(x2<x1&&y2>y1&&x<x1&&y>y1||x2<x1&&y2<y1&&x<x1&&y<y1||x2>x1&&y2>y1&&x>x1&&y>y1||x2>x1&&y2<y1&&x>x1&&y<y1)
    11     //判断是否在同一象限即可 
    12     flag=1;
    13     if(flag)
    14     printf("YES
    ");
    15     else
    16     printf("NO
    ");
    17     return 0;
    18 }
    View Code(2333)
  • 相关阅读:
    安装nodejs和yarn(配置淘宝源)
    适用于 Linux 的 Windows 子系统没有已安装的分发版
    selenium定位元素click时报错
    dubbo从入门到实战(转)
    SpringBoot整合JPA简单介绍
    办公自动化路上的异化
    邮箱黑名单:如何查看邮件IP是否被列入黑名单,及如何删除
    邮箱黑名单(1):
    Vmware挂载san存储_vSphere 6.x 共享存储LUN丢失分区表修复(精华)
    AD中FSMO五大角色的介绍及操作
  • 原文地址:https://www.cnblogs.com/Mingusu/p/10306284.html
Copyright © 2011-2022 走看看