数据结构之马踏棋盘
来源:互联网 发布:linux查询cpu个数 编辑:程序博客网 时间:2024/04/28 21:15
经过优化
#include<cstdio>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<Windows.h>using namespace std;#define STACK_INIT_SIZE 100 //存储空间出事分配量 #define STACK_INCRENMENT 10 // 存储空间分配增量 #define MAXN 8int dir[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};int vis[MAXN+5][MAXN+5];int map[MAXN+5][MAXN+5];struct Order{int pos;int d;};bool cmp(Order a,Order b){return a.d<b.d;}bool judge(int x,int y){if(x>=1&&x<=MAXN&&y>=1&&y<=MAXN&&!vis[x][y])return 1;return 0;}struct Node{int x,y,d;int cnt;Order order[8];int flag;//判断是否排序过 Node(int xx=1,int yy=1,int dd=-1){x=xx;y=yy;d=dd;flag=0;}void gerOrder()//出度排序 {cnt=0;for(int i=0;i<8;i++){int tx=x+dir[i][0];int ty=y+dir[i][1];if(judge(tx,ty)){order[cnt].pos=i;order[cnt].d=0;for(int j=0;j<8;j++){int ttx=tx+dir[j][0];int tty=ty+dir[j][1]; if(judge(ttx,tty))order[cnt].d++;}cnt++;}}sort(order,order+cnt,cmp);}};struct Stack{Node *base; // 在栈构造之前和小会之后,base的值是NULL Node *top; // 栈顶指针 int stackSize;//当前分配的空间,以元素为单位 int stackNum; // 栈内元素数Stack(){base=(Node*)malloc(STACK_INIT_SIZE*sizeof(Node));top=base;stackSize=STACK_INIT_SIZE;stackNum=0;}bool empty(){if(top==base)return 1;return 0;}Node& GetTop()//返回后可直接修改 {if(top==base){cout<<"栈内无元素1"<<endl;exit(0);}return (*top);}void Pop(){if(top==base){cout<<"栈内无元素2"<<endl;exit(0);}--top;--stackNum;}void Push(Node e){if(top-base>=stackSize-1)//栈满,追加存储空间 {base=(Node *)realloc(base,(stackSize+STACK_INCRENMENT)*sizeof(Node));top=base+stackSize;stackSize+=STACK_INCRENMENT;}stackNum++;*(++top)=e;}};void print(){cout<<endl;for(int i=1;i<=MAXN;i++){for(int j=1;j<=MAXN;j++){printf("%02d ",map[i][j]);}cout<<endl;}}bool search(int n,int m){Stack S;S.Push(Node(n,m,-1));vis[n][m]=1;int flag=0;while(!S.empty()){Node& now=S.GetTop();if(now.flag==0){now.flag=1;now.gerOrder();}map[now.x][now.y]=S.stackNum;if(S.stackNum==MAXN*MAXN)return 1;for(now.d++;now.d<now.cnt;now.d++){int tx=now.x+dir[now.order[now.d].pos][0];int ty=now.y+dir[now.order[now.d].pos][1];if(judge(tx,ty)){vis[tx][ty]=1;S.Push(Node(tx,ty,-1));break;}}if(now.d==now.cnt){S.Pop();map[now.x][now.y]=0;vis[now.x][now.y]=0;}//Sleep(10);//print();//system("cls");}free(S.base);return 0;}int main(){ //freopen("f://out.txt", "w", stdout); int n,m;//马的起点位置 while(cin>>n>>m){memset(vis,0,sizeof(vis));if(search(n,m))print();elsecout<<"无解"<<endl;}return 0;}
- 数据结构之马踏棋盘
- 【马踏棋盘】 数据结构
- 数据结构---马踏棋盘
- 【数据结构】马踏棋盘问题
- 【数据结构】马踏棋盘的遍历问题
- 数据结构学习----马踏棋盘程序的实现
- 【数据结构】关于马踏棋盘(8皇后问题)
- C语言:马踏棋盘,改编自《数据结构与算法》.
- 数据结构课后习题 马踏棋盘 c语言版
- C语言-数据结构-骑士周游-马踏棋盘问题-源代码
- 马踏棋盘之递归实现
- 马踏棋盘 之 贪心算法
- 马踏棋盘之贪心算法优化
- 回溯算法之马踏棋盘
- 10马踏棋盘之中国象棋
- 马踏棋盘之字典序遍历棋盘(简单深搜)
- 马踏棋盘问题
- 马踏棋盘
- TCMalloc源码阅读(二)--线程局部缓存ClassSize分析
- 九 Django 1.5.4 Bootstrap
- NYOJ-103-A+B Problem II-2013年08月16日23:56:46
- Linux makefile 教程 非常详细,且易懂
- vtk环境搭建(windowsXP/win7,vtk6.0.0+cmake2.8+vs2010)
- 数据结构之马踏棋盘
- VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x
- [Academia] 学术文章数据库及搜索引擎
- 请谨慎使用HTML的条件注释!
- 二叉树中增加next指针 Populating Next Right Pointers in Each Node
- NYOJ-312-20岁生日-2013年08月17日08:42:42
- NYIST格点
- Oracle 11gR2 RAC LoadBalancing and TAF
- ListView小知识整理:滑动背景、Item间隙等