问题描述
- 向大神请教一下我的华容道算法有什么问题,运行到后期马超和黄忠会出现问题,算法相同的张飞和赵云却不会。
-
运行的时候,在移动较多次以后,黄忠和马超会出现问题,比如只交换一个字符,或者连带旁边武将的字符一起交换了,请教一下,谢谢!
下面是头文件
#include
#include
#include
#include
using namespace std;
主函数
#include "header.h"
int main()
{ char order[5];
extern int game();
cout<<"华容道v1.0 欢迎进入游戏"<
label3:
cin>>order;
if (order[0]=='S'&&order[1]=='T'&&order[2]=='A'&&order[3]=='R'&&order[4]=='T') game();
else if (order[0]=='H'&&order[1]=='E'&&order[2]=='L'&&order[3]=='P') cout<<"人物姓名拼音首字母(如CC表示曹操),移动方向:U(表示向上)/D(表示向下)/L(表示向左)/R(表示向右)] 例如:CC U。"<<endl;
else {
cout<<"错误!"<<endl;
goto label3;
}
return 0;
}
解决方案
这儿是game()代码
#include "header.h"
int swapzr(int&x,int&y,int&z)//卒右移指令
{
int temp;
temp=x;
x=y;
y=temp;
z+=1;
return 0;
}
int swapzl(int&x,int&y,int&z)//卒左移指令
{
int temp;
temp=x;
x=y;
y=temp;
z-=1;
return 0;
}
int swapzu(int&x,int&y,int&z)//卒上移指令
{
int temp;
temp=x;
x=y;
y=temp;
z-=1;
return 0;
}
int swapzd(int&x,int&y,int&z)//卒下移指令
{
int temp;
temp=x;
x=y;
y=temp;
z+=1;
return 0;
}
int swapvr(int&x,int&y,int&z,int&m,int&n,int&q)//黄忠马超赵云张飞右移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
return 0;
}
int swapvl(int&x,int&y,int&z,int&m,int&n,int&q)//黄忠马超赵云张飞左移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
return 0;
}
int swapvu(int&x,int&y,int&z,int&m,int&n,int&q)//黄忠马超赵云张飞上移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
return 0;
}
int swapvd(int&x,int&y,int&z,int&m,int&n,int&q)//黄忠马超赵云张飞下移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
return 0;
}
int swapgr(int&x,int&y,int&z,int&m,int&n,int&q)//关羽右移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
return 0;
}
int swapgl(int&x,int&y,int&z,int&m,int&n,int&q)//关羽左移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
return 0;
}
int swapgu(int&x,int&y,int&z,int&m,int&n,int&q)//关羽上移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
return 0;
}
int swapgd(int&x,int&y,int&z,int&m,int&n,int&q)//关羽下移指令
{
int temp1,temp2;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
return 0;
}
int swapcr(int&x,int&y,int&z,int&m,int&n,int&q,int&d,int&e,int&f,int&r,int&s,int&t)//曹操右移指令
{
int temp1,temp2,temp3,temp4;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
temp3=d;
d=e;
e=temp3;
f+=1;
temp4=r;
r=s;
s=temp4;
t+=1;
return 0;
}
int swapcl(int&x,int&y,int&z,int&m,int&n,int&q,int&d,int&e,int&f,int&r,int&s,int&t)//曹操左移指令
{
int temp1,temp2,temp3,temp4;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
temp3=d;
d=e;
e=temp3;
f-=1;
temp4=r;
r=s;
s=temp4;
t-=1;
return 0;
}
int swapcu(int&x,int&y,int&z,int&m,int&n,int&q,int&d,int&e,int&f,int&r,int&s,int&t)//曹操上移指令
{
int temp1,temp2,temp3,temp4;
temp1=x;
x=y;
y=temp1;
z-=1;
temp2=m;
m=n;
n=temp2;
q-=1;
temp3=d;
d=e;
e=temp3;
f-=1;
temp4=r;
r=s;
s=temp4;
t-=1;
return 0;
}
int swapcd(int&x,int&y,int&z,int&m,int&n,int&q,int&d,int&e,int&f,int&r,int&s,int&t)//曹操下移指令
{
int temp1,temp2,temp3,temp4;
temp1=x;
x=y;
y=temp1;
z+=1;
temp2=m;
m=n;
n=temp2;
q+=1;
temp3=d;
d=e;
e=temp3;
f+=1;
temp4=r;
r=s;
s=temp4;
t+=1;
return 0;
}
/*
01黄 02曹 03操 04赵
05忠 06曹 07操 08云
09马 10关 11羽 12张
13超 14Z1 15Z2 16飞
17Z3 18 18 20Z4
*/
int game()
{ extern int main();
int hz1x=0,hz1y=0,hz2x=1,hz2y=0,c1x=0,c1y=1,c2x=0,c2y=2,c3x=1,c3y=1,c4x=1,c4y=2,zy1x=0,zy1y=3,zy2x=1,zy2y=3,gy1x=2,gy1y=1,gy2x=2,gy2y=2,zf1x=2,zf1y=3,zf2x=3,zf2y=3,mc1x=2,mc1y=0,mc2x=3,mc2y=0,z1x=3,z1y=1,z2x=3,z2y=2,z3x=4,z3y=0,z4x=4,z4y=3;
char step[5];
int a[5][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,20};
int i,j;
start:
for (i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
switch(a[i][j])
{
case 1:cout<<" 黄 ";break;
case 2:cout<<" 曹 ";break;
case 3:cout<<" 操 ";break;
case 4:cout<<" 赵 ";break;
case 5:cout<<" 忠 ";break;
case 6:cout<<" 曹 ";break;
case 7:cout<<" 操 ";break;
case 8:cout<<" 云 ";break;
case 9:cout<<" 马 ";break;
case 10:cout<<" 关 ";break;
case 11:cout<<" 羽 ";break;
case 12:cout<<" 张 ";break;
case 13:cout<<" 超 ";break;
case 14:cout<<" Z1 ";break;
case 15:cout<<" Z2 ";break;
case 16:cout<<" 飞 ";break;
case 17:cout<<" Z3 ";break;
case 20:cout<<" Z4 ";break;
default:cout<<" ";break;
}
cout<
}
label2:
cin>>step;
{if (step[0]=='S'&&step[1]=='T'&&step[2]=='A'&&step[3]=='R'&&step[4]=='T') main();
else if(step[0]=='H'&&step[1]=='E'&&step[2]=='L'&&step[3]=='P')
{
cout<<"人物姓名拼音首字母(如CC表示曹操),移动方向:U(表示向上)/D(表示向下)/L(表示向左)/R(表示向右)] 例如:CCU。"<<endl;
goto label2;
}
if(step[0]=='Z'&&step[1]=='1')//Z1移动
{
if(step[2]=='R')
{ if(a[z1x][z1y+1]==18&&z1y<4)swapzr(a[z1x][z1y],a[z1x][z1y+1],z1y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[z1x][z1y-1]==18&&z1y>0)swapzl(a[z1x][z1y],a[z1x][z1y-1],z1y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[z1x-1][z1y]==18&&z1x>0)swapzu(a[z1x][z1y],a[z1x-1][z1y],z1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[z1x+1][z1y]==18&&z1x<5)swapzd(a[z1x][z1y],a[z1x+1][z1y],z1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
} //这段语句于此结束
if(step[0]=='Z'&&step[1]=='2')//Z2移动
{
if(step[2]=='R')
{ if(a[z2x][z2y+1]==18&&z2y<4)swapzr(a[z2x][z2y],a[z2x][z2y+1],z2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[z2x][z2y-1]==18&&z2y>0)swapzl(a[z2x][z2y],a[z2x][z2y-1],z2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[z2x-1][z2y]==18&&z2x>0)swapzu(a[z2x][z2y],a[z2x-1][z2y],z2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[z2x+1][z2y]==18&&z2x<5)swapzd(a[z2x][z2y],a[z2x+1][z2y],z2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
} //这段语句于此结束
if(step[0]=='Z'&&step[1]=='3')//Z3移动
{
if(step[2]=='R')
{ if(a[z3x][z3y+1]==18&&z3y<4)swapzr(a[z3x][z3y],a[z3x][z3y+1],z3y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[z3x][z3y-1]==18&&z3y>0)swapzl(a[z3x][z3y],a[z3x][z3y-1],z3y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[z3x-1][z3y]==18&&z3x>0)swapzu(a[z3x][z3y],a[z3x-1][z3y],z3x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[z3x+1][z3y]==18&&z3x<5)swapzd(a[z3x][z3y],a[z3x+1][z3y],z3x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
} //这段语句于此结束
if(step[0]=='Z'&&step[1]=='4')//Z4移动
{
if(step[2]=='R')
{ if(a[z4x][z4y+1]==18&&z4y<4)swapzr(a[z4x][z4y],a[z4x][z4y+1],z4y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[z4x][z4y-1]==18&&z4y>0)swapzl(a[z4x][z4y],a[z4x][z4y-1],z4y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[z4x-1][z4y]==18&&z4x>0)swapzu(a[z4x][z4y],a[z4x-1][z4y],z4x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[z4x+1][z4y]==18&&z4x<5)swapzd(a[z4x][z4y],a[z4x+1][z4y],z4x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
} //这段语句于此结束
if(step[0]=='H'&&step[1]=='Z')//黄忠移动
{
if(step[2]=='R')
{ if(a[hz1x][hz1y+1]==18&&hz1y<4&&a[hz2x][hz2y+1]==18||a[hz2x][hz2y+1]==19)swapvr(a[hz1x][hz1y],a[hz1x][hz1y+1],hz1y,a[hz2x][hz2y],a[hz2x][hz2y+1],hz2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[hz1x][hz1y-1]==18&&hz1y>0&&a[hz2x][hz2y-1]==18||a[hz2x][hz2y+1]==19)swapvl(a[hz1x][hz1y],a[hz1x][hz1y-1],hz1y,a[hz2x][hz2y],a[hz2x][hz2y-1],hz2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[hz1x-1][hz1y]==18&&hz1x>0)swapvu(a[hz1x][hz1y],a[hz1x-1][hz1y],hz1x,a[hz2x][hz2y],a[hz2x-1][hz2y],hz2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[hz2x+1][hz2y]==18&&hz2x<5)swapvd(a[hz2x][hz2y],a[hz2x+1][hz2y],hz2x,a[hz1x][hz1y],a[hz1x+1][hz1y],hz1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
}//这段语句于此结束
if(step[0]=='M'&&step[1]=='C')//马超移动
{
if(step[2]=='R')
{ if(a[mc1x][mc1y+1]==18&&mc1y<4&&a[mc2x][mc2y+1]==18)swapvr(a[mc1x][mc1y],a[mc1x][mc1y+1],hz1y,a[mc2x][mc2y],a[mc2x][mc2y+1],mc2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[mc1x][mc1y-1]==18&&a[mc2x][mc2y-1]==18&&mc1y>0)swapvl(a[mc1x][mc1y],a[mc1x][mc1y-1],mc1y,a[mc2x][mc2y],a[mc2x][mc2y-1],mc2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(mc1x>0&&a[mc1x-1][mc1y]==18) swapvu(a[mc1x][mc1y],a[mc1x-1][mc1y],mc1x,a[mc2x][mc2y],a[mc2x-1][mc2y],mc2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[mc2x+1][mc2y]==18&&mc2x<5)swapvd(a[mc2x][mc2y],a[mc2x+1][mc2y],mc2x,a[mc1x][mc1y],a[mc2x][mc2y],mc1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
}//这段语句于此结束
if(step[0]=='Z'&&step[1]=='Y')//赵云移动
{
if(step[2]=='R')
{ if(a[zy1x][zy1y+1]==18&&zy1y<4&&a[zy2x][zy2y+1]==18)swapvr(a[zy1x][zy1y],a[zy1x][zy1y+1],hz1y,a[zy2x][zy2y],a[zy2x][zy2y+1],zy2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[zy1x][zy1y-1]==18&&zy1y>0&&a[zy2x][zy2y-1]==18)swapvl(a[zy1x][zy1y],a[zy1x][zy1y-1],zy1y,a[zy2x][zy2y],a[zy2x][zy2y-1],zy2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[zy1x-1][zy1y]==18&&zy1x>0) swapvu(a[zy1x][zy1y],a[zy1x-1][zy1y],zy1x,a[zy2x][zy2y],a[zy2x-1][zy2y],zy2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[zy2x+1][zy2y]==18&&zy2x<5)swapvd(a[zy2x][zy2y],a[zy2x+1][zy2y],zy2x,a[zy1x][zy1y],a[zy1x+1][zy1y],zy1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
}//这段语句于此结束
if(step[0]=='Z'&&step[1]=='F')//张飞移动
{
if(step[2]=='R')
{ if(a[zf1x][zf1y+1]==18&&zf1y<4&&a[zf2x][zf2y+1]==18)swapvr(a[zf1x][zf1y],a[zf1x][zf1y+1],hz1y,a[zf2x][zf2y],a[zf2x][zf2y+1],zf2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[zf1x][zf1y-1]==18&&zf1y>0&&a[zf2x][zf2y-1]==18)swapvl(a[zf1x][zf1y],a[zf1x][zf1y-1],zf1y,a[zf2x][zf2y],a[zf2x][zf2y-1],zf2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[zf1x-1][zf1y]==18&&zf1x>0) swapvu(a[zf1x][zf1y],a[zf1x-1][zf1y],zf1x,a[zf2x][zf2y],a[zf2x-1][zf2y],zf2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[zf2x+1][zf2y]==18&&zf2x<5)swapvd(a[zf2x][zf2y],a[zf2x+1][zf2y],zf2x,a[zf1x][zf1y],a[zf1x+1][zf1y],zf1x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
}//这段语句于此结束
if(step[0]=='G'&&step[1]=='Y')//关羽移动
{
if(step[2]=='R')
{ if(a[gy2x][gy2y+1]==18&&gy2y<4)swapgr(a[gy2x][gy2y],a[gy2x][gy2y+1],gy2y,a[gy1x][gy1y],a[gy1x][gy1y+1],gy1y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='L')
{ if(a[gy1x][gy1y-1]==18&&gy1y>0)swapgl(a[gy1x][gy1y],a[gy1x][gy1y-1],gy1y,a[gy2x][gy2y],a[gy2x][gy2y-1],gy2y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='U')
{ if(a[gy1x-1][gy1y]==18&&gy1x>0&&a[gy2x-1][gy2y]==18)swapgu(a[gy1x][gy1y],a[gy1x-1][gy1y],gy1x,a[gy2x][gy2y],a[gy2x-1][gy2y],gy2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
if(step[2]=='D')
{ if(a[gy1x+1][gy1y]==18&&gy1x<4&&a[gy2x+1][gy2y]==18)swapgd(a[gy1x][gy1y],a[gy1x+1][gy1y],gy1x,a[gy2x][gy2y],a[gy2x+1][gy2y],gy2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
goto start;
}
else goto start;
}//这段语句于此结束
if(step[0]=='C'&&step[1]=='C')//曹操移动
{ if(step[2]=='R')
{ if(a[c2x][c2y+1]==18&&c2y<4&&a[c4x][c4y+1]==18)swapcr(a[c2x][c2y],a[c2x][c2y+1],c2y,a[c4x][c4y],a[c4x][c4y+1],c4y,a[c1x][c1y],a[c1x][c1y+1],c1y,a[c3x][c3y],a[c3x][c3y+1],c3y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
if (c3x==18&&c3y==18&&c4x==18&&c4y==18) goto end;
goto start;
}
if(step[2]=='L')
{ if(a[c1x][c1y-1]==18&&c1y>0&&a[c3x][c3y-1]==18)swapcl(a[c1x][c1y],a[c1x][c1y-1],c1y,a[c3x][c3y],a[c3x][c3y-1],c3y,a[c2x][c2y],a[c2x][c2y-1],c2y,a[c4x][c4y],a[c4x][c4y-1],c4y);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
if (c3x==18&&c3y==18&&c4x==18&&c4y==18) goto end;
goto start;
}
if(step[2]=='U')
{ if(a[c1x-1][c1y]==18&&c1x>0&&a[c2x-1][c2y]==18)swapcu(a[c1x][c1y],a[c1x-1][c1y],c1x,a[c2x][c2y],a[c2x-1][c2y],c2x,a[c3x][c3y],a[c3x-1][c3y],c3x,a[c4x][c4y],a[c4x-1][c4y],c4x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
if (c3x==18&&c3y==18&&c4x==18&&c4y==18) goto end;
goto start;
}
if(step[2]=='D')
{ if(a[c3x+1][c3y]==18&&c3x<4&&a[c4x+1][c4y]==18)swapcd(a[c4x][c4y],a[c4x+1][c4y],c4x,a[c3x][c3y],a[c3x+1][c3y],c3x,a[c1x][c1y],a[c1x+1][c1y],c1x,a[c2x][c2y],a[c2x+1][c2y],c2x);
else cout<<"错误!格子被占用或已经到达边缘!"<<endl;
if (c3x==18&&c3y==18&&c4x==18&&c4y==18) goto end;
goto start;
}
else goto start;
}
end:
cout<<"成功!"<<endl;
return 0;
}
}