迷宫问题c语言
Ⅰ c语言迷宫问题求救!
可电脑自动创建迷宫的迷宫
#include<stdio.h>
#include<stdlib.h>
#define STACK_INI_SIZE 100
#define STACKINCREMENT 50
#define NULL 0
typedef struct
{
int *top;
int *base;
int length;
int stacksize;
}sqstack;
int mg[25][25];
int m=1,n=1,x,y;
/*********************************************/
main()
{
void initstack(sqstack *s);/*初始化栈*/
void stackpush(sqstack *s,int);/*增加栈顶*/
void stackpop(sqstack *s);/*撤销栈顶*/
void stackinput(sqstack *s);/*输出栈*/
int mgway(sqstack *s);/*迷宫路径*/
void destorystack(sqstack *s);/*撤销栈S*/
void makemg(sqstack *s);/*制造迷宫*/
sqstack s;
int i,flag1=1,flag2=1,flag3=1;
char ch;
srand((unsigned)time(NULL));
while(flag1)
{
initstack(&s);
flag2=1;
makemg(&s);
i=mgway(&s);
if(i==0)
printf("此迷宫没有通路\n");
else
stackinput(&s);
destorystack(&s);
printf("还继续么?");
fflush(stdin);
scanf("%c",&ch);
while(flag2)
{
if(ch=='n'||ch=='N')
{
flag1=0;
flag2=0;
}
else if(ch=='y'||ch=='Y')
{
flag1=1;
flag2=0;
}
else
{
printf("输入错误请重新输入");
fflush(stdin);
scanf("%c",&ch);
}
}
}
}
/*********************************************/
void initstack(sqstack *s)/*初始化栈*/
{
s->top=s->base=(int *)malloc(STACK_INI_SIZE*sizeof(int));
if(!s->base)
exit(1);
s->stacksize=STACK_INI_SIZE;
*(s->base)=0;
s->top++;
*(s->top)=101;
s->top++;
s->length=2;
}
/*********************************************/
void stackpush(sqstack *s,int i)/*增加栈顶*/
{
if(s->length>=s->stacksize)
{
printf("路过");
s->base=(int *)realloc(s->base,(STACK_INI_SIZE+STACKINCREMENT)*sizeof(int));
if(!s->base)
exit(1);
s->top=s->base+s->length;
s->stacksize+=STACKINCREMENT;
stackinput(s);
}
if(s->length==0)
{
*(s->base)=i;
s->top++;
s->length++;
}
else
{
*(s->top)=i;
s->top++;
s->length++;
}
}
/*********************************************/
void stackpop(sqstack *s)/*删除栈顶*/
{
if(s->length==0)
printf("栈空 无法删除");
if(s->length==1)
{
s->top--;
*(s->top)=*(s->base)=NULL;
s->length=0;
}
else
{
s->top--;
*(s->top)=NULL;
s->length--;
}
}
/*********************************************/
void stackinput(sqstack *s)/*迷宫栈的输出*/
{
int w,x,y,z,i=0,*p;
p=s->base;
p++;
printf("迷宫正确路径\n");
while(p!=s->top)
{
printf("(%d%d,%d%d)",x=*p/1000,y=*p/100%10,z=*p%100/10,w=*p%10);
p++;
i++;
if(i==7)
{
printf("\n");
i=0;
}
}
}
/*********************************************/
int mgway(sqstack *s)/*迷宫路径*/
{
int gudge(sqstack *s,int);/*判断是否能通行*/
int homing(sqstack *s);/*退栈后I所对应的方向*/
int i=1,j,k;
while(s->top!=s->base)
{
if(i<5)
j=gudge(s,i);
if(j==1&&i<5)
{
k=m*100+n;
stackpush(s,k);
if(m==y-2&&n==x-2)
{
return(1);
}
else
i=1;
}
else
{
if(m==0&&n==0)
return(0);
else if(i==4||i==5)
{
i=homing(s);
stackpop(s);
i++;
}
else
i++;
}
}
return(0);
}
/*********************************************/
int gudge(sqstack *s,int i)/*退栈后i所对应的方向*/
{
int echo(sqstack *s);
if(i==1)
n++;
if(i==2)
m++;
if(i==3)
n--;
if(i==4)
m--;
if((mg[m][n]==0||mg[m][n]==2)&&echo(s))
return(1);
else
{
if(i==1)
n--;
if(i==2)
m--;
if(i==3)
n++;
if(i==4)
m++;
return(0);
}
}
/*********************************************/
int echo(sqstack *s)
{
int i,*p,*q;
i=m*100+n;
p=s->top;
q=s->base;
q++;
p--;
while(p!=q&&*p!=i)
p--;
if(*p==i)
return(0);
else
return(1);
}
/*********************************************/
int homing(sqstack *s)/*i退位后所对应的方向*/
{
int a,b,c,d,*q;
q=s->top;
q--;
a=(*q)/100;
b=(*q)%100;
q--;
c=(*q)/100;
d=(*q)%100;
m=(*q)/100;
n=(*q)%100;
if(a==c)
{
if(d-b==1)
return(3);
else if(d-b==-1)
return(1);
}
else if(b==d)
{
if(c-a==1)
return(4);
else if(c-a==-1)
return(2);
}
return(0);
}
void destorystack(sqstack *s)
{
if(*(s->base)!=NULL)
free(s->base);
s->length=0;
}
/*********************************************/
void makemg(sqstack *s)/*创建迷宫及输出迷宫图的函数*/
{
int mgway(sqstack *s);
void clearstack(sqstack *s);
int flag=1,flag2=1,i,j,k;
char ch;
while(flag)
{
printf("请输入迷宫的长宽(长度范围2-15,宽范围2-15)\n");
printf("输入长");
fflush(stdin);
scanf("%d",&y);
printf("输入宽");
fflush(stdin);
scanf("%d",&x);
if(x<16&&x>3&&y>3&&y<16)
flag=0;
if(flag==0)
printf("自动筛选能通过的迷宫需要一段时间,请耐心等待,如20秒后无法显示出迷宫样本请重试... ...\n");
}
flag=1;
while(flag2)
{
m=1;
n=1;
for(i=0;i<x;i++)
for(j=0;j<y;j++)
mg[i][j]=rand()%3;
i=0;
for(j=0;j<y;j++)
{
mg[i][j]=1;
mg[j][i]=1;
}
i=y-1;
for(j=0;j<x;j++)
mg[j][i]=1;
i=x-1;
for(j=0;j<y;j++)
mg[i][j]=1;
mg[1][1]=0;
mg[x-2][y-2]=0;
k=mgway(s);
if(k==1)
flag2=0;
else
clearstack(s);
}
printf(" ");
for(i=0;i<y;i++)
printf("%2d",i);
printf("\n ");
for(i=0;i<y;i++)
printf("↓");
printf("\n");
for(i=0;i<x;i++)
{
printf("%2d→",i);
for(j=0;j<y;j++)
{
if(mg[i][j]==2)
printf("0 ");
else
printf("%d ",mg[i][j]);
}
printf("\n");
}
}
/*********************************************/
void clearstack(sqstack *s)/*清空栈对于此程序而言*/
{
s->top=s->base;
*(s->base)=0;
s->top++;
*(s->top)=101;
s->top++;
s->length=2;
}
Ⅱ 数据结构C语言版的迷宫问题如何解决
//迷宫实现 。。。
#include<iostream>
using namespace std;
class Stack
{
public:
void clear();
bool push(const int item);
bool pop(int & item);
bool Top(int & item);
bool isEmpty();
bool isFull();
};
class arrStack: public Stack
{
private:
int mSize;
int top;
int *st;
public:
arrStack(int size)
{
mSize=size;
top=-1;
st=new int[mSize];
}
arrStack()
{
top=-1;
}
~arrStack()
{
delete []st;
}
void clear()
{
top=-1;
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
bool push(const int item)
{
if(top==mSize-1)
{
cout<<"栈满溢出。。。"<<endl;
return false;
}
else
{
st[++top]=item;
return true;
}
}
bool pop(int &item)
{
if(top==-1)
{
cout<<"栈空。。。"<<endl;
return false;
}
else
{
item=st[top--];
return true;
}
}
bool Top(int &item)
{
if(top==-1)
{
cout<<"栈空,不能读取栈顶元素。。。"<<endl;
return false;
}
else
{
item=st[top];
return true;
}
}
};
void main()
{
arrStack ly(100);
int i=1,j=1,cur,m,n;
int a[9][9];
while(i==0||j==0)
{
a[i][j]=0; // =0为墙
}
a[1][3]=a[1][7]=a[2][3]=a[2][7]=a[3][4]=a[3][5]=a[3][6]=a[4][2]=a[4][3]=a[4][4]=a[5][4]=0;
a[6][2]=a[6][6]=a[7][2]=a[7][3]=a[7][4]=a[7][6]=a[7][7]=a[8][1]=0;
a[1][1]=1;
do
{
if(a[i][j]!=0&&a[i][j]!=2&&a[i][j]!=3)
{
ly.push(i);
ly.push(j);
a[i][j]=3; //走过为3
if(i==8&&j==8)
{
cout<<"成功。。。"<<endl;
break;
}
else
{
i=i;
j++;
}
}
else
{
if(!ly.isEmpty()&&(a[i+1][j-1]!=0)&&(a[i+1][j-1]!=2))
{
i++;
j--;
}
else
{
i=i;
j--;
a[i][j+1]=2;
}
if(((!ly.isEmpty()&&(a[i+1][j]!=0)&&(a[i+1][j]!=2)&&(a[i+1][j]!=3))||(!ly.isEmpty()&&(a[i][j+1]!=0)&&(a[i][j+1]!=2)&&a[i][j+1]!=2)||((!ly.isEmpty()&&a[i-1][j]!=0)&&(a[i-1][j])!=2&&(a[i-1][j])!=3))||((!ly.isEmpty()&&a[i][j-1]!=0)&&(a[i][j-1]!=2)&&(a[i][j-1])!=3))
{}
//if(!ly.isEmpty()&&(((a[i+1][j]==0)||(a[i+1][j]==2)||(a[i+1][j]==3))&&((a[i][j+1]==0)||(a[i][j+1]==2)||(a[i][j+1]==3))&&((a[i-1][j]==0)||(a[i-1][j])==2)||(a[i-1][j]==3)))
else
{
ly.pop(m);
ly.pop(n);
i=m;
j=n;
a[i][j]=2; //已走不通为2
ly.Top(m);
ly.Top(n);
i=n;
j=m;
}
}
}while(1);
cout<<"输出迷宫路线。。。:"<<endl;
do
{
ly.pop(m);
ly.pop(n);
cout<<"("<<n<<","<<m<<")"<<" ";
}while(!ly.isEmpty());
cout<<endl;
}
整不好你跟我都一个学校。。。
Ⅲ 数据结构迷宫问题(c语言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 10//迷宫包括外墙最大行列数目
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define STACK_INIT_SIZE 100 //存储空间初始分配量
typedef struct{
int r,c;
} PosType;//迷宫中r行c列的位置
typedef struct MazeType{
int r;
int c;
char arr[MAXLEN][MAXLEN];//可取' ''*' '@' '#'
}MazeType; //迷宫类型
typedef struct{
int step; // 当前位置在路径上的“序号”
PosType seat; //当前的坐标位置
int di; //往下一坐标位置的方向
}SElemType;
typedef struct NodeType{
SElemType data;
NodeType *next;
}NodeType,*LinkType;//结点类型
typedef struct SqStack{
LinkType top;
int stacksize;
}SqStack; //栈类型
PosType start;
PosType end;
MazeType maze;
bool found;
Status InitStack(SqStack &S){
S.top=(LinkType)malloc(sizeof(NodeType));
S.top->next=NULL;
S.stacksize=0;
return OK;
}//InitStack;
Status Push(SqStack &S,SElemType &e){
LinkType p;
p=(NodeType*)malloc(sizeof(NodeType));
if(!p) return ERROR;
p->data=e;
p->next=S.top;
S.top=p;
S.stacksize++;
//S.top=S.base+S.stacksize;
//S.top->data=e;
return OK;
}//Push
Status StackEmpty(SqStack S){
if(S.top->next==NULL) return OK;
return ERROR;
}//StackEmpty
Status Pop(SqStack &S,SElemType &e){
LinkType p;
if(StackEmpty(S)) return ERROR;
p=S.top;
e=p->data;
S.top=S.top->next;
S.stacksize--;
free(p);
return OK;
// 栈顶元素出栈
}//Pop
Status DestroyStack(SqStack &S){//销毁栈S,
LinkType p;
while(S.top!=NULL){
p=S.top;
S.top=S.top->next;
free(p);
}//while
if(S.top==NULL) return OK;
else return ERROR;
}//DestroyStack
Status MarkPrint(MazeType &maze,PosType curpos){
maze.arr[curpos.r][curpos.c]='@';//"@"表示曾走过但不通
return OK;
}//Markprint曾走过但不是通路标记并返回OK
Status FootPrint(MazeType &maze,PosType curpos){
maze.arr[curpos.r][curpos.c]='*';//"*"表示可通
return OK;
}//FootPrint
PosType NextPos(PosType &curpos,int i){
PosType cpos;
cpos=curpos;
switch(i){ //1.2.3.4分别表示东,南,西,北方向
case 1 : cpos.c+=1; break;
case 2 : cpos.r+=1; break;
case 3 : cpos.c-=1; break;
case 4 : cpos.r-=1; break;
default: exit(ERROR);
}//switch
return cpos;
}//Nextpos
Status Pass(MazeType &maze, PosType curpos){
/*for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
printf("%4c",maze.arr[i][j]);
}
printf("\n");
}*/
//char a=' ';
//printf("%4d",a);
//printf("%d,%d",curpos.r,curpos.c);
//printf("%4c",maze.arr[1][1]);
if(maze.arr[curpos.r][curpos.c]==' '){//当前位置可通
//printf("go");
return TRUE;
}//if
else{
//printf("go");
return FALSE;
}//else
}//Pass
void InitMaze(MazeType &maze, char a[MAXLEN][MAXLEN], int row, int col){
//printf("go");
maze.r=row;
maze.c=col;
//printf("%d,%d",row,col);
for(int i=0;i<=col+1;i++){a[0][i]='1';a[row+1][i]='1';}
//printf("go");
for(i=0;i<=row+1;i++){a[i][0]='1';a[i][col+1]='1';}
//printf("go");
/*for(i=0;i<10;i++){
for(int j=0;j<10;j++){
printf("%4c",a[i][j]);
}
printf("\n");
}
*/
for(i=0;i<=maze.r+2;i++){
for(int j=0;j<maze.c+2;j++){
if(a[i][j]=='1') maze.arr[i][j]='#';
//if(a[i][j]==0) maze.arr[i][j]=' ';
else maze.arr[i][j]=' ';
}//for
}//for
}//InitMaze
Status MazePath(MazeType &maze,PosType start,PosType end)
{//求解迷宫maze中,从入口start到出口end的一条路径,
//若存在则返回TRUE,否则返回FALSE
PosType curpos;
int curstep;
SqStack S;
SElemType e;
InitStack(S);
curpos=start;//设定“当前位置”为“入口位置”
curstep=1; //探索第一步
found=false;
do{
if(Pass(maze,curpos))
{
//当前位置可以通过,即是未曾走到过的通道块留下足迹
FootPrint(maze,curpos);//做可以通过的标识
e.step=curstep;
e.seat=curpos;
e.di=1;//为栈顶元素赋值
if(Push(S,e)!=OK) return ERROR;
if(curpos.r==end.r && curpos.c==end.c) found=true;//如果到达终点返回true
else{
curpos=NextPos(curpos,1);//下一位置是当前位置的东邻
curstep++;//探索下一步
}//else
}//if
else
if(StackEmpty(S)!=1)
{
Pop(S,e);
while(e.di==4 && !StackEmpty(S))
{
MarkPrint(maze,e.seat);
Pop(S,e);
curstep--;
}//while
if(e.di<4)
{
e.di++;
Push(S,e);//换下个方向探索
curpos=NextPos(e.seat,e.di);
}//if
}//if
}while(StackEmpty(S)!=1&&!found);
DestroyStack(S);
if(found)
return true;
else
return false;//没有找到路径
}//MazePath
void PrintMaze(MazeType &maze){
//将标记路径信息的迷宫输出到终端(包括外墙)
for(int i=0;i<=maze.r+2;i++){
for(int j=0;j<=maze.c+2;j++){
printf("%4c",maze.arr[i][j]);//输出迷宫
}//for
printf("\n");
}//for
}//PrintMaze
void Initialization(){
system("cls");
printf("********************************************************");
printf("\n*CreatMaze--c MazePath--m PrintMaze--p Quit--q*");
printf("\n********************************************************");
}//Initialization
void ReadCommand(char &cmd){
do{
if(cmd=='c'||cmd=='C'){
printf("\n********************************************************");
printf("\n*Enter a operation :m或M *");
printf("\n*退出--Enter a operation :q或Q *");
printf("\n********************************************************");
}//if
else if(cmd=='m'||cmd=='M'){
printf("\n********************************************************");
printf("\n*Enter a operation :p或P *");
printf("\n*退出--Enter a operation :q或Q *");
printf("\n********************************************************");
}//else if
else if(cmd=='0'){
printf("\n********************************************************");
printf("\n*Enter a operation :c或P *");
printf("\n*退出--Enter a operation :q或Q *");
printf("\n********************************************************");
}
printf("\n\n Operration:-");
cmd=getchar();
}while(!(cmd=='c'||cmd=='C'||cmd=='m'||cmd=='M'||cmd=='p'||cmd=='P'||cmd=='q'||cmd=='Q'));
}//ReadCommand
void Interpre(char cmd){
//MazeType maze;//开始的时候定义在这个地方,不是全局的,调用出现了问题。
//PosType start;//开始的时候定义在这个地方,不是全局的,调用出现了问题。
//PosType end;//开始的时候定义在这个地方,不是全局的,调用出现了问题。
switch(cmd){
case 'C':
case 'c':{
int rnum, cnum, i=0,m=1,n=1;
char a2[MAXLEN][MAXLEN];
char input[1];
char data[100000];
printf("\n请输入迷宫数据文件名!\n");
//printf("go");
scanf("%s",input);
FILE *fp;
fp=fopen(input,"r");
//printf("go");
if(!fp){
printf("\n不能打开文件\n");
break;
}//if
//printf("go");
while(!feof(fp)){
fscanf(fp,"%s",&data[i]);
if(i==0){rnum=(int)data[i]-(int)'0';/*printf("%d",rnum);*/}
if(i==1){cnum=(int)data[i]-(int)'0';/*printf("%d",rnum);*/}
if(i>=2){
if(n>cnum){m++;n=1;}
a2[m][n]=data[i];
//printf("%d",a2[m][n]);
n++;
}//if
i++;
}//while
fclose(fp);
//printf("go");
InitMaze(maze, a2, rnum, cnum);
printf("\n迷宫建立完成!!\n");
//PrintMaze(maze);
break;
}//case
case 'M':
case 'm':{
printf("\n请输入迷宫入口的坐标,以空格为间隔:--");
scanf("%d %d",&start.r,&start.c);
printf("\n请输入迷宫出口的坐标,以空格为间隔:--");
scanf("%d %d",&end.r,&end.c);
//printf("%d,%d,%d,%d",start.r,start.c,end.r,end.c);
//PrintMaze(maze);
MazePath(maze, start, end);
//PrintMaze(maze);
break;
}//case
case 'P':
case 'p':{
if(found){
printf("\n求解迷宫的结果如下--\n");
PrintMaze(maze);
}//if
else{
printf("\n找不到路径!\n");
}//else
}//case
}//switch
}//Interpre
void main(){
char cmd='0';
Initialization();
//cmd=getchar();
do{
ReadCommand(cmd);
Interpre(cmd);
}while(cmd!='q'&&cmd!='Q');
}//main
Ⅳ c语言 迷宫问题
1是路径还是0是路径?
#include<cstdio>
#include<cstdlib>
enum se{
path,
block,
end,
mark
};
struct way{
int x;
int y;
}record[1000];
int maze[14][28]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,
1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
void set_end(int ex, int ey){ //设置结束
maze[ex][ey] = end;
}
void printMaze(){
for(int i=0;i<14;i++) //迷宫图形化输出
{
for(int j=0;j<28;j++)
{
if(maze[i][j] == block)
printf("■");
else printf("□");
}
printf("\n");
}
}
void printResult(int length){
for(int i = 0; i < 14; i++) //迷宫路径图形化输出
{
for(int j = 0; j < 28; j++)
{
if(maze[i][j] == mark)
printf(">>");
else if(maze[i][j] == block)
printf("■");
else if(maze[i][j] == end)
printf("<<");
else if(maze[i][j] == path)
printf("□");
}
printf("\n");
}
for(int x = 0; x < length - 1; x++){
printf("<%d,%d> ->", record[x].x, record[x].y);
}
printf("<%d,%d> ", record[x].x, record[x].y);
printf("\n");
}
void search_path(int x, int y, int length){ // 递归寻找路径
if(maze[x][y] == end){
printResult(length);
system("pause");
exit(0);
}
else{
if(maze[x + 1][y] != block && maze[x + 1][y] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x + 1, y, length);
maze[x][y] = path;
length --;
}
if(maze[x - 1][y] != block && maze[x - 1][y] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x - 1, y, length);
maze[x][y] = path;
length --;
}
if(maze[x][y + 1] != block && maze[x][y + 1] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x, y + 1, length);
maze[x][y] = path;
length --;
}
if(maze[x][y - 1] != block && maze[x][y - 1] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x, y - 1, length);
maze[x][y] = path;
length --;
}
return;
}
}
int main(void)
{
set_end(12,26);
printMaze();
search_path(1,5,0);
printf("no path!"); //没有路径
return 0;
}
Ⅳ c语言迷宫问题的详细解法,最好有说明。
//老老实实地算:
int Sum1(int n)
{
int nSum = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= i; j++)
nSum += j;
return nSum;
}
//高度技巧地算(递归):
int Sum2(int n)
{
if(n == 1)return 1;
else return n * (n + 1) / 2 + Sum2(n - 1);
}
//投机取巧地算:
int Sum3(int n)
{
int nSum = 0;
//1+1+2+1+2+3+1+2+3+4+1+2+3+4......+N 可以化简为:
//1 * N + 2 * (N - 1) + 3 * (N - 3) + ... + N * 1
for(int i = 1; i <= n; i++)
nSum += i * (n - i + 1);
return nSum;
}
//绝对偷懒地算:
int Sum4(int n)
{
//根据求和公式可以计算得到:
// 1+1+2+1+2+3+1+2+3+4+1+2+3+4......+N
// = N * (N + 1) * (N + 2) / 6
//(这里就不详细介绍计算过程啦,中学课本里就有)
//所以简单起见,直接返回计算值就可以啦
return n * (n + 1) * (n + 2) / 6;
}
至于哪个更精简,自己看着办吧:-)
Ⅵ c语言做的迷宫问题
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>struct node
{
int sign;//标识,0什么都不在,1在open中,2在closed中
int flag;//标志位 0/1,0可以走,1不可以走
int f,g,h;//判断函数
int x,y;//坐标
int old;//是否old节点,0非,1是
};struct link
{
node fnode;
link *next;
link *pri;
};link *open,*closed,*bestnode,*successor,*p,*q,*r,*s;int maze_flag[7][7]={ {0,1,0,0,0,0,0},
{0,1,0,1,0,1,0},
{0,1,0,0,0,1,0},
{0,1,0,1,0,1,0},
{0,0,0,1,0,0,0},
{1,1,0,1,0,1,0},
{0,0,0,0,0,1,0}};//表示迷宫的数组,0可以走,1不可以走node maze[7][7];int judge(node n)//判断函数,判断n节点是否可以走
{
if(n.flag==1)
return(1);
else
return(0);
}void in_open(node n)//将n节点放入open表
{
p=open;
while(p->next!=open)
{
if(n.f>=p->fnode.f)
{
p->next->pri=(link *)malloc(sizeof(link));
p->next->pri->pri=p;
p=p->next;
p->pri->next=p;
p->pri->pri->next=p->pri;
p=p->pri;
p->fnode.flag=n.flag;
p->fnode.f=n.f;
p->fnode.g=n.g;
p->fnode.h=n.h;
p->fnode.x=n.x;
p->fnode.y=n.y;
p->fnode.old=n.old;
p->fnode.sign=n.sign=1;
}
else
p=p->next;
}
open->pri=(link *)malloc(sizeof(link));
open->pri->pri=p;
open->pri->next=open;
p->next=open->pri;
p=p->next;
p->fnode.flag=n.flag;
p->fnode.f=n.f;
p->fnode.g=n.g;
p->fnode.h=n.h;
p->fnode.x=n.x;
p->fnode.y=n.y;
p->fnode.old=n.old;
p->fnode.sign=n.sign=1;
}void out_open(node n)//将n节点从open表中移出
{
p=open;
while(p->next!=open)
{
if(n.f=p->fnode.f)
{
link *p1;
p1=p->next;
p->next=p->next->next;
p->next->pri=p;
free(p1);
n.sign=0;
}
else
p=p->next;
}
}void in_closed(node n)//将n节点放入closed表
{
while(q->next!=closed)
{
if(n.f>=q->fnode.f)
{
q->next->pri=(link *)malloc(sizeof(link));
q->next->pri->pri=q;
q=q->next;
q->pri->next=p;
q->pri->pri->next=q->pri;
q=q->pri;
q->fnode.flag=n.flag;
q->fnode.f=n.f;
q->fnode.g=n.g;
q->fnode.h=n.h;
q->fnode.x=n.x;
q->fnode.y=n.y;
q->fnode.old=n.old;
q->fnode.sign=n.sign=2;
}
else
q=q->next;
}
closed->pri=(link *)malloc(sizeof(link));
closed->pri->pri=q;
closed->pri->next=closed;
q->next=closed->pri;
q=q->next;
q->fnode.flag=n.flag;
q->fnode.f=n.f;
q->fnode.g=n.g;
q->fnode.h=n.h;
q->fnode.x=n.x;
q->fnode.y=n.y;
q->fnode.old=n.old;
q->fnode.sign=n.sign=2;
}void out_closed(node n)//将n节点从closed表中移出
{
q=closed;
while(q->next!=closed)
{
if(n.f=q->fnode.f)
{
link *q1;
q1=q->next;
q->next=q->next->next;
q->next->pri=q;
free(q1);
n.sign=0;
}
else
q=q->next;
}
}void in_bestnode(node n)//将n节点设为bestnode节点
{
while(r->next!=bestnode)
{
if(n.f>=r->fnode.f)
{
r->next->pri=(link *)malloc(sizeof(link));
r->next->pri->pri=r;
r=r->next;
r->pri->next=r;
r->pri->pri->next=r->pri;
r=r->pri;
r->fnode.flag=n.flag;
r->fnode.f=n.f;
r->fnode.g=n.g;
r->fnode.h=n.h;
r->fnode.x=n.x;
r->fnode.y=n.y;
r->fnode.old=n.old;
}
else
r=r->next;
}
bestnode->pri=(link *)malloc(sizeof(link));
bestnode->pri->pri=r;
bestnode->pri->next=bestnode;
r->next=bestnode->pri;
r=r->next;
r->fnode.flag=n.flag;
r->fnode.f=n.f;
r->fnode.g=n.g;
r->fnode.h=n.h;
r->fnode.x=n.x;
r->fnode.y=n.y;
r->fnode.old=n.old;
}void out_bestnode(node n)//将n节点的bestnode去掉
{
r=bestnode;
while(r->next!=bestnode)
{
if(n.f=p->fnode.f)
{
link *r1;
r1=r->next;
r->next=r->next->next;
r->next->pri=r;
free(r1);
}
else
r=r->next;
}
}void in_successor(node n)//将n节点设置为successor节点
{
s=successor;
while(s->next!=successor)
{
if(n.f>=s->fnode.f)
{
s->next->pri=(link *)malloc(sizeof(link));
s->next->pri->pri=s;
s=p->next;
s->pri->next=s;
s->pri->pri->next=s->pri;
s=s->pri;
s->fnode.flag=n.flag;
s->fnode.f=n.f;
s->fnode.g=n.g;
s->fnode.h=n.h;
s->fnode.x=n.x;
s->fnode.y=n.y;
s->fnode.old=n.old;
}
else
s=s->next;
}
successor->pri=(link *)malloc(sizeof(link));
successor->pri->pri=s;
successor->pri->next=successor;
s->next=successor->pri;
s=s->next;
s->fnode.flag=n.flag;
s->fnode.f=n.f;
s->fnode.g=n.g;
s->fnode.h=n.h;
s->fnode.x=n.x;
s->fnode.y=n.y;
s->fnode.old=n.old;
}void out_successor(node n)//将n节点的successor去掉
{
s=successor;
while(s->next!=successor)
{
if(n.f=p->fnode.f)
{
link *s1;
s1=s->next;
s->next=s->next->next;
s->next->pri=s;
free(s1);
}
else
s=s->next;
}
}void print(link *n)//输出link类型的表n
{
link *forprint;
forprint=n;
printf("the key is ");
while(forprint->next!=n)
printf("(%d,%d)\n",forprint->fnode.x,forprint->fnode.y);
}int main()
{
//初始化部分
//这部分的功能是将二维的整形数组赋值给node型的二维数组
int i=0,j=0;
for(i=0;i<7;i++)
for(j=0;j<7;j++)
{
maze[i][j].x=i;
maze[i][j].y=j;
maze[i][j].flag=maze_flag[i][j];
if(maze[i][j].flag==0)
{
maze[i][j].h=6-i+6-j;
maze[i][j].sign=maze[i][j].f=maze[i][j].g=maze[i][j].old=0;
}
else
maze[i][j].h=-1;
}
for(i=0;i<7;i++)//输出迷宫示意图
{
for(j=0;j<7;j++)
{
printf("%2d",maze_flag[i][j]);
}
printf("\n");
}
//这部分的功能是将open,closed,bestnode表初始化,都置为空表
p=open=(link *)malloc(sizeof(link));
open->next=open;
open->pri=open;
q=closed=(link *)malloc(sizeof(link));
closed->next=closed;
closed->pri=closed;
r=bestnode=(link *)malloc(sizeof(link));
bestnode->next=bestnode;
bestnode->pri=bestnode;
//将第一个元素即(0,0)节点放入open表,开始算法
in_open(maze[0][0]);
maze[0][0].f=maze[0][0].h;
link *s2;
s2=successor;
if(open->next!=open)//open表为空时则失败退出
{
while(1)
{
in_bestnode(open->fnode);//将open表的第一个元素放入bestnode中
in_closed(maze[open->fnode.x][open->fnode.y]);//将open表的第一个元素放入closed中
maze[open->fnode.x][open->fnode.y].g++;//将open表的第一个元素的g值加一,表示已经走了一步
out_open(maze[open->fnode.x][open->fnode.y]);//将open表的第一个元素删除 if(bestnode->fnode.x==6&&bestnode->fnode.y==6)//若bestnode是目标节点,则成功退出
{
printf("succes!!\nthen print the key:\n");
print(closed);
break;
}
else//若bestnode不是目标节点,则扩展其临近可以走的节点为successor
{
if(i==0||j==0||i==6||j==6)
{
if(i==0&&j==0)//若为(0,0),则判断右边和下边的元素
{
if(judge(maze[i][j+1])==0)
in_successor(maze[i][j+1]);
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
}
else if(i==0&&j==6)//若为(0,6),则判断左边和下边的元素
{
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
}
else if(i==6&&j==0)//若为(6,0),则判断左边和上边的元素
{
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
}
else if(i==6&&j==6)//若为(6,6),则判断左边和上边的元素
{
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
}
else if(i==0)//若为第一行的元素(不在角上),则判断左边,下边和右边
{
if(judge(maze[i][j+1])==0)
in_successor(maze[i][j+1]);
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
}
else if(i==6)//若为第七行的元素(不在角上),则判断左边,上边和右边
{
if(judge(maze[i][j+1])==0)
in_successor(maze[i][j+1]);
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
}
else if(j==0)//若为第一列的元素(不在角上),则判断右边,下边和上边
{
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i][j+1])==0)
in_successor(maze[i][j+1]);
}
else if(j==6)//若为第七列的元素(不在角上),则判断左边,上边和上边
{
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
}
}
else//若为中将的元素,则判断四个方向的节点
{
if(judge(maze[i][j-1])==0)
in_successor(maze[i][j-1]);
if(judge(maze[i][j+1])==0)
in_successor(maze[i][j+1]);
if(judge(maze[i-1][j])==0)
in_successor(maze[i-1][j]);
if(judge(maze[i+1][j])==0)
in_successor(maze[i+1][j]);
}
}
while(s2->next!=successor)//对所有的successor节点进行下列操作
{
maze[s2->fnode.x][s2->fnode.y].g=bestnode->fnode.g+bestnode->fnode.h;//计算g(suc)=g(bes)+h(bes,suc)
if(s2->fnode.sign==1)//若在open表中,则置为old,记下较小的g,并从open表中移出,放入closed表中
{
s2->fnode.old=1;
if(s2->fnode.g<maze[s2->fnode.x][s2->fnode.y].g)
{
maze[s2->fnode.x][s2->fnode.y].g=s2->fnode.g;
maze[s2->fnode.x][s2->fnode.y].f=maze[s2->fnode.x][s2->fnode.y].g+maze[s2->fnode.x][s2->fnode.y].h;
out_open(maze[s2->fnode.x][s2->fnode.y]);
in_closed(maze[s2->fnode.x][s2->fnode.y]);
maze[s2->fnode.x][s2->fnode.y].old=0;
}
else
continue;
}
else if(s2->fnode.sign==2)//若在closed表中,则置为old,记下较小的g,并将old从closed表中移出,将较小的g的节点放入closed表中
{
s2->fnode.old=1;
if(s2->fnode.g<maze[s2->fnode.x][s2->fnode.y].g)
{
maze[s2->fnode.x][s2->fnode.y].g=s2->fnode.g;
maze[s2->fnode.x][s2->fnode.y].f=maze[s2->fnode.x][s2->fnode.y].g+maze[s2->fnode.x][s2->fnode.y].h;
out_closed(maze[s2->fnode.x][s2->fnode.y]);
in_closed(maze[s2->fnode.x][s2->fnode.y]);
maze[s2->fnode.x][s2->fnode.y].old=0;
}
else
continue;
}
else//若即不再open表中也不在closed表中,则将此节点放入open表中,并计算此节点的f值
{
in_open(maze[s2->fnode.x][s2->fnode.y]);
maze[s2->fnode.x][s2->fnode.y].f=maze[s2->fnode.x][s2->fnode.y].g+maze[s2->fnode.x][s2->fnode.y].h;
}
s2=s2->next;
}
s2=successor;
}
}
else
printf("error!!This maze does not have the answer!");
return(0);
}
Ⅶ C语言 用队列求解迷宫问题
#include <stdio.h>
#define Maxsize 100
#define N 10
int M[N+2][N+2]=
{
{1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,0,1},
{1,0,0,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,1,0,0,1},
{1,0,1,1,1,1,1,1,1,0,1,1},
{1,0,0,0,0,1,1,0,1,0,0,1},
{1,1,0,1,0,0,0,0,1,0,1,1},
{1,0,0,1,1,0,1,0,0,0,0,1},
{1,0,1,1,0,0,0,1,1,1,1,1},
{1,0,0,0,0,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1}
};
struct path
{
int i,j,p;
}Q[Maxsize];
int front=-1;
int rear=-1;
void print(int front)
{
int k=front,i=0,j;
while(k!=0)
{
j=k;
k=Q[k].p;
Q[j].p=-1;
}
printf("迷宫最快捷路径如下:\n");
k=0;
while(k<Maxsize)
{
if(Q[k].p==-1)
{
i++;
printf("{%d,%d}\t",Q[k].i,Q[k].j);
M[Q[k].i][Q[k].j]=0;
if(i%5==0)
printf("\n");
}
k++;
}
printf("\n迷宫最快捷路径如图所示:\n");
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(M[i][j]==-1)
printf("%c%c",161,245);
else if(M[i][j]==0)
printf("%c%c",161,242);
else
printf("%c%c",161,246);
}
printf("\n");
}
}
int labyrinth(int x1,int y1,int x2,int y2)
{
int i,j,find=0,di;
rear++;
Q[rear].i=x1;Q[rear].j=y1;Q[rear].p=-1;
M[1][1]=-1;
while(front<=rear&&find==0)
{
front++;
i=Q[front].i;j=Q[front].j;
if(i==x2&&j==y2)
{
find=1;
print(front);
return 1;
}
for(di=0;di<4;di++)
{
switch(di)
{
case 0:
i=Q[front].i-1;j=Q[front].j;break;
case 1:
i=Q[front].i;j=Q[front].j+1;break;
case 2:
i=Q[front].i+1;j=Q[front].j;break;
case 3:
i=Q[front].i;j=Q[front].j-1;break;
}
if(M[i][j]==0)
{
rear++;
Q[rear].i=i;Q[rear].j=j;Q[rear].p=front;
M[i][j]=-1;
}
}
}
return 0;
}
void main()
{
labyrinth(1,1,N,N);
}
在vc上运行
Ⅷ C语言迷宫问题
#include<stdio.h>
#include<conio.h>
int migong[10][10]= //设置迷宫,最外围1为墙 里边0为可走路径 1为障碍
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1,1,1},
{1,0,1,1,1,1,1,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,0,0,0,1,0,1,1,1,1},
{1,1,1,1,0,0,1,1,1,1},
{1,0,0,0,0,1,1,1,1,1},
{1,0,1,1,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int num;
struct
{
int x,y,d;
}lj[100];//x,y分别为垂直和水平方向
void start()
{
int top=0,x,y,d,find;//d为设置方向,上下左右。find为设置找不找得到路
lj[top].x=1;
lj[top].y=1;
migong[1][1]=-1;
find=0;d=-1;
while(top>-1){
if(lj[top].x==8&&lj[top].y==8)
{
printf("迷宫路径如下:\n");
printf("start->");
for(x=0;x<=top;x++)
{
printf("(%d,%d)-> ",lj[x].x,lj[x].y);//把找到的路径输出
num++;
if(num%8==0)
printf("\n");
}
printf("->end!\n");
}
while(d<4&&find==0){
d++;
switch(d){
case 0:x=lj[top].x-1; y=lj[top].y; break;//方向为上
case 1:x=lj[top].x; y=lj[top].y+1;break;//方向为右
case 2:x=lj[top].x+1; y=lj[top].y; break;//方向为下
case 3:x=lj[top].x; y=lj[top].y-1;}//方向为左
if(migong[x][y]==0)
find=1;
}
if(find==1){ //判断是否找得到
lj[top].d=d;
top++;
lj[top].x=x;
lj[top].y=y;
d=-1;find=0; //重新调整方向
migong[x][y]=-1;}
else{
migong[lj[top].x][lj[top].y]=0;
top--;d=lj[top].d; //找不到的话退栈
}
}
}
void main()
{
start();
getch();
}
Ⅸ c语言的迷宫问题
//寻路_带权重_带障碍_最短_文件地图_不闪------wlfryq------//
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedefstruct
{
intx;
inty;
}_PT;
_PTpt;
introw=0,col=0;
//设置CMD窗口光标位置
voidsetxy(intx,inty)
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
//获取当前CMD当前光标所在位置
voidgetxy()
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDcoordScreen={0,0};//光标位置
CONSOLE_SCREEN_BUFFER_INFOcsbi;
if(GetConsoleScreenBufferInfo(hConsole,&csbi))
{
pt.x=csbi.dwCursorPosition.X;
pt.y=csbi.dwCursorPosition.Y;
}
}
typedefstruct
{
intx;
inty;
inttype;
intv;
}PT;
PT**s=NULL,stack[50],start,end,c;
intpos=0;
voidprt()
{
inti,j;
system("cls");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(s[i][j].type==1)
{
printf("■");
}
elseif(i==end.x&&j==end.y)
{
printf("★");
}
elseif(i==c.x&&j==c.y)
{
printf("◎");
}
else
{
printf("");
}
}
printf(" ");
}
Sleep(500);
}
voidstack_in(PTa)
{
stack[pos++]=a;
}
PTstack_out()
{
inti;
PTt;
t=stack[0];
for(i=0;i<pos-1;i++)
{
stack[i]=stack[i+1];
}
pos--;
returnt;
}
voidfun()
{
PTa;
intx,y,v;
while(1)
{
if(pos==0)
{
break;
}
a=stack_out();
x=a.x;
y=a.y;
if(x==start.x&&y==start.y)
{
break;
}
v=s[x][y].v;
if(x-1>=0&&s[x-1][y].type==0&&(s[x-1][y].v==-1||s[x-1][y].v>v+1))
{
s[x-1][y].v=v+1;
stack_in(s[x-1][y]);
}
if(x+1<=row-1&&s[x+1][y].type==0&&(s[x+1][y].v==-1||s[x-1][y].v>v+1))
{
s[x+1][y].v=v+1;
stack_in(s[x+1][y]);
}
if(y-1>=0&&s[x][y-1].type==0&&(s[x][y-1].v==-1||s[x-1][y].v>v+1))
{
s[x][y-1].v=v+1;
stack_in(s[x][y-1]);
}
if(y+1<=col-1&&s[x][y+1].type==0&&(s[x][y+1].v==-1||s[x-1][y].v>v+1))
{
s[x][y+1].v=v+1;
stack_in(s[x][y+1]);
}
}
}
voidgo(intx,inty)
{
printf(" 按任意键开始 ");
getchar();
intv;
while(1)
{
if(x==end.x&&y==end.y)
{
setxy(0,y+2);
printf("end....");
return;
}
v=s[x][y].v;
if(v==0)
{
return;
}
if(x-1>=0&&s[x-1][y].v==v-1)
{
c=s[x-1][y];
setxy(y*2,x);
x=x-1;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(x+1<=row-1&&s[x+1][y].v==v-1)
{
c=s[x+1][y];
setxy(y*2,x);
x++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y-1>=0&&s[x][y-1].v==v-1)
{
c=s[x][y-1];
setxy(y*2,x);
y--;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y+1<=col-1&&s[x][y+1].v==v-1)
{
c=s[x][y+1];
setxy(y*2,x);
y++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
}
printf(" returngo ");
system("pause");
}
voidGetMapFromFile()
{
inti,j,x,y,len;
charch[50]={'