独立钻石跳棋问题
来源:互联网 发布:caffe 找不到cudnn 编辑:程序博客网 时间:2024/04/28 11:18
//参考 :http://blog.sina.com.cn/s/blog_510ad0640100bo3d.html#include <iostream>#include <fstream>using namespace std;struct step //记录移动棋子的信息{ int sx, sy; // 记录移动棋子前棋子的位置 int tx, ty; // 记录移动棋子后棋子的位置 int dir; // dir值代表移动棋子的方向};struct step mystack[100], last_step;char diamond[7][7];int Left_diamond;int x, y, nx, ny, ndir, top; // ndir值代表方向, 0代表向右, 1代表向下, 2代表向左, 3代表向上int endx, endy; //终止方格坐标int n; //初始钻石个数void Init_diamond(){ for(int i=0; i<7; i++) { for(int j=0; j<7; j++) { if((i<2 || i>4) && (j<2 || j>4)) //棋盘外 continue; else diamond[i][j] = '!'; //空方格 } } ifstream fin("独立钻石跳棋.txt"); cout << "输入棋子数:"; fin >> n; cout << n << endl; Left_diamond = n; cout << "输入棋子位置:\n"; int x, y; for(i=0; i<n; i++) { fin >> y >> x; cout << x << " " << y << endl; diamond[x][y] = '*'; //钻石 } cout << "输入终止方格坐标:"; fin >> endx >> endy;}int Move_diamond(int y, int x, int dir){ if(diamond[y][x] != '*') { return 0; } struct step temp; switch(dir) { case 0: //向右走 if(x+2>6 || diamond[y][x+1]!='*' || diamond[y][x+2]!='!') { return 0; } diamond[y][x] = diamond[y][x+1] = '!'; diamond[y][x+2] = '*'; temp.sx = x; temp.sy = y; temp.tx = x+2; temp.ty = y; temp.dir = dir; mystack[top++] = temp; return 1; break; case 1: //向下走 if(y+2>6 || diamond[y+1][x]!='*' || diamond[y+2][x]!='!') { return 0; } diamond[y][x] = diamond[y+1][x] = '!'; diamond[y+2][x] = '*'; temp.sx = x; temp.sy = y; temp.tx = x; temp.ty = y+2; temp.dir = dir; mystack[top++] = temp; return 1; break; case 2: //向左走 if(x-2<0 || diamond[y][x-1]!='*' || diamond[y][x-2]!='!') { return 0; } diamond[y][x] = diamond[y][x-1] = '!'; diamond[y][x-2] = '*'; temp.sx = x; temp.sy = y; temp.tx = x-2; temp.ty = y; temp.dir = dir; mystack[top++] = temp; return 1; break; case 3: //向上走 if(y-2<0 || diamond[y-1][x]!='*' || diamond[y-2][x]!='!') { return 0; } diamond[y][x] = diamond[y-1][x] = '!'; diamond[y-2][x] = '*'; temp.sx = x; temp.sy = y; temp.tx = x; temp.ty = y-2; temp.dir = dir; mystack[top++] = temp; return 1; break; default: break; } return 0;}int main(){ Init_diamond(); top = nx = ny = ndir = 0; // 回溯遍历,直到找到一个解 while(1) { if(Left_diamond == 1 && (diamond[endx][endy] == '*' || (endx==0 && endy==0))) //结束 { break; } for(y=ny; y<7; y++,nx=0) { for(x=nx; x<7; x++,ndir=0) { for(int dir=ndir; dir<4; dir++) { if(Move_diamond(y, x, dir)) { Left_diamond--; nx = ny = ndir = 0; goto nextstep; } } } }nextstep: if(y == 7) { top--; // 回到上一步, 并改变方向 if(top >= 0) { last_step = mystack[top]; diamond[(last_step.sy + last_step.ty)/2][(last_step.sx + last_step.tx)/2] = '*'; diamond[last_step.sy][last_step.sx] = '*'; diamond[last_step.ty][last_step.tx] = '!'; nx = last_step.sx; ny = last_step.sy; ndir = last_step.dir + 1; Left_diamond++; } else { cout<<"对不起,找不到答案!"<<endl; break; } } } // 输出解 cout<<"下面是程序执行过程中棋子跳动的数据变化过程!"<<"\n"; for(int n=0; n<top; n++) cout << "(" << mystack[n].sx <<","<< mystack[n].sy<<") ("<<mystack[n].tx<< "," <<mystack[n].ty<< ")" <<endl; return 0;}
0 0
- 独立钻石跳棋问题
- 独立钻石跳棋问题的C++实现
- 独立钻石棋
- 独立钻石棋详解
- 求助独立钻石算法实现
- C/C++跳棋问题
- 跳棋
- 跳棋
- 跳棋
- 跳棋
- 跳棋
- 跳棋
- 独立钻石棋(Diamond Chess)算法
- 马跳棋盘的问题
- Lambda FAQ_16.钻石问题
- DiamondProblem(钻石问题)
- 【转自cnblogs】钻石问题
- Hopping dots (独立钻石棋变种)攻略
- 非常有用的 Oracle 查询语句
- AlertDialog的自定义View以及弹出后的亮度控制
- jquery加选中样式
- Android中Context、getApplication、getApplicationContext、this、Activity.this的区别
- 对于别人的工作
- 独立钻石跳棋问题
- xsd自动补全和提示
- QEMU 对虚机的地址空间管理
- java 泛型在类,接口和方法上的应用
- 大型网站的 HTTPS 实践(四)——协议层以外的实践
- 使用 .Net Memory Profiler 诊断 .NET 应用内存泄漏(方法与实践)
- iOS 将图片等比例缩放
- Nepxion分布式RPC框架 - 管理中心
- js+HTML5实现视频截图的方法