标签:necessary and sep sse strong pair ems number img
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26102 | Accepted: 12305 |
Description
Input
Output
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
下棋,从一个坐标到另一个坐标最少几步,
第一个输入T个样例
第一行一个数L表示棋盘大小
然后两行两个坐标
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 int p[333][333]; 6 int main() 7 { 8 int T; 9 scanf("%d", &T); 10 while (T--) 11 { 12 memset(p, -1, sizeof(p)); 13 queue<int>q; 14 int L; 15 scanf("%d", &L); 16 int a, b, x, y; 17 scanf("%d%d%d%d", &a, &b, &x, &y); 18 p[a][b] = 0; 19 q.push(a * 1000 + b);//一个坐标转为一个数——前三位是横坐标后三位是纵坐标 20 while (!q.empty()) 21 { 22 int t = q.front(); 23 q.pop(); 24 a = t / 1000; 25 b = t % 1000; 26 if (a == x&&b == y) 27 { 28 printf("%d\n", p[a][b]); 29 break; 30 } 31 if (a - 1 >= 0 && b + 2 < L&&p[a - 1][b + 2] == -1) 32 { 33 q.push((a - 1) * 1000 + (b + 2)); 34 p[a - 1][b + 2] = p[a][b] + 1; 35 }// 36 if (a + 1 < L && b + 2 < L&&p[a + 1][b + 2] == -1) 37 { 38 q.push((a + 1) * 1000 + (b + 2)); 39 p[a + 1][b + 2] = p[a][b] + 1; 40 }// 41 if (a - 2 >= 0 && b + 1 < L&&p[a - 2][b + 1] == -1) 42 { 43 q.push((a - 2) * 1000 + (b + 1)); 44 p[a - 2][b + 1] = p[a][b] + 1; 45 }// 46 if (a - 2 >= 0 && b - 1 >=0&&p[a - 2][b - 1] == -1) 47 { 48 q.push((a - 2) * 1000 + (b - 1)); 49 p[a - 2][b - 1] = p[a][b] + 1; 50 }// 51 if (a + 2 <L && b + 1 < L&&p[a + 2][b + 1] == -1) 52 { 53 q.push((a + 2) * 1000 + (b + 1)); 54 p[a + 2][b + 1] = p[a][b] + 1; 55 }// 56 if (a + 2<L && b - 1 >=0&&p[a + 2][b - 1] == -1) 57 { 58 q.push((a + 2) * 1000 + (b - 1)); 59 p[a + 2][b - 1] = p[a][b] + 1; 60 }// 61 if (a - 1 >= 0 && b - 2 >=0&&p[a - 1][b - 2] == -1) 62 { 63 q.push((a - 1) * 1000 + (b - 2)); 64 p[a - 1][b - 2] = p[a][b] + 1; 65 }// 66 if (a + 1<L && b - 2>=0&&p[a + 1][b - 2] == -1) 67 { 68 q.push((a + 1) * 1000 + (b - 2)); 69 p[a + 1][b - 2] = p[a][b] + 1; 70 }//八个可能的位置 71 } 72 } 73 }
标签:necessary and sep sse strong pair ems number img
原文地址:http://www.cnblogs.com/jinmingyi/p/6832497.html