8数码问题之深度优先算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using namespace std;
//class definition class eight_num { private:
int num[9]; int not_in_position_num; int deapth; int eva_function;
public: eight_num* parent; eight_num* open_pre;
{
num[0] = num1;
num[1] = num2;
num[2] = num3;
num[3] = num4;
num[4] = num5;
num[5] = num6;
num[6] = num7;
num[7] = num8;
num[8] = num9;
}
eight_num(void)
//构造函数
{
}
void eight_num::show() {
cout << num[0]; cout << " "; cout << num[1]; cout << " "; cout << num[2]; cout << "\n"; cout << num[3]; cout << " "; cout << num[4]; cout << " "; cout << num[5]; cout << "\n"; cout << num[6]; cout << " "; cout << num[7]; cout << " "; cout << num[8]; cout << "\n"; }
if (num[j]<num[i] && num[j] != 0) count_num++;
if (target[j]<target[i] && target[j] != 0) count_target++;
} count_num = count_num - 2 * (count_num / 2); count_target = count_target - 2 * (count_target / 2); if ((count_num == 1 && count_target == 1) || (count_num == 0 && count_target == 0))
for (int i = 0; i<9; i++)
num[i] = i;
}
void cul_para(void);
void get_numbers_to(int other_num[9]); int get_nipn(void) {
return not_in_position_num; } int get_deapth(void) {
void eight_num::cul_para(void) {
int i; int temp_nipn=0; for (i = 0; i<9; i++) if (num[i] != target[i])
temp_nipn++; not_in_position_num = temp_nipn; if (this->parent == NULL)
return deapth; } int get_evafun(void) {
return eva_function; } void set_num(int other_num[9]); void show(void); eight_num& operator=(eight_num&); eight_num& operator=(int other_num[9]); int operator==(eight_num&); int operator==(int other_num[9]); };
}
eight_num& eight_num::operator=(eight_num& another_8num) {
for (int i = 0; i<9; i++) num[i] = another_8num.num[i];
not_in_position_num = another_8num.not_in_position_num; deapth = another_8num.deapth + 1; eva_function = not_in_position_num + deapth; return *this; }
//该节点的父节点 //该节点的同层的前一个节点
eight_num(int init_num[9]); //构造函数
eight_num(int num1, int num2, int num3, int num4, int num5, int num6, int
num7, int num8, int num9) //构造函数
return 1; else
return 0; }
//判断有无重复 int existed(int num[9], eight_num *where) {
eight_num *p; for (p = where; p != NULL; p = p->parent) if (*p == num)
return 1; return 0; }
//空格向下移 int move_down(int num[9]) {
for (int i = 0; i<9; i++) if (num[i] == 0)
break; if (i>5)
return 0; else {
num[i] = num[i + 3]; num[i + 3] = 0; return 1; } } //空格向左移 int move_left(int num[9]) { for (int i = 0; i<9; i++) if (num[i] == 0) break; if (i == 0 || i == 3 || i == 6) return 0; else { num[i] = num[i - 1]; num[i - 1] = 0; return 1; } } //空格向右移 int move_right(int num[9]) { for (int i = 0; i<9; i++) if (num[i] == 0) break; if (i == 2 || i == 5 || i == 8)
deapth = 0; else
deapth = this->parent->deapth + 1; eva_function = not_in_position_num + deapth; }
eight_num::eight_num(int init_num[9]) {
for (int i = 0; i<9; i++) num[i] = init_num[i];
//主函数开始 int main(void) {
int max_step = 30; double time; clock_t Start, Finish; int memery_used = 0, step = 0; int num[9]; int flag = 0;//是否输入错误标志,1表示输入错误 int bingo = 0;//是否查找成功标志,1表示成功 int new_flag = 0;//是否有新节点生成,1表示有 int i, j; cout << "郭文涛 S201402233\n请输入初始数组(0代表空白):\n"; for (i = 0; i<9; i++) {
eight_num& eight_num::operator=(int other_num[9]) {
for (int i = 0; i<9; i++) num[i] = other_num[i];
return *this; }
int eight_num::operator==(eight_num& another_8num) {
// 8数码之深度优先 //编译环境:VS2013
#include "stdafx.h" #include <iostream> #include <time.h> #include <stdio.h> #include <dos.h> #include <conio.h>
static int target[9] = { 1, 2, 3, 8, 0, 4, 7, 6, 5 }; int i = 0; //static int target[9]={1,2,3,4,5,6,7,8,0};
flag = 1; if (target[i]<0 || target[i]>8 || flag == 1) {
i--; cout << "Illegle number!\tReinput!\n"; } } } eight_num S(num), Target(target); //S 为初始节点,Target为目标节点 S.parent = S.open_pre = NULL; S.cul_para(); memery_used++; cout << "初始数组是:\n"; S.show(); cout << "目标数组是:\n"; Target.show();
int match = 1; for (int i = 0; i<9; i++) if (num[i] != another_8num.num[i]) {
match = 0; break; } if (match == 0) return 0; else return 1; }
int eight_num::operator==(int other_num[9]) {
int match = 1; for (int i = 0; i<9; i++) if (num[i] != other_num[i]) {
match = 0; break; } if (match == 0) return 0; else return 1; }
//class definition over //*************************
flag = 0; cin >> num[i]; for (j = 0; j<i; j++) if (num[i] == num[j])
flag = 1; if (num[i]<0 || num[i]>8 || flag == 1) {
i--; cout << "非法数组!\请重新输入\n"; } } cout << "你确定要转换(是Y/否N)?"; char input; cin >> input; if (input == 'y' || input == 'Y') { cout << "\n请输入新数组:\n"; for (i = 0; i<9; i++) { flag = 0; cin >> target[i]; for (j = 0; j<i; j++) if (target[i] == target[j])
rwk.baidu.comturn 0; else {
num[i] = num[i + 1]; num[i + 1] = 0; return 1; } }
//判断可否解出 int icansolve(int num[9], int target[9]) {
int i, j; // int count_num,count_target; int count_num = 0, count_target = 0; for (i = 0; i<9; i++) for (j = 0; j<i; j++) {
//空格向上移 int move_up(int num[9]) {
for (int i = 0; i<9; i++) if (num[i] == 0)
break; if (i<3)
return 0; else {
num[i] = num[i - 3]; num[i - 3] = 0; return 1; } }
void eight_num::get_numbers_to(int other_num[9])
{ for (int i = 0; i<9; i++) other_num[i] = num[i];
}
void eight_num::set_num(int other_num[9]) {
for (int i = 0; i<9; i++) num[i] = other_num[i];