zoukankan      html  css  js  c++  java
  • A. Two Semiknights Meet DFS

    A. Two Semiknights Meet
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.

    Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.

    Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.

    Please see the test case analysis.

    Input

    The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.

    Output

    For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.

    Sample test(s)
    input
    2
    ........
    ........
    ......#.
    K..##..#
    .......#
    ...##..#
    ......#.
    K.......

    ........
    ........
    ..#.....
    ..#..#..
    ..####..
    ...##...
    ........
    ....K#K#
    output
    YES
    NO
    Note

    Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (27). The semiknight from square (41) goes to square (23) and the semiknight goes from square (81) to square (63). Then both semiknights go to (45) but this square is bad, so they move together to square (27).

    On the second board the semiknights will never meet.

    char maze[10][10];
    int a[2],b[2];
    int vis[10][10];
    int flag;
    void dfs(int x,int y,int step)
    {
        if(x == a[1] && y == b[1]&& step%2 == 0)
        {
            flag = 1;
            return;
        }
        if(x < 0 || x >= 8 || y < 0 || y >= 8)
            return ;
        if(vis[x][y])
            return;
        vis[x][y] = 1;
        if(flag)
            return ;
        dfs(x - 2,y - 2,step+1);
        dfs(x - 2,y + 2,step+1);
        dfs(x + 2,y - 2,step+1);
        dfs(x + 2,y + 2,step+1);
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int id = 0;
            rep(i,0,8)
            {
                scanf("%s",maze[i]);
                rep(j,0,8)
                {
                    if(maze[i][j] == 'K')
                    {
                        a[id] = i;
                        b[id] = j;
                        id++;
                    }
                }
            }
            clr(vis);
            flag = 0;
            dfs(a[0],b[0],0);
            if(flag)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    javascript作用域
    [翻译]Review——How JavaScript works:The building blocks of Web Workers
    [转]Shared——回调函数是什么
    由上一个血案引发的关于property和attribute关系的思考
    RN canvas画布大小之谜
    [转]Tips——Chrome DevTools
    Algorithm——无重复字符的最长子串
    [翻译]Review——The Inner Workings Of Virtual DOM
    Tips——单页面内的多重跳转路由使用
    [转]Shared——Javascript中的call详解
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3426053.html
Copyright © 2011-2022 走看看