zoukankan      html  css  js  c++  java
  • CF#212 Two Semiknights Meet

    http://codeforces.com/problemset/problem/362/A

    题意:两个棋子同时走田字,问是否能相遇。#没什么卵用。

    开始没注意两个棋子同时走,当相差距离为2时,不可能相遇。

    思路:两个棋子横坐标相差为4或者相差为0时,才可以相遇。纵坐标同理。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<cmath>
     5 using namespace std;
     6 char map[10][10];
     7 int a[10],b[10];
     8 main()
     9 {
    10     int t,i,j,k,s,q,w;
    11     scanf("%d",&t);
    12     while(t--)
    13     {
    14         k=0;s=0;
    15         for(i=0;i<8;i++)
    16         scanf("%s",map[i]);
    17         for(i=0;i<8;i++)
    18         {
    19             for(j=0;j<8;j++)
    20             {
    21                 if(map[i][j]=='K')
    22                 {
    23                     a[k++]=i;
    24                     b[s++]=j;
    25                 }
    26             }
    27         }
    28         q=abs(a[1]-a[0]);
    29         w=abs(b[1]-b[0]);
    30         if((q==4||q==0)&&(w==4||w==0))
    31         printf("YES
    ");
    32         else
    33         printf("NO
    ");
    34 }
    35 }
    View Code

    DFS代码

    注意step%2==0才可能相遇。

     1 char maze[10][10];
     2 int a[2],b[2];
     3 int vis[10][10];
     4 int flag;
     5 void dfs(int x,int y,int step)
     6 {
     7     if(x == a[1] && y == b[1]&& step%2 == 0)
     8     {
     9         flag = 1;
    10         return;
    11     }
    12     if(x < 0 || x >= 8 || y < 0 || y >= 8)
    13         return ;
    14     if(vis[x][y])
    15         return;
    16     vis[x][y] = 1;
    17     if(flag)
    18         return ;
    19     dfs(x - 2,y - 2,step+1);
    20     dfs(x - 2,y + 2,step+1);
    21     dfs(x + 2,y - 2,step+1);
    22     dfs(x + 2,y + 2,step+1);
    23 }
    24 int main() 
    25 {
    26     //freopen("in.txt","r",stdin);
    27     int t;
    28     scanf("%d",&t);
    29     while(t--)
    30     {
    31         int id = 0;
    32         rep(i,0,8)
    33         {
    34             scanf("%s",maze[i]);
    35             rep(j,0,8)
    36             {
    37                 if(maze[i][j] == 'K')
    38                 {
    39                     a[id] = i;
    40                     b[id] = j;
    41                     id++;
    42                 }
    43             }
    44         }
    45         clr(vis);
    46         flag = 0;
    47         dfs(a[0],b[0],0);
    48         if(flag)
    49             cout<<"YES"<<endl;
    50         else
    51             cout<<"NO"<<endl;
    52     }
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    jquery height
    正则表达式的一点奇怪
    this和call
    ajax views
    史上变态的模块
    在php中有什么用
    localhost访问不了
    $.extend abc
    $.extend
    和人沟通的一个要点
  • 原文地址:https://www.cnblogs.com/CrazyBaby/p/5694499.html
Copyright © 2011-2022 走看看