Sicily 1936. Knight Moves

题目地址:1936. Knight Moves

思路:

这道题一开始不理解题意…orz...囧,看大神们理解的。

题意是说一个8*8的国际象棋,骑士以马的形式走动(“日”字型),指定两个点,输出最小的步骤。

可以利用广度搜索解决。

具体代码如下:

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <string>
 5 using namespace std;
 6
 7 int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};    //可以走八个方向
 8 int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
 9
10 bool visited[100];
11
12 int main() {
13     int t;
14     cin >> t;
15     while (t--) {
16         memset(visited, false, sizeof(visited));
17         int distance[100] = {0};
18
19         string node1, node2;
20         cin >> node1 >> node2;
21
22         int X = (node1[0]-‘a‘)*8 + node1[1]-‘1‘;
23         int Y = (node2[0]-‘a‘)*8 + node2[1]-‘1‘;
24
25         queue<int> store;
26         store.push(X);
27         while (!store.empty()) {
28             if (store.front() == Y)
29                 break;
30
31             int x = store.front()/8;
32             int y = store.front()%8;
33
34             for (int i = 0; i < 8; i++) {
35                 int nx = x+dx[i];
36                 int ny = y+dy[i];
37
38                 if (nx < 0||nx > 7||ny < 0||ny > 7)
39                     continue;
40                 int temp = nx*8 + ny;
41
42                 if (!visited[temp]) {
43                     store.push(temp);
44                     visited[temp] = true;
45                     distance[temp] = distance[store.front()] + 1;
46                 }
47             }
48             store.pop();
49         }
50         cout << "To get from " << node1
51              << " to " << node2 << " takes "
52              << distance[Y] << " knight moves.\n";
53     }
54
55     return 0;
56 }
57  
时间: 2024-07-31 14:26:54

Sicily 1936. Knight Moves的相关文章

Sicily 1936. Knight Moves 解题报告

1936_Knight_Moves 题目链接: http://soj.me/1936 题目大意: 给出一个8×8的棋盘,骑士走的是“日”字型,给出骑士的初始位置和目标位置,问最少经过多少步可以到达目标位置. 思路: 比较简单的宽度优先搜索题目,只要从起始位置开始,每一步搜索完这一层能够达到的所有点,这样你能保证第一次找到目标点的话就一定是最少的步数到达的. 代码: #include <iostream> #include <queue> using namespace std; i

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square

POJ 1915 Knight Moves

Knight Moves Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the mini

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9

HDOJ 1372 Knight Moves【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7482    Accepted Submission(s): 4481 Problem Description A friend of you is doin

poj 2243 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11794   Accepted: 6646 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that v

POJ Knight Moves 2243 x

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13974   Accepted: 7797 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that v

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin