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\n");
32         else
33         printf("NO\n");
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

 

转载于:https://www.cnblogs.com/CrazyBaby/p/5694499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值