CodeForces362A Two Semiknights Meet(dfs)

原创 2016年06月01日 21:30:00

CodeForces362A Two Semiknights Meet

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description

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 Input

Input

2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......

........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#

Output

YES
NO

Hint

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 (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).

On the second board the semiknights will never meet.

链接在这:题目链接


两个棋子k(其实就是象棋里的象或相),同时走看能否相遇,注意#可以经过,但不能在#处相遇,
dfs解决

代码:

#include<stdio.h>
#include<string.h>

char map[10][10];
int vis1[10][10];
int vis2[10][10];
int dxy[4][2]={2,2,2,-2,-2,2,-2,-2};
int x1,y1,x2,y2;

void dfs1 (int x,int y,int step)
{
    if(x<0||x>=8||y<0||y>=8)
        return;
    else if(map[x][y]=='K'||map[x][y]=='.'||map[x][y]=='#')
    {
        if(vis1[x][y]==0)
            vis1[x][y]=step;
        else
        {
            if(vis1[x][y]>step)
                vis1[x][y]=step;
            else
                return;
        }
        for(int i=0;i<4;i++)
        {
            int nx=x+dxy[i][0];
            int ny=y+dxy[i][1];
            dfs1(nx,ny,step+1);
        }
    }
}

void dfs2 (int x,int y,int step)
{
    if(x<0||x>=8||y<0||y>=8)
        return;
    else if(map[x][y]=='K'||map[x][y]=='.'||map[x][y]=='#')
    {
        if(vis2[x][y]==0)
            vis2[x][y]=step;
        else
        {
            if(vis2[x][y]>step)
                vis2[x][y]=step;
            else
                return;
        }
        for(int i=0;i<4;i++)
        {
            int nx=x+dxy[i][0];
            int ny=y+dxy[i][1];
            dfs2(nx,ny,step+1);
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int flag=0;
        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        for(int i=0;i<8;i++)
            scanf("%s",map[i]);
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                if(map[i][j]=='K')
                {
                    if(flag==0)
                        x1=i,y1=j,flag=1;
                    else
                        x2=i,y2=j;
                }
            }
        }
        dfs1(x1,y1,1);
        dfs2(x2,y2,1);
        int ans=0;
        int max1=-1,max2=-1;
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                if(map[i][j]!='#'&&vis1[i][j]!=0&&vis2[i][j]!=0&&(vis1[i][j]-vis2[i][j])%2==0)
                {
                    ans=1;
                }
                if(max1<vis1[i][j])
                    max1=vis1[i][j];
                if(max2<vis2[i][j])
                    max2=vis2[i][j];
            }
        }
        if(max1<=1||max2<=1)
            ans=0;
        if(ans)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 362A Two Semiknights Meet

题目链接:http://codeforces.com/problemset/problem/362/A 题目大意:
  • Firwaless
  • Firwaless
  • 2013年11月15日 20:01
  • 929

ZCMU-1342-Two Semiknights Meet

1342: Two Semiknights Meet Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 56  Solved: 17 [Submit][S...
  • ZCMUCZX
  • ZCMUCZX
  • 2017年01月12日 13:05
  • 142

Codeforecs-362A Two Semiknights Meet(dfs)

题意:骑士只能在8*8的方格中走田字格,就像中国象棋中的“相"一样。在给出n个...
  • show999hao
  • show999hao
  • 2013年11月19日 16:46
  • 817

A. Two Semiknights Meet

解题说明:此题就需要找到两个K的位置,然后加以判断即可。 #include #include int main() { int i, j, n, flag; char a[9][9]; ch...
  • jj12345jj198999
  • jj12345jj198999
  • 2013年12月13日 21:34
  • 981

A - Two Semiknights Meet

Description A boy Petya loves chess very much. He even came up with a chess piece of his own, a s...
  • viphong
  • viphong
  • 2015年07月24日 02:23
  • 385

CodeForces - 362A Two Semiknights Meet

A. Two Semiknights Meet time limit per test 1 second memory limit per test 256 megabytes...
  • Lngxling
  • Lngxling
  • 2017年12月13日 19:47
  • 27

CodeForces 362A - Two Semiknights Meet

A boy Petya loves chess very much. He even came up with a chess piece ofhis own, a semiknight. The s...
  • leifjacky
  • leifjacky
  • 2015年07月27日 12:48
  • 286

Two Semiknights Meet CodeForces - 362A

题意: 两个K要开会,#格子可以走,不可以做开会地点。 两个K无时无刻都在移动,不可以停留 思路: 记录K的位置的奇偶性,存图比对...
  • qq_33951440
  • qq_33951440
  • 2017年03月01日 21:19
  • 102

codeforces 362A C - Two Semiknights Meet

一直卡在测试用例七,可能是因为没有判断时间,两个骑士要在同一时刻到达某一点 用bfs 搜出每个骑士的各个可能地点,标记时间 如果某处不是'#',且两个骑士相差的时间%2 == 0 ,就说明存在 ...
  • zhuanshunzhe
  • zhuanshunzhe
  • 2017年02月28日 21:57
  • 78

77 ~And, two stars meet again~ RC汉化补丁[BUG修正]

【遊戲名稱/Name】:77 ~And, two stars meet again~ 【遊戲廠商/Company】:Whirlpoor 【發售日期/Sale date】:2009/07/...
  • luozhuang
  • luozhuang
  • 2011年09月30日 11:31
  • 20227
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces362A Two Semiknights Meet(dfs)
举报原因:
原因补充:

(最多只允许输入30个字)