马踏棋盘 贪心
来源:互联网 发布:路人甲电视软件 编辑:程序博客网 时间:2024/05/21 11:11
#include<iostream>;
using namespace std;
struct ChessStep {
int x;
int y;
int haveVisiteNum;//已经访问过的次数
ChessStep* next;
};
class ChessBoard {
private:
int isVisit[8][8];
int haveVisit;
ChessStep* topStep;
int Htry1[8] = { -2,-1,1,2,2,1,-1,-2 };
int Htry2[8] = { 1,2,2,1,-1,-2,-2,-1 };
int FuncNum;
public:
ChessBoard() {
topStep = new ChessStep();
topStep->x = 0;
topStep->y = 0;
topStep->haveVisiteNum = 0;
topStep->next = NULL;
FuncNum = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
isVisit[i][j] = 0;
}
}
haveVisit = 1;
isVisit[0][0] = 1;
}
ChessBoard(int x, int y) {
topStep = new ChessStep();
topStep->x = x;
topStep->y = y;
topStep->haveVisiteNum = 0;
topStep->next = NULL;
FuncNum = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
isVisit[i][j] = 0;
}
}
haveVisit = 1;
isVisit[x][y] = 1;
}
void addStep(int x, int y);
void unStep();//栈顶删除
int checkVisit(int x, int y);
void Run();
void autoRun();
void printRoad();
};
void ChessBoard::printRoad()
{
int i, j;
// nCount++;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%-4d", isVisit[i][j]);
}
printf("\n");
}
printf("\n\n");
}
int ChessBoard::checkVisit(int x, int y) {
if (isVisit[x][y] == 0) {
int shaobin = 0;
for (int i = 0; i < 8; i++) {
if ((x + Htry1[i]) < 8 && (y + Htry2[i]) < 8 && (x + Htry1[i]) >= 0 && (y + Htry2[i]) >= 0) {
if (!isVisit[x + Htry1[i]][y + Htry2[i]]) {
shaobin++;
}
}
}
return shaobin;
}
else {
return -1;
}
}
void ChessBoard::addStep(int x, int y) {
ChessStep* newStep = new ChessStep();
newStep->next = topStep;
newStep->x = x;
newStep->y = y;
newStep->haveVisiteNum = 0;
isVisit[x][y] = ++haveVisit;
topStep = newStep;
}
void ChessBoard::unStep() {
isVisit[topStep->x][topStep->y] = 0;
haveVisit--;
ChessStep* temp = topStep;
topStep = topStep->next;
delete temp;
}
void ChessBoard::Run() {
int toCheck, checkList[8], min = 8, minId = -1, checkNum = 0;
for (int i = 0; i < 8; i++) {
int rX = topStep->x + Htry1[i], rY = topStep->y + Htry2[i];
if (isVisit[rX][rY] == 0 && rX < 8 && rY < 8 && rX >= 0 && rY >= 0) {
int check = checkVisit(topStep->x + Htry1[i], topStep->y + Htry2[i]);
checkList[i] = check;
checkNum++;
}
else {
checkList[i] = 8;
}
}
if (checkNum-topStep->haveVisiteNum != 0) {
for (int i = 0; i < topStep->haveVisiteNum; i++) {//这里是为了删掉已经走过的路
min = -1; minId = 0;
for (int j = 0; j < 8; j++) {
if (checkList[i] < min) {
min = checkList[i];
minId = i;
}
}
checkList[minId] = 8;
}
minId = -1; min = 8;
for (int i = 0; i < 8; i++) {
if (checkList[i] < min) {
min = checkList[i];
minId = i;
}
}
}
toCheck = minId;
//cout << "to Check " << toCheck << endl;
if (toCheck == -1) {
unStep();
}
else {
topStep->haveVisiteNum++;
addStep(topStep->x + Htry1[toCheck], topStep->y + Htry2[toCheck]);
if (haveVisit == 64) {
FuncNum++;
printRoad();
unStep();
}
}
}
void ChessBoard::autoRun() {
while (FuncNum < 500) {
Run();
}
}
int main() {
ChessBoard b;
b.autoRun();
return 0;
}
using namespace std;
struct ChessStep {
int x;
int y;
int haveVisiteNum;//已经访问过的次数
ChessStep* next;
};
class ChessBoard {
private:
int isVisit[8][8];
int haveVisit;
ChessStep* topStep;
int Htry1[8] = { -2,-1,1,2,2,1,-1,-2 };
int Htry2[8] = { 1,2,2,1,-1,-2,-2,-1 };
int FuncNum;
public:
ChessBoard() {
topStep = new ChessStep();
topStep->x = 0;
topStep->y = 0;
topStep->haveVisiteNum = 0;
topStep->next = NULL;
FuncNum = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
isVisit[i][j] = 0;
}
}
haveVisit = 1;
isVisit[0][0] = 1;
}
ChessBoard(int x, int y) {
topStep = new ChessStep();
topStep->x = x;
topStep->y = y;
topStep->haveVisiteNum = 0;
topStep->next = NULL;
FuncNum = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
isVisit[i][j] = 0;
}
}
haveVisit = 1;
isVisit[x][y] = 1;
}
void addStep(int x, int y);
void unStep();//栈顶删除
int checkVisit(int x, int y);
void Run();
void autoRun();
void printRoad();
};
void ChessBoard::printRoad()
{
int i, j;
// nCount++;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%-4d", isVisit[i][j]);
}
printf("\n");
}
printf("\n\n");
}
int ChessBoard::checkVisit(int x, int y) {
if (isVisit[x][y] == 0) {
int shaobin = 0;
for (int i = 0; i < 8; i++) {
if ((x + Htry1[i]) < 8 && (y + Htry2[i]) < 8 && (x + Htry1[i]) >= 0 && (y + Htry2[i]) >= 0) {
if (!isVisit[x + Htry1[i]][y + Htry2[i]]) {
shaobin++;
}
}
}
return shaobin;
}
else {
return -1;
}
}
void ChessBoard::addStep(int x, int y) {
ChessStep* newStep = new ChessStep();
newStep->next = topStep;
newStep->x = x;
newStep->y = y;
newStep->haveVisiteNum = 0;
isVisit[x][y] = ++haveVisit;
topStep = newStep;
}
void ChessBoard::unStep() {
isVisit[topStep->x][topStep->y] = 0;
haveVisit--;
ChessStep* temp = topStep;
topStep = topStep->next;
delete temp;
}
void ChessBoard::Run() {
int toCheck, checkList[8], min = 8, minId = -1, checkNum = 0;
for (int i = 0; i < 8; i++) {
int rX = topStep->x + Htry1[i], rY = topStep->y + Htry2[i];
if (isVisit[rX][rY] == 0 && rX < 8 && rY < 8 && rX >= 0 && rY >= 0) {
int check = checkVisit(topStep->x + Htry1[i], topStep->y + Htry2[i]);
checkList[i] = check;
checkNum++;
}
else {
checkList[i] = 8;
}
}
if (checkNum-topStep->haveVisiteNum != 0) {
for (int i = 0; i < topStep->haveVisiteNum; i++) {//这里是为了删掉已经走过的路
min = -1; minId = 0;
for (int j = 0; j < 8; j++) {
if (checkList[i] < min) {
min = checkList[i];
minId = i;
}
}
checkList[minId] = 8;
}
minId = -1; min = 8;
for (int i = 0; i < 8; i++) {
if (checkList[i] < min) {
min = checkList[i];
minId = i;
}
}
}
toCheck = minId;
//cout << "to Check " << toCheck << endl;
if (toCheck == -1) {
unStep();
}
else {
topStep->haveVisiteNum++;
addStep(topStep->x + Htry1[toCheck], topStep->y + Htry2[toCheck]);
if (haveVisit == 64) {
FuncNum++;
printRoad();
unStep();
}
}
}
void ChessBoard::autoRun() {
while (FuncNum < 500) {
Run();
}
}
int main() {
ChessBoard b;
b.autoRun();
return 0;
}
//大概2秒钟500次的速度,比不用贪心还是快很多
//改进了一点,之前判断是否访问的方法有问题,不能找到所有结果,现在已完善
0 0
- 马踏棋盘 贪心
- 马踏棋盘(贪心)
- 马踏中国象棋棋盘的贪心算法
- 马踏棋盘:贪心算法java实现
- 马踏棋盘 之 贪心算法
- 马踏棋盘之贪心算法优化
- 贪心算法 --- 马踏棋盘(C)
- 马踏棋盘算法 贪心算法优化
- C++贪心算法实现马踏棋盘问题
- 马踏棋盘问题 — 深搜和贪心算法
- 栈的应用--马踏棋盘-贪心加栈
- 马踏棋盘的贪心算法 递归算法 非递归算法实现
- 基于贪心算法的马踏棋盘哈密顿回路问题
- 贪心算法棋盘覆盖
- 马踏棋盘问题
- 马踏棋盘
- 马踏棋盘
- 马踏棋盘
- iOS应用性能调优的25个建议和技巧(1)
- java Thrad 的五种状态
- JavaScript入门系列--概述与java的区别
- 利用ffmpeg打造播放器直播观看公网rtmp1s延时极致优化
- MAC端Git安装以及环境搭建
- 马踏棋盘 贪心
- 控制台应用程序的Opencv
- 关于Fragment与Fragment、Activity通信的四种方式
- iOS应用性能调优的25个建议和技巧(3)
- 程序设计
- Deep Learning Roadmap
- 精华版线段树模板
- spannableString的使用
- 蛇形填数