|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
#define MAXSIZE 100
#define N 8
int board[N][N];
int Addi[N]={1,-1,-2,2,2,1,-1,-2};
int Addj[N]={2,-2,1,1,-1,-2,2,-1};
struct Stack
{
int i;
int j;
int director;
}stack[MAXSIZE];
int top=-1;
void menu()
{
system("color 9a");
printf("\t\t\t******************************\n");
printf("\t\t\t*\t马 踏 棋 盘 *\n");
printf("\t\t\t******************************\n");
printf("\t\t\t* 1.初始化马的起始位置 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 2.浏览寻址结果 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 3.退出 *\n");
printf("\t\t\t******************************\n");
}
int FindPath(int i,int j)
{
int find,director,number,min;
int i1,j1,h,k,s;
int a[8],b1[8],b2[8],d[8];
while(top>-1)
{
for(h=0;h<8;h++)
{
number=0;
i=stack[top].i+Addi[h];
j=stack[top].j+Addj[h];
b1[h]=i;
b2[h]=j;
if(board[i][j]==0&&i>=0&&i<8&&j>=0&&j<8)
{
for(k=0;k<8;k++)
{
i1=b1[h]+Addi[k];
j1=b2[h]+Addj[k];
if(board[i1][j1]==0&&i1>=0&&i1<8&&j1>=0&&j1<8)
number++;
}
a[h]=number;
}
}
for(h=0;h<8;h++)
{
min=9;
for(k=0;k<8;k++)
if(min>a[k])
{
min=a[k];
d[h]=k;
s=k;
}
a[s]=9;
}
director=stack[top].director;
if(top>=63)
return (1);
find=0;
for(h=director+1;h<8;h++)
{
i=stack[top].i+Addi[d[h]];
j=stack[top].j+Addj[d[h]];
if(board[i][j]==0&&i>=0&&i<8&&j>=0&&j<8)
{
find=1;
break;
}
}
if(find==1)
{
stack[top].director=director;
top++;
stack[top].i=i;
stack[top].j=j;
stack[top].director=-1;
board[i][j]=top+1;
}
else
{
board[stack[top].i][stack[top].j]=0;
top--;
}
}
return (0);
}
void Save(int x,int y)
{
int i,j,flag;
FILE *fp;
printf("是否保存结果(1/0):");
scanf("%d",&flag);
if(flag==1)
{
fp=fopen("123.txt","w");
fprintf(fp,"马儿起始坐标为:(%d,%d)\n",x+1,y+1);
for(i=0;i<N;i++,fprintf(fp,"\n\n",""))
{
for(j=0;j<N;j++)
fprintf(fp," %3d ",board[i][j]);
}
fprintf(fp,"\n\n","");
fclose(fp);
printf("\n.........保存结果成功...........\n");
}
else if(flag==0);
else system("pause");
}
void Read()
{
FILE *fp;
char ch;
fp=fopen("123.txt","r");
rewind(fp);
printf("\n\n");
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
void Display(int x,int y)
{
int i,j,num=1;
printf("\n");
while(num<65)
{
printf("\n\n\t马踏棋盘寻址过程演示如下:\n\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
if(board[i][j]==num)
printf("\t%d ",board[i][j]);
else printf("\t0 ");
printf("\n\n");
}
Sleep(500);system("CLS");
num++;
}
system("CLS");
printf("\n\n\t马踏棋盘寻址最终结果如下:\n\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("\t%d ",board[i][j]);
printf("\n\n");
}
printf("\n.............是否保存结果.............");
Save(x,y);
}
void Start(int xi,int yi)
{
int x,y;
top=-1;
top++;
stack[top].i=xi;
stack[top].j=yi;
stack[top].director=-1;
board[xi][yi]=top+1;
x=stack[top].i;
y=stack[top].j;
if(FindPath(x,y))
Display(x,y);
else
printf("无解");
}
int Customer()
{
char Iput2[10],Customer[10]="1234567",ch;
system("color 04");
printf("\t\t\t*********************************\n");
printf("\t\t\t*\t马 踏 棋 盘 *\n");
printf("\t\t\t*********************************\n");
printf("\t\t\t* 马踏棋盘规则:马在棋盘的任意位 *\n");
printf("\t\t\t* 都遵守横两格上下竖一格,或者竖 *\n");
printf("\t\t\t* 两个格子左右横移一个格子 *\n");
printf("\t\t\t* (国际规则) *\n");
printf("\t\t\t*********************************\n");
int i=0;
printf("\n\t\t\t请输入用户账户:");
while(1)
{
ch=getch();
if((ch>=65&&ch<=90||ch>=97&&ch<=122||ch>=48&&ch<=57)&&i<10)
Iput2[i++]=ch;
else if(ch==8)
{
printf("\b \b");
i--;
continue;
}
else break;
printf("*");
}
Iput2[i]='\0';
if(strcmp(Customer,Iput2)==0)return 1;
else return 0;
}
int PassWord()
{
int i=0;
char Pcode[10]="1234567";
char Iput1[10],a;
system("color 04");
printf("\n\t\t\t请输入用户密码:");
while(1)
{
a=getch();
if((a>=65&&a<=90||a>=97&&a<=122||a>=48&&a<=57)&&i<10)
Iput1[i++]=a;
else if(a==8)
{
printf("\b \b");
i--;
continue;
}
else break;
printf("*");
}
Iput1[i]='\0';
if(strcmp(Pcode,Iput1)==0)return 1;
else return 0;
}
int EnterPass()
{
int i=1;
while(1)
{
if(i>3){system("CLS");printf("\n\t\1\1\1\1\1\1\1......输入密码已超过三次,谢谢使用!......\1\1\1\1\1\1\1\n\n");return 0;}
if(Customer()&&PassWord())
{
printf("\n\n.........................登入成功,欢迎使用....................................\n");Sleep(2000);return 1;break;}
else
{
system("CLS");printf("\n\t\t\t账户或密码错误...................\n");
printf("\n\n");
i++;
}
}
}
int main()
{
int x,y;
int i,j,num,ts=10;
if(EnterPass())
{
loop: system("CLS");menu();
printf("\n请用户输入菜单前的功能编号:");
scanf("%d",&num);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
board[i][j]=0;
if(num==1)
{
while(1)
{
printf("\n请输入起始位置的横坐标 x = ");
scanf("%d",&x);
printf("\n请输入起始位置的纵坐标 y = ");
scanf("%d",&y);
if(x>=1&&x<=8&&y>=1&&y<=8)break;
printf("\n输入错误!_请用户按要求输入..............\n");
}
Start(x-1,y-1);
system("pause");
}
else if(num==2){Read();system("pause");}
else return 0;
goto loop;
}
else return 0;
}
|
|