求助!想把如下c++编写的骑士周游问题转成java代码,求论坛大神帮助

wayaoliang2864 2017-07-12 08:27:09
代码如下。因为本次作业要求加一个图形化界面,但是本人暂时没有学会c++图形化界面的编写,想转用java,但是转换代码这里遇到了困难!主要是重载部分和优先级队列部分不会转化!求帮助!

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <queue>

using namespace std;

typedef struct
{
int x;
int y;
} Step;

Step step[8] = { { -2, -1 },{ -1, -2 },{ 1, -2 },{ 2, -1 },{ 2, 1 },{ 1, 2 },{ -1, 2 },{ -2,1 } };

typedef struct NextPos
{
int nextPosSteps; //表示下一位置有多少种走法;走法少的优先考虑
int nextPosDirection; //下一位置相对于当前位置的方位
int nextPosToMidLength; //表示当前位置距中间点距离;距离中间点远的优先考虑

//
bool operator < (const NextPos &a) const
{
return nextPosSteps > a.nextPosSteps && nextPosToMidLength < a.nextPosToMidLength;
}

};

int board[8][8];
//int N; //棋盘大小
int MIDPOS = 3; // 中点

//检测这个位置是否可以走
bool check(int x, int y)
{
if (x >= 0 && x < 8 && y >= 0 && y < 8 && board[x][y] == 0)
return true;
else return false;
}
//下一位置有多少种走法
int nextPosHasSteps(int x, int y)
{
int steps = 0;
for (int i = 0; i < 8; ++i)
{
if (check(x + step[i].x, y + step[i].y))
steps++;
}
return steps;
}
//判断是否回到起点
bool returnStart(int x, int y)
{
//校验最后是否可以回到起点,也就是棋盘的中间位置
//int midpos = 3;
//midx = N / 2 - 1;
//midy = midx;
for (int i = 0; i < 8; ++i)
if (x + step[i].x == MIDPOS && y + step[i].y == MIDPOS)
return true;
return false;
}

//输出结果
void outputResult(int xstart, int ystart)
{
int num = 64;
int k = num - board[xstart][ystart];
for (int i = 1; i <= 8; ++i)
{
cout << endl << endl;
for (int j = 1; j <= 8; ++j)
{
board[i][j] = (board[i][j] + k) % num + 1;
cout << setw(5) << board[i][j];
}
}
cout << endl << endl;
}

//某一位置距离棋盘中心的距离
int posToMidLength(int x, int y)
{
//int midx = N / 2 - 1;
//int midy = midx;;
return (abs(x - MIDPOS) + abs(y - MIDPOS));
}

void BackTrace(int t, int x, int y, int xstart, int ystart)
{
//找到结果
if (t == 64 && returnStart(x, y)) //遍历了棋盘的所以位置,并且最后可以回到起点,形成回路
{
outputResult(xstart, ystart);
exit(1);
}
else
{
priority_queue<NextPos> nextPosQueue;
for (int i = 0; i < 8; ++i)
{
if (check(x + step[i].x, y + step[i].y))
{
NextPos aNextPos;
aNextPos.nextPosSteps = nextPosHasSteps(x + step[i].x, y + step[i].y);
aNextPos.nextPosDirection = i;
aNextPos.nextPosToMidLength = posToMidLength(x + step[i].x, y + step[i].y);
nextPosQueue.push(aNextPos);
}
}

while (nextPosQueue.size())
{
int d = nextPosQueue.top().nextPosDirection;
nextPosQueue.pop();

x += step[d].x;
y += step[d].y;
board[x][y] = t + 1;
BackTrace(t + 1, x, y, xstart, ystart);
//回溯
board[x][y] = 0;
x -= step[d].x;
y -= step[d].y;
}
}
}


void horseRun(int xstart, int ystart)
{
//初始化棋盘
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
board[i][j] = 0;
//int midx = N / 2 - 1;
//int midy = midx;
board[MIDPOS][MIDPOS] = 1; //从棋盘的中间的位置开始马周游
BackTrace(1, MIDPOS, MIDPOS, xstart, ystart);
}

int main()
{
//马周游起始位置
int x, y;

//cout << "请输入棋盘大小N:";
//cin >> N;

cout << "请输入马周游起始位置:";
cin >> x >> y;

horseRun(x, y); //执行马周游
return 0;
}
...全文
149 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