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: 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: 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 #include33 #include 34 #include 35 #include 36 #include 37 #include <string> 38 #include <set> 39 #include 40 #include 41 #include 42 #include 43 #include
44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 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 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
code2:
1 #include2 #include 3 #include