HOJ---11491 A Knight and a Queen[超大数组+BFS]

 

A Knight and a Queen
Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:32768KB
Total submit users: 52, Accepted users: 42
Problem 11491 : No special judgement
Problem description
Marge walks in the house and finds Homer staring at a chessboard with a knight and a queen on it.

Marge: Homer, are you OK? What’s up with these intellectual endeavors of yours recently?
Homer: Oh, I’m OK. And I’m not playing chess anyway.
Marge: So what are you doing?
Homer: I’m imagining that I’m a knight.
Marge: Yeah right.
Homer: Well, a knight sitting on the chessboard. And I imagine you are my queen.
Marge: I like that.
Homer: Of course you are sitting on the chessboard too. Now I wonder if I can reach the square you’re sitting on in 16 or fewer moves.
Marge: So can you?
Homer: I’m still trying to figure it out.

  Write a program that, when given a knight and a queen on an n by n chessboard, finds if the knight can reach the queen in m or fewer than moves. One ”move” for a knight is defined as 2 squares in one direction, then one square in a perpendicular direction. Knights cannot move along diagonals. For example, if n = 8, kx = 3, and ky = 5, then possible positions for the knight after one move given in (kx, ky) are: (1,6), (1,4), (2,7), (2,3), (4,7), (4,3), (5,6), and (5,4).



Input
For each test the input consists of 3 lines. The first line contains 2 numbers: n and m. n is the dimension of the board, and m is the number of moves the knight is allowed to do.
  The second line contains two numbers: kx and ky, s.t. 1 ≤ kx ≤ n, 1 ≤ ky ≤ n. These two numbers indicate the position of the knight on the board.
  The last line again contains two numbers: qx and qy, s.t. 1 ≤ qx ≤ n, 1 ≤ qy ≤ n. They indicate the position of the queen on the board.

Output
  If the queen is reachable by the knight with m or less moves, output should contain the following string on a single line ending with a newline:
Knight can reach Queen within m moves!

  If the queen is not reachable by the knight with m or less moves, output should contain the following string on a single line ending with a newline:
Knight cannot reach Queen within m moves!

  In the above, m should be the actual value from the input.   No test case will have the queen and the knight sitting on the same square. You can further assume that n ≤ 1000000,m ≤ 256. Your program should finish in less than one minute.

Sample Input
8 2
3 5
7 4

8 3
3 5
7 4
Sample Output
Knight cannot reach Queen within 2 moves!
Knight can reach Queen within 3 moves!
Problem Source
2008 Maryland High-school Programming Contest

 

 

 

 

因为数组大小很大,所以要把起点和终点平移,然后注意边界就好。

