POJ2243
2009-10-19 19:55
190 查看
摘要:和1915是一样的题目
#include <iostream>
#include <queue>
using namespace std;
typedef struct Node{
int x;
int y;
int dist;
}Node;
queue<Node> q;
Node start;
Node finish;
int length = 8;
const int size = 8;
const int offset_num = 8;
int offset_x[offset_num+1] = {0};
int offset_y[offset_num+1] = {0};
int graph[size+1][size+1] = {0};
void initOffset()
{
offset_x[1] = -2;
offset_x[2] = -1;
offset_x[3] = 1;
offset_x[4] = 2;
offset_x[5] = 2;
offset_x[6] = 1;
offset_x[7] = -1;
offset_x[8] = -2;
offset_y[1] = -1;
offset_y[2] = -2;
offset_y[3] = -2;
offset_y[4] = -1;
offset_y[5] = 1;
offset_y[6] = 2;
offset_y[7] = 2;
offset_y[8] = 1;
}
bool checkOffset(int i, Node current)
{
int x = current.x + offset_x[i];
int y = current.y + offset_y[i];
if(x<0 || x>=length || y<0 || y>=length){
return false;
}
if(graph[y][x] > 0){
return false;
}
return true;
}
void BFS()
{
if(start.x==finish.x && start.y==finish.y){
finish.dist = 0;
return;
}
Node current = start;
while(true){
bool find = false;
for(int i=1; i<=offset_num; i++){
if( checkOffset(i, current) ){
Node next;
next.x = current.x+offset_x[i];
next.y = current.y+offset_y[i];
next.dist = current.dist + 1;
if(next.x==finish.x && next.y==finish.y){
find = true;
finish.dist = next.dist;
break;
}
q.push(next);
graph[next.y][next.x] = next.dist;
}
}
if( find ){
break;
}
if( q.empty() ){
break;
}
current = q.front();
q.pop();
}
}
int main()
{
initOffset();
string str_start, str_finish;
while( cin >> str_start >> str_finish ){
start.x = str_start.at(0)-'a';
start.y = str_start.at(1)-'1';
finish.x = str_finish.at(0)-'a';
finish.y = str_finish.at(1)-'1';
start.dist = finish.dist = 0;
for(int i=0; i<=length; i++){
for(int j=0; j<=length; j++){
graph[i][j] = 0;
}
}
while( !q.empty() ){
q.pop();
}
BFS();
cout << "To get from " << str_start << " to " << str_finish << " takes " << finish.dist << " knight moves." << endl;
}
}
#include <iostream>
#include <queue>
using namespace std;
typedef struct Node{
int x;
int y;
int dist;
}Node;
queue<Node> q;
Node start;
Node finish;
int length = 8;
const int size = 8;
const int offset_num = 8;
int offset_x[offset_num+1] = {0};
int offset_y[offset_num+1] = {0};
int graph[size+1][size+1] = {0};
void initOffset()
{
offset_x[1] = -2;
offset_x[2] = -1;
offset_x[3] = 1;
offset_x[4] = 2;
offset_x[5] = 2;
offset_x[6] = 1;
offset_x[7] = -1;
offset_x[8] = -2;
offset_y[1] = -1;
offset_y[2] = -2;
offset_y[3] = -2;
offset_y[4] = -1;
offset_y[5] = 1;
offset_y[6] = 2;
offset_y[7] = 2;
offset_y[8] = 1;
}
bool checkOffset(int i, Node current)
{
int x = current.x + offset_x[i];
int y = current.y + offset_y[i];
if(x<0 || x>=length || y<0 || y>=length){
return false;
}
if(graph[y][x] > 0){
return false;
}
return true;
}
void BFS()
{
if(start.x==finish.x && start.y==finish.y){
finish.dist = 0;
return;
}
Node current = start;
while(true){
bool find = false;
for(int i=1; i<=offset_num; i++){
if( checkOffset(i, current) ){
Node next;
next.x = current.x+offset_x[i];
next.y = current.y+offset_y[i];
next.dist = current.dist + 1;
if(next.x==finish.x && next.y==finish.y){
find = true;
finish.dist = next.dist;
break;
}
q.push(next);
graph[next.y][next.x] = next.dist;
}
}
if( find ){
break;
}
if( q.empty() ){
break;
}
current = q.front();
q.pop();
}
}
int main()
{
initOffset();
string str_start, str_finish;
while( cin >> str_start >> str_finish ){
start.x = str_start.at(0)-'a';
start.y = str_start.at(1)-'1';
finish.x = str_finish.at(0)-'a';
finish.y = str_finish.at(1)-'1';
start.dist = finish.dist = 0;
for(int i=0; i<=length; i++){
for(int j=0; j<=length; j++){
graph[i][j] = 0;
}
}
while( !q.empty() ){
q.pop();
}
BFS();
cout << "To get from " << str_start << " to " << str_finish << " takes " << finish.dist << " knight moves." << endl;
}
}
相关文章推荐
- poj2243+poj1915骑士问题
- poj2243 Knight Moves(BFS)
- POJ 2243 BFS 和 简单的调试方法学习
- BFS简单搜索--POJ 2243
- POJ 2243 Knight Moves(BFS或DFS)
- HDU 3966 & POJ 3237 & HYSBZ 2243 & HRBUST 2064 树链剖分
- POJ 2243 Knight Moves (BFS入门)
- POJ 2243 Knight Moves
- poj 2243
- hdu 1372 || poj 2243 Knight Moves(搜索:BFS+优先队列水题)
- POJ 2243 Knight Moves
- POJ 2243 Floyd入门
- poj 2243 Knight Moves
- poj_2243/uva439 Knight Moves(bfs經典)
- ZZULI 1616(POJ 2243||HDU 1372)…
- poj2243+poj1915骑士问题
- poj 2243 简单bfs
- POJ 2243 Knight Moves(BFS || 双向BFS)
- poj 2243 bfs(还是骑士移动)
- POJ 2243 bfs