UVA 439 bfs搜索
2014-07-25 08:41
337 查看
http://vjudge.net/contest/view.action?cid=50788#problem/C
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 of a given set of n squares
on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
the column and a digit (1-8) representing the row on the chessboard.
题目大意:骑士走的路径都是“日”字,求一个点到另一个点的最少步数。
解题思路:bfs裸题,
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
int dx[]={-1,-1,1,1,2,2,-2,-2};
int dy[]={2,-2,2,-2,-1,1,1,-1};
char a[2][2];
struct note
{
int x,y,sum;
};
int bfs(int x,int y,int n,int m)
{
int flag[10][10];
memset(flag,0,sizeof(flag));
queue<note> q;
int cnt=0;
note num;
num.x=x;
num.y=y;
num.sum=0;
while(!q.empty())
q.pop();
q.push(num);
while(!q.empty())
{
int u=q.front().x;
int v=q.front().y;
int count=q.front().sum;
//printf("%d,%d,%d\n",u,v,cnt);
if(u==n&&v==m)
{
cnt=count;
//printf("**\n");
break;
}
q.pop();
for(int i=0;i<8;i++)
{
int xx=dx[i]+u;
int yy=dy[i]+v;
if(xx<8&&xx>=0&&yy>=0&&yy<8&&flag[xx][yy]==0)
{
flag[xx][yy]=1;
num.x=xx;
num.y=yy;
num.sum=count+1;
//printf("**\n");
q.push(num);
}
}
}
return cnt;
}
int main()
{
while(~scanf("%s%s",a[0],a[1]))
{
int x=a[0][0]-'a';
int y=a[0][1]-'1';
int n=a[1][0]-'a';
int m=a[1][1]-'1';
//printf("%d %d,,%d,%d\n",x,y,n,m);
printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0][0],a[0][1],a[1][0],a[1][1],bfs(x,y,n,m));
}
return 0;
}
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 of a given set of n squares
on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input Specification
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representingthe column and a digit (1-8) representing the row on the chessboard.
Output Specification
For each test case, print one line saying "To get from xx to yy takes n knight moves.".Sample Input
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from a1 to h8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves. To get from h8 to a1 takes 6 knight moves. To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves.
题目大意:骑士走的路径都是“日”字,求一个点到另一个点的最少步数。
解题思路:bfs裸题,
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
int dx[]={-1,-1,1,1,2,2,-2,-2};
int dy[]={2,-2,2,-2,-1,1,1,-1};
char a[2][2];
struct note
{
int x,y,sum;
};
int bfs(int x,int y,int n,int m)
{
int flag[10][10];
memset(flag,0,sizeof(flag));
queue<note> q;
int cnt=0;
note num;
num.x=x;
num.y=y;
num.sum=0;
while(!q.empty())
q.pop();
q.push(num);
while(!q.empty())
{
int u=q.front().x;
int v=q.front().y;
int count=q.front().sum;
//printf("%d,%d,%d\n",u,v,cnt);
if(u==n&&v==m)
{
cnt=count;
//printf("**\n");
break;
}
q.pop();
for(int i=0;i<8;i++)
{
int xx=dx[i]+u;
int yy=dy[i]+v;
if(xx<8&&xx>=0&&yy>=0&&yy<8&&flag[xx][yy]==0)
{
flag[xx][yy]=1;
num.x=xx;
num.y=yy;
num.sum=count+1;
//printf("**\n");
q.push(num);
}
}
}
return cnt;
}
int main()
{
while(~scanf("%s%s",a[0],a[1]))
{
int x=a[0][0]-'a';
int y=a[0][1]-'1';
int n=a[1][0]-'a';
int m=a[1][1]-'1';
//printf("%d %d,,%d,%d\n",x,y,n,m);
printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0][0],a[0][1],a[1][0],a[1][1],bfs(x,y,n,m));
}
return 0;
}
相关文章推荐
- uva439 Knight Moves (搜索 BFS, BFS启蒙题)
- UVa 11244 counting stars ( 搜索 BFS)
- UVA - 439 - Knight Moves (BFS)
- UVA 11624 Fire! BFS搜索
- UVA 439-Knight Moves(bfs)
- UVA439 水,枚举+BFS
- UVa 439 - Knight Moves (BFS)
- uva-439 - HDOJ-1372-Knight Moves-翻译详解-BFS
- UVa 439骑士的移动(BFS)
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
- Knight Moves UVA - 439(BFS典例)
- UVa439---Knight Moves(BFS应用)
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- UVa 439 Knight Moves(BFS应用)
- Uva11624 Fire(bfs 搜索)
- UVA 11882 Biggest Number (dfs搜索+bfs 剪枝)
- uva439 - Knight Moves(BFS求最短路)
- uva 439 Knight Moves(骑士的移动)BFS
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
- UVA439_Knight Moves(BFS)