code:

  1 /*
  2 16 9
  3 15 2
  4 2 15
  5 Knight cannot reach Queen within 9 moves!
  6 16 10
  7 15 2
  8 2 15
  9 Knight can reach Queen within 10 moves!
 10 8 3
 11 4 2
 12 4 8
 13 Knight cannot reach Queen within 3 moves!
 14 1000000 256
 15 200000 800000
 16 800000 200000
 17 Knight cannot reach Queen within 256 moves!
 18 1000000 256
 19 350000 350000
 20 650000 650000
 21 Knight cannot reach Queen within 256 moves!
 22 384 256
 23 1 1
 24 384 384
 25 Knight can reach Queen within 256 moves!
 26 96 64
 27 1 1
 28 96 96
 29 Knight can reach Queen within 96 moves!
 30 */
 31 
 32 #include <iostream>   
 33 #include <iomanip>   
 34 #include <fstream>   
 35 #include <sstream>   
 36 #include <algorithm>   
 37 #include <string>   
 38 #include <set>   
 39 #include <utility>   
 40 #include <queue>   
 41 #include <stack>   
 42 #include <list>   
 43 #include <vector>   
 44 #include <cstdio>   
 45 #include <cstdlib>   
 46 #include <cstring>   
 47 #include <cmath>   
 48 #include <ctime>   
 49 #include <ctype.h> 
 50 using namespace std;
 51 
 52 int map[2000][2000];
 53 int vst[2000][2000];
 54 int n,m;
 55 int sx,sy,ex,ey;
 56 bool flag;
 57 int ledgex1,ledgey1,ledgex2,ledgey2;
 58 int dir[8][2]={
 59     2,1,
 60     1,2,
 61     -1,2,
 62     -2,1,
 63     -2,-1,
 64     -1,-2,
 65     1,-2,
 66     2,-1
 67 };
 68 
 69 struct node
 70 {
 71     int x,y;
 72     int step;
 73 }Node;
 74 
 75 bool check(int x,int y)
 76 {
 77     if(x>=(1000-ledgex1)&&x<(1000+n-ledgex1)&&y>=(1000-ledgey1)&&y<(1000+n-ledgey1)&&!vst[x][y])
 78         return true;
 79     else
 80         return false;
 81 }
 82 
 83 void bfs()
 84 {
 85     int i;
 86     node pre,last;
 87     pre.x=1000;
 88     pre.y=1000;
 89     pre.step=0;
 90     queue<node>Que;
 91     Que.push(pre);
 92     while(!Que.empty())
 93     {
 94         pre=Que.front();
 95         Que.pop();
 96         if(pre.step>m)
 97         {
 98             flag=false;
 99              return;
100         }
101         if(pre.x==ex&&pre.y==ey)
102         {
103             if(pre.step>m)
104                 flag=false;
105             return;
106         }
107         for(i=0;i<8;i++)
108         {
109             last.x=pre.x+dir[i][0];
110             last.y=pre.y+dir[i][1];
111             last.step=pre.step+1;
112             if(check(last.x,last.y))
113             {
114                 vst[last.x][last.y]=1;
115                 Que.push(last);
116             }
117         }
118     }
119 }
120 
121 int main()
122 {
123     while(~scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&ex,&ey))
124     {
125         flag=true;
126         memset(vst,0,sizeof(vst));
127         if(abs(ex-sx)>m*2||abs(ey-sy)>m*2)
128             flag=false;
129         else
130         {
131             sx--;
132             ex--;
133             sy--;
134             ey--;
135             ledgex1=(sx>ex?ex:sx)>513?513:(sx>ex?ex:sx);
136             ledgey1=(sy>ey?ey:sy)>513?513:(sy>ey?ey:sy);
137             ex=ex-sx+1000;
138             ey=ey-sy+1000;
139             bfs();
140         }
141         if(flag)
142             printf("Knight can reach Queen within %d moves!\n",m);
143         else
144             printf("Knight cannot reach Queen within %d moves!\n",m);
145     }
146     return 0;
147 }

 

 

 

学长的代码,用了#include<map>

code2:

 1 #include <iostream>
 2 #include <queue>
 3 #include <map>
 4 using namespace std;
 5 
 6 struct node {
 7     int x,y,step;
 8     node () {
 9         x=y=step=0;
10     }
11 };
12 
13 int n,m,sx,sy,tx,ty,flag;
14 int dx[8]={-1,-2,-2,-1,1,2,2,1},dy[8]={-2,-1,1,2,2,1,-1,-2};
15 
16 void bfs()
17 {
18     if (abs(sx-tx)/2>m||abs(sy-ty)/2>m)
19         return;
20     queue<node> q;
21     map<pair<int, int>, int> v;
22     v.clear();
23     pair<int, int> b;
24     node cur,nex;
25     cur.x=sx;
26     cur.y=sy;
27     cur.step=0;
28     b.first=cur.x;
29     b.second=cur.y;
30     v[b]=1;
31     q.push(cur);
32     while (!q.empty()) {
33         cur=q.front();
34         q.pop();
35         if (cur.step>m)
36             continue;
37         if (cur.x==tx&&cur.y==ty&&cur.step<=m) {
38             flag=1;
39             return;
40         }
41         for (int i=0; i<8; i++) {
42             nex.x=cur.x+dx[i];
43             nex.y=cur.y+dy[i];
44             b.first=nex.x;
45             b.second=nex.y;
46             if (nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=n&&!v[b]) {
47                 nex.step=cur.step+1;
48                 v[b]=1;
49                 q.push(nex);
50             }
51         }
52     }
53 }
54 
55 int main()
56 {
57     while (~scanf("%d%d",&n,&m)) {
58         scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
59         flag=0;
60         bfs();
61         if (flag)
62             printf("Knight can reach Queen within %d moves!\n",m);
63         else printf("Knight cannot reach Queen within %d moves!\n",m);
64     }
65     return 0;
66 }

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值