迷宫小车思路,附带自己的成功代码及一副成功地图(51)
二维数组:用二维数组将坐标保存,如果芯片51则变量最多128个,故适用于地图范围在11*11之内。
暴力走:遇见岔路口默认方向左中右则暴力走出,适用于简单地图。
参加比赛建议暴力走,地图在赛前会给出,将硬件搞好即可,软件问题不大,选用模块建议超声波测距离,最好是4个,保证每一步的精确。车一定足够小,建议将芯片焊在板子上,大小最好在8*8以内,电池建议用蓄电池,298建议用12V,只要硬件好其他的都不是事。
千万不能按格走,很难保证精确度。
按格走代码如下,二维数组有些繁琐,地图长度为8*8;
#include<reg52.h> #define u8 unsigned char #define u16 unsigned int sbit int1=P1^4; sbit int2=P1^5; sbit int3=P1^6; sbit int4=P1^7; sbit hongwai1=P3^5; //???? sbit hongwai2=P3^6; //???? sbit hongwai3=P3^7; u8 ditu[8][8]; u8 ditu1[8]; u8 bushu; char zuoyou,qianhou; u8 fangxiang=1; u8 chalukoushu; u8 sihutong; u8 zuoyoubian; u8 qianhoubian; u8 zuoyoubian1; u8 qianhoubian1; u8 duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; void delay(u16 i) { while(i--); } void display() { u8 i,k; k=zuoyou; // k=ditu[0][0]; // k=ditu[0][0]; // for(i=0;i<2;i++) // { // switch(i) // { P0=duan[k]; //// case 1: P0= ~duan[k];break; // } // delay(50000); // } } void tingzhi() { int1=0; int2=0; int3=0; int4=0; delay(50000); delay(50000); delay(50000); } void houtui() { int1=0; int2=1; int3=0; int4=1; delay(40000); tingzhi(); bushu-=1; } void zuozhuan() { u16 i=20,j=0; for(j=0;j<17000;j++) { int1=1; int2=0; int3=1; int4=0; } i=17; while(i--) { for(j=0;j<2000;j++) { int1=0; int2=0; int3=1; int4=0; } } for(j=0;j<23000;j++) { int1=1; int2=0; int3=1; int4=0; } tingzhi(); bushu+=2; } void youzhuan() { u16 i=20,j=0; for(j=0;j<17000;j++) { int1=1; int2=0; int3=1; int4=0; } i=25; while(i--) { for(j=0;j<2000;j++) { int1=1; int2=0; int3=0; int4=0; } } for(j=0;j<20000;j++) { int1=1; int2=0; int3=1; int4=0; } tingzhi(); bushu+=2; } void qianjin() { int1=1; int2=0; int3=1; int4=0; delay(41000); tingzhi(); bushu+=1; } void kaishidezhizou() { int1=1; int2=0; int3=1; int4=0; delay(50000); delay(50000); tingzhi(); } void fanyouzhuan() { u16 i=20,j=0; for(j=0;j<13000;j++) { int1=0; int2=1; int3=0; int4=1; } i=24; while(i--) { for(j=0;j<2000;j++) { int1=0; int2=1; int3=0; int4=0; } } for(j=0;j<23000;j++) { int1=0; int2=1; int3=0; int4=1; } tingzhi(); bushu-=2; } void fanzuozhuan() { u16 i=20,j=0; for(j=0;j<17000;j++) { int1=0; int2=1; int3=0; int4=1; } i=20; while(i--) { for(j=0;j<2000;j++) { int1=0; int2=0; int3=0; int4=1; } } for(j=0;j<23000;j++) { int1=0; int2=1; int3=0; int4=1; } tingzhi(); bushu-=2; } void jilu() { if( ditu[zuoyou][qianhou]!=90) { ditu[zuoyou][qianhou]=1+bushu; } } void panduan() { if(zuoyou+1>7) { zuoyoubian=1; } if(zuoyou==0) { zuoyoubian1=1; } if(qianhou+1>7) { qianhoubian=1; } if(qianhou==0) { qianhoubian1=1; } } void main()//?????????,?????,???? ????????????д??? { u8 lukoushu;//??????? u8 i,j; //?????????????????????????? zuoyou=0; qianhou=0; fangxiang=1; chalukoushu=0; sihutong=0; while(hongwai3==0) { tingzhi(); //??? } qianjin();//????? bushu=2; while(1) { lukoushu=0; zuoyoubian=0; qianhoubian=0; zuoyoubian1=0; qianhoubian1=0; i=0; j=0; if(zuoyou==8) { zuoyou=0; } if(qianhou==8) { qianhou=0; } if(fangxiang==5) { fangxiang=1; } if(fangxiang==0) { fangxiang=4; } if(hongwai1==1) { i+=1; } if(hongwai2==1) { i+=1; } if(hongwai3==1) { i+=1; } // else if(i>=2&&sihutong==1) //???? // { // chalukoushu-=1; // } panduan(); if(sihutong==1) { ditu[zuoyou][qianhou]=100; if(fangxiang==1) { if(hongwai1==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { lukoushu+=1; } if(hongwai2==1&&ditu[zuoyou+1-zuoyoubian*8][qianhou]==0) { lukoushu+=1; } if(hongwai3==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { lukoushu+=1; } if(lukoushu>0) { sihutong=0; } else if(ditu[zuoyou+1-zuoyoubian*8][qianhou+qianhoubian1*8-1]==(bushu-1)) { ditu[zuoyou][qianhou]=100; fanyouzhuan(); // zuoyou+=1; qianhou+=qianhoubian1*8; qianhou-=1; qianjin(); ditu[zuoyou][qianhou]=100; fangxiang=4; } else if(ditu[zuoyou+zuoyoubian1*8-1][qianhou+qianhoubian1*8-1]==(bushu-1)) { ditu[zuoyou][qianhou]=100; fanzuozhuan(); // zuoyou-=1; qianjin(); qianhou+=qianhoubian1*8; qianhou-=1; ditu[zuoyou][qianhou]=100; fangxiang=2; } else if(ditu[zuoyou][qianhou+qianhoubian1*8-1]==(bushu))//??? { ditu[zuoyou][qianhou]=100; houtui(); qianhou+=qianhoubian1*8; qianhou-=1; ditu[zuoyou][qianhou]=100; } } else if(fangxiang==2) { if(hongwai1==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { lukoushu+=1; } if(hongwai2==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { lukoushu+=1; } if(hongwai3==1&&ditu[zuoyou+1-zuoyoubian*8][qianhou]==0) { lukoushu+=1; } if(lukoushu>0) { sihutong=0; } else if(ditu[zuoyou+zuoyoubian1*8-1][qianhou+qianhoubian1*8-1]==(bushu-1)) { ditu[zuoyou][qianhou]=100 ; fanyouzhuan(); qianjin(); zuoyou+=zuoyoubian1*8; zuoyou-=1; ditu[zuoyou][qianhou]=100; // qianhou-=1; fangxiang=1; } else if(ditu[zuoyou+zuoyoubian1*8-1][qianhou+1-qianhoubian*8]==(bushu-1)) { ditu[zuoyou][qianhou]=100; fanzuozhuan(); qianjin(); zuoyou+=zuoyoubian1*8; zuoyou-=1; ditu[zuoyou][qianhou]=100; // qianhou+=1; fangxiang=3; } else if(ditu[zuoyou+zuoyoubian1*8-1][qianhou]==(bushu)) { ditu[zuoyou][qianhou]=100; houtui(); zuoyou+=zuoyoubian1*8; zuoyou-=1; ditu[zuoyou][qianhou]=100; } } else if(fangxiang==3) { if(hongwai1==1&&ditu[zuoyou+1-zuoyoubian*8][qianhou]==0) { lukoushu+=1; } if(hongwai2==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { lukoushu+=1; } if(hongwai3==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { lukoushu+=1; } if(lukoushu>0) { sihutong=0; } else if(ditu[zuoyou+1-zuoyoubian*8][qianhou+1-qianhoubian*8]==(bushu-1)) { ditu[zuoyou][qianhou]=100 ; fanzuozhuan(); qianjin(); // zuoyou+=1; qianhou+=1; qianhou-=qianhoubian*8; ditu[zuoyou][qianhou]=100; fangxiang=4; } else if(ditu[zuoyou+zuoyoubian1*8-1][qianhou+1-qianhoubian*8]==(bushu-1)) { ditu[zuoyou][qianhou]=100 ; fanyouzhuan(); qianjin(); // zuoyou-=1; qianhou+=1; qianhou-=qianhoubian*8; ditu[zuoyou][qianhou]=100; fangxiang=2; } else if(ditu[zuoyou][qianhou+1-qianhoubian*8]==(bushu)) { ditu[zuoyou][qianhou]=100; houtui(); qianhou+=1; qianhou-=qianhoubian*8; ditu[zuoyou][qianhou]=100; } } else if(fangxiang==4) { if(hongwai1==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { lukoushu+=1; } if(hongwai2==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { lukoushu+=1; } if(hongwai3==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { lukoushu+=1; } if(lukoushu>0) { sihutong=0; } else if(ditu[zuoyou+1-zuoyoubian*8][qianhou+qianhoubian1*8-1]==(bushu-1)) { ditu[zuoyou][qianhou]=100; fanzuozhuan(); qianjin(); zuoyou+=1; zuoyou-=zuoyoubian*8; ditu[zuoyou][qianhou]=100; // qianhou+=1; fangxiang=1; } else if(ditu[zuoyou+1-zuoyoubian*8][qianhou+1-qianhoubian1*8]==(bushu-1)) { ditu[zuoyou][qianhou]=100; fanyouzhuan(); zuoyou+=1; zuoyou-=zuoyoubian*8; qianjin(); ditu[zuoyou][qianhou]=100; // qianhou-=1; fangxiang=3; } else if(ditu[zuoyou+1-zuoyoubian*8][qianhou]==(bushu)) { ditu[zuoyou][qianhou]=100; houtui(); zuoyou+=1; zuoyou-=zuoyoubian*8; ditu[zuoyou][qianhou]=100; } } } else { if(fangxiang==1) { if((hongwai1==0||ditu[zuoyou-1+zuoyoubian1*8][qianhou]==100)&&(hongwai2==0||ditu[zuoyou+1-zuoyoubian*8][qianhou]==100)&&(hongwai3==0||ditu[zuoyou][qianhou+1-qianhoubian*8]==100)) { sihutong=1; } if(hongwai1==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { jilu(); houtui(); qianhou-=1; jilu(); zuozhuan(); panduan(); zuoyou-=1; qianhou+=1; jilu(); fangxiang-=1; } else if(hongwai3==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { if(qianhou==7) { qianjin(); tingzhi(); while(1); } jilu(); qianjin(); qianhou+=1; jilu(); } else if((hongwai2==1)&&((ditu[zuoyou+1-zuoyoubian*8][qianhou])==0)) { jilu(); houtui(); qianhou-=1; jilu(); panduan(); youzhuan(); zuoyou+=1; qianhou+=1; jilu(); fangxiang+=1; } } else if(fangxiang==2) { if((hongwai1==0||ditu[zuoyou][qianhou+1-qianhoubian*8]==100)&&(hongwai2==0||ditu[zuoyou][qianhou+qianhoubian1*8-1]==100)&&(hongwai3==0||ditu[zuoyou+1-zuoyoubian*8][qianhou]==100)) { sihutong=1; } /*if( ditu[zuoyou][qianhou]!=90) { ditu[zuoyou][qianhou]=1+bushu; }*/ if(hongwai1==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { if(qianhou==7) { houtui(); zuozhuan(); tingzhi(); while(1); } jilu(); houtui(); zuoyou-=1; jilu(); zuozhuan(); panduan(); qianhou+=1; zuoyou+=1; jilu(); fangxiang-=1; } else if(hongwai3==1&&ditu[zuoyou+1-zuoyoubian*8][qianhou]==0) { jilu(); qianjin(); zuoyou+=1; jilu(); } else if(hongwai2==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { jilu(); houtui(); zuoyou-=1; jilu(); youzhuan(); panduan(); qianhou-=1; zuoyou+=1; jilu(); fangxiang+=1; } } else if(fangxiang==3) { if((hongwai1==0||ditu[zuoyou+1-zuoyoubian*8][qianhou]==100)&&(hongwai2==0||ditu[zuoyou+zuoyoubian1*8-1][qianhou]==100)&&(hongwai3==0||ditu[zuoyou][qianhou+qianhoubian1*8-1]==100)) { sihutong=1; } if(hongwai1==1&&ditu[zuoyou+1-zuoyoubian*8][qianhou]==0) { jilu(); houtui(); qianhou+=1; jilu(); panduan(); zuozhuan(); zuoyou+=1; qianhou-=1; jilu(); fangxiang-=1; } else if(hongwai3==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { jilu(); qianjin(); qianhou-=1; jilu(); } else if(hongwai2==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { jilu(); houtui(); qianhou+=1; jilu(); panduan(); youzhuan(); zuoyou-=1; qianhou-=1; jilu(); fangxiang+=1; } } else if(fangxiang==4) { if((hongwai1==0||ditu[zuoyou][qianhou+qianhoubian1*8-1]==100)&&(hongwai2==0||ditu[zuoyou][qianhou+1-qianhoubian*8]==100)&&(hongwai3==0||ditu[zuoyou-+zuoyoubian1*81][qianhou]==100)) { sihutong=1; } if(hongwai1==1&&ditu[zuoyou][qianhou+qianhoubian1*8-1]==0) { jilu(); houtui(); zuoyou+=1; jilu(); panduan(); zuozhuan(); qianhou-=1; zuoyou-=1; jilu(); fangxiang-=1; } else if(hongwai3==1&&ditu[zuoyou+zuoyoubian1*8-1][qianhou]==0) { jilu(); qianjin(); zuoyou-=1; jilu(); } else if(hongwai2==1&&ditu[zuoyou][qianhou+1-qianhoubian*8]==0) { if(qianhou==7) { houtui(); youzhuan(); tingzhi(); while(1); } jilu(); houtui(); zuoyou+=1; jilu(); panduan(); youzhuan(); qianhou+=1; zuoyou-=1; jilu(); fangxiang+=1; } } } } }