手动寻路贪吃蛇 和 自动寻路贪吃蛇(两种)

昨天晚上闲的无聊,随手看到了一篇写贪吃蛇的博客,于是试着写了一发
写完后感觉不错,自己 (手残党) 玩了几盘还可以 于是叫舍友一起玩,舍友居然嫌弃,
于是乎继续无聊的写了一个自动寻路的函数加上去,跑出来的最大也就是 140 左右
原因很简单 局部解 不一定 是最优解 更何况是多个局部解累加起来的
和队友商量最优解,发现只要尽可能的绕最大的圈,到最后就是最优解

手动贪吃蛇
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
#define maxn 5000000
struct node{
  short x,y;
}a[maxn];
int b[200][200];
#define X 55
#define Y 29
int cnt,ans,L;
void build(){
  short x,y;
    for(x=0;x<X;x++){
      y=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(x=0;x<X;x++){
      y=Y;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=X;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
}
void init(){
    memset(b,0,sizeof b);
    cnt=1;
    ans=0;
    L=2;
    build();
}
void run(){
  short x=X/2,y=Y/2,dx=0,dy=0,cx=0,cy=0,sx=0,sy=0;
  char c;
  kbhit();
  c = getch();
  sx=(short)rand()%(X-3)+1;
  sy=(short)rand()%(Y-3)+1;
  while(b[sx][sy]!=0){
    sx=(short)rand()%(X-3)+1;
    sy=(short)rand()%(Y-3)+1;
  }
  COORD spos={sx,sy};
  b[sx][sy]=-2;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),spos);
  printf("$");
  while(1){
    switch(c){
      case 'w': dx = 0, dy = -1; break;
      case 's': dx = 0, dy = 1; break;
      case 'a': dx = -1, dy = 0; break;
      case 'd': dx = 1, dy = 0; break;
    }
    COORD pos={x=x+dx,y=y+dy};
    if(x==sx&&y==sy){
      b[(int)sx][(int)sy]=0;
      ans++;
      L++;
      sx=(short)rand()%(X-3)+1;
      sy=(short)rand()%(Y-3)+1;
      while(b[sx][sy]!=0){
        sx=(short)rand()%(X-3)+1;
        sy=(short)rand()%(Y-3)+1;
      }
      COORD spos={sx,sy};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),spos);
      printf("$");
    }
    if(b[x][y]==-1){
      //system("cls");
      COORD pos={X/2,Y/2};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x01|0x05);
      cout<<"Score is "<<ans<<endl;
      return ;
    }
    b[x][y]=-1;
    a[++cnt].x=x;
    a[cnt].y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
    printf("*");
    cx=a[cnt-L-1].x;
    cy=a[cnt-L-1].y;
    COORD cpos={cx,cy};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cpos);
    printf(" ");
    b[cx][cy]=0;
    Sleep(100);
    for(;kbhit();) c = getch();
  }
}
int main(){
  srand(time(NULL));
  init();
  run();
  return 0;
}

自动寻路 贪吃蛇 (第一种)

(非最优解)

#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
#define INF 0x3f3f
#define maxn 5000000
struct node{
  short x,y;
}a[maxn],u,v;
stack <node> st;
queue <node> q;
int b[200][200];
int mp[200][200];
#define X 55
#define Y 29
int cnt,ans,L;
short bx,by,ex,ey;
void build(){
  short x,y;
    for(x=0;x<X;x++){
      y=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(x=0;x<X;x++){
      y=Y;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=X;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
}
void init(){
    memset(b,0,sizeof b);
    cnt=1;
    ans=0;
    L=2;
    build();
}
void init_mp(){
  short x,y;
  for(int i=0;i<X;i++){
    for(int j=0;j<Y;j++){
      if(b[i][j]==-1) mp[i][j]=INF;
      else mp[i][j]=0;
    }
  }
}
void bfs(){
  q.push((node){bx,by});
  while(!q.empty()){
    u=q.front(),v;
    q.pop();
    //cout<<u.x<<"  "<<u.y<<endl;
    if(u.x-1>0&&mp[u.x-1][u.y]==0){
      mp[u.x-1][u.y]=mp[u.x][u.y]+1;
      v.x=u.x-1;
      v.y=u.y;
      q.push(v);
    }
    if(u.x+1<X&&mp[u.x+1][u.y]==0){
      mp[u.x+1][u.y]=mp[u.x][u.y]+1;
      v.x=u.x+1;
      v.y=u.y;
      q.push(v);
    }
    if(u.y-1>0&&mp[u.x][u.y-1]==0){
      mp[u.x][u.y-1]=mp[u.x][u.y]+1;
      v.x=u.x;
      v.y=u.y-1;
      q.push(v);
    }
    if(u.y+1<Y&&mp[u.x][u.y+1]==0){
      mp[u.x][u.y+1]=mp[u.x][u.y]+1;
      v.x=u.x;
      v.y=u.y+1;
      q.push(v);
    }
  }
}
bool dfs(int x,int y){
  v.x=x;
  v.y=y;
    st.push(v);
  if(x==bx&&y==by) return true;
  if(x-1>0&&mp[x-1][y]+1==mp[x][y]){
    if(dfs(x-1,y)){
      return true;
    }
  }
  if(x+1<X&&mp[x+1][y]+1==mp[x][y]){
    v.x=x+1;
    v.y=y;
    if(dfs(x+1,y)){
      return true;
    }
  }
  if(y-1>0&&mp[x][y-1]+1==mp[x][y]){
    v.x=x;
    v.y=y-1;
    if(dfs(x,y-1)){
      return true;
    }
  }
  if(y+1<X&&mp[x][y+1]+1==mp[x][y]){
    v.x=x;
    v.y=y+1;
    if(dfs(x,y+1)){
      return true;
    }
  }
}
bool solve(){
  init_mp();
  mp[bx][by]=1;
  bfs();
  if(mp[ex][ey]==0) return false;
  dfs(ex,ey);
  return true;
}
void run(){
  short x=X/2,y=Y/2,dx=0,dy=0,cx=0,cy=0,sx=0,sy=0;
  while(1){
    if(cnt>4500000) cnt=0;
    bx=x;
    by=y;
    sx=(short)rand()%(X-3)+1;
    sy=(short)rand()%(Y-3)+1;
    while(b[sx][sy]!=0){
      sx=(short)rand()%(X-3)+1;
      sy=(short)rand()%(Y-3)+1;
    }
    COORD spos={sx,sy};
    b[sx][sy]=-2;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),spos);
    printf("$");
    ex=sx;
    ey=sy;
    ans++;
    if(!solve()){
      COORD pos={X+2,Y/2};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x01|0x05);
      cout<<"Score is "<<ans<<endl;
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
      return ;
    }
    while(!st.empty()){
      Sleep(20);
      u=st.top();
      st.pop();
      COORD pos={u.x, u.y};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("*");
      a[++cnt]=u;
      b[u.x][u.y]=-1;
      cx=a[cnt-L-1].x;
      cy=a[cnt-L-1].y;
      b[cx][cy]=0;
      COORD cpos={cx,cy};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cpos);
      printf(" ");
    }
    L++;
    x=ex;
    y=ey;
  }
}
int main(){
  int i=10;
  while(i--){
    srand(time(NULL));
    init();
    solve();
    run();
    Sleep(3000);
    system("cls");
  }
  return 0;
}

自动寻路 贪吃蛇 (第二种)
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
#define INF 0x3f3f
#define maxn 5000000
struct node{
  short x,y;
}a[maxn],u,v;
stack <node> st;
queue <node> q;
int b[200][200];
int mp[200][200];
#define X 55
#define Y 29
int cnt,ans,L;
short bx,by,ex,ey;
void build(){
  short x,y;
    for(x=0;x<X;x++){
      y=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(x=0;x<X;x++){
      y=Y;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=0;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
    for(y=0;y<Y;y++){
      x=X;
      COORD pos={x,y};
      b[x][y]=-1;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("#");
    }
}
void init(){
    memset(b,0,sizeof b);
    cnt=1;
    ans=0;
    L=2;
    build();
}
void init_mp(){
  short x,y;
  for(int i=0;i<X;i++){
    for(int j=0;j<Y;j++){
      if(b[i][j]==-1) mp[i][j]=INF;
      else mp[i][j]=0;
    }
  }
  while(!st.empty()){
    st.pop();
  }
}
void bfs(){
  q.push((node){bx,by});
  while(!q.empty()){
    u=q.front(),v;
    q.pop();
    //cout<<u.x<<"  "<<u.y<<endl;
    if(u.x-1>0&&mp[u.x-1][u.y]==0){
      mp[u.x-1][u.y]=mp[u.x][u.y]+1;
      v.x=u.x-1;
      v.y=u.y;
      q.push(v);
    }
    if(u.x+1<X&&mp[u.x+1][u.y]==0){
      mp[u.x+1][u.y]=mp[u.x][u.y]+1;
      v.x=u.x+1;
      v.y=u.y;
      q.push(v);
    }
    if(u.y-1>0&&mp[u.x][u.y-1]==0){
      mp[u.x][u.y-1]=mp[u.x][u.y]+1;
      v.x=u.x;
      v.y=u.y-1;
      q.push(v);
    }
    if(u.y+1<Y&&mp[u.x][u.y+1]==0){
      mp[u.x][u.y+1]=mp[u.x][u.y]+1;
      v.x=u.x;
      v.y=u.y+1;
      q.push(v);
    }
  }
}
bool dfs(int x,int y){
  v.x=x;
  v.y=y;
    st.push(v);
  if(x==bx&&y==by) return true;
  if(x-1>0&&mp[x-1][y]+1==mp[x][y]){
    if(dfs(x-1,y)){
      return true;
    }
  }
  if(x+1<X&&mp[x+1][y]+1==mp[x][y]){
    v.x=x+1;
    v.y=y;
    if(dfs(x+1,y)){
      return true;
    }
  }
  if(y-1>0&&mp[x][y-1]+1==mp[x][y]){
    v.x=x;
    v.y=y-1;
    if(dfs(x,y-1)){
      return true;
    }
  }
  if(y+1<X&&mp[x][y+1]+1==mp[x][y]){
    v.x=x;
    v.y=y+1;
    if(dfs(x,y+1)){
      return true;
    }
  }
}
int solve(){
  init_mp();
  mp[bx][by]=1;
  if(bx==ex&&by==ey){
    st.push((node){bx,by});
    return 2;
  }
  bfs();
  if(mp[ex][ey]==0) return 0;
  dfs(ex,ey);
  st.pop();
  return 1;
}
void run(){
  short x=X/2,y=Y/2,dx=0,dy=0,cx=0,cy=0,sx=0,sy=0;
  sx=(short)rand()%(X-3)+1;
  sy=(short)rand()%(Y-3)+1;
  while(b[sx][sy]!=0){
    sx=(short)rand()%(X-3)+1;
    sy=(short)rand()%(Y-3)+1;
  }
  COORD spos={sx,sy};
  b[sx][sy]=-2;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),spos);
  printf("$");
  ex=sx;
  ey=sy;
  while(1){
    bx=x;
    by=y;
    int bb=solve();
    if(bb==0){
        COORD pos={X+2,Y/2};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x01|0x05);
        cout<<"Score is "<<ans<<endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
        return ;
    }else if(bb==1){
      Sleep(20);
      u=st.top();
      COORD pos={x=u.x, y=u.y};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
      printf("*");
      a[++cnt]=u;
      b[u.x][u.y]=-1;
      cx=a[cnt-L-1].x;
      cy=a[cnt-L-1].y;
      b[cx][cy]=0;
      COORD cpos={cx,cy};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cpos);
      printf(" ");
    }else if(bb==2){
      L++;
      ans++;
      sx=(short)rand()%(X-3)+1;
      sy=(short)rand()%(Y-3)+1;
      while(b[sx][sy]!=0){
        sx=(short)rand()%(X-3)+1;
        sy=(short)rand()%(Y-3)+1;
      }
      COORD spos={sx,sy};
      b[sx][sy]=-2;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),spos);
      printf("$");
      ex=sx;
      ey=sy;
    }
  }
}
int main(){
  int i=10;
  while(i--){
    srand(time(NULL));
    init();
    solve();
    run();
    Sleep(3000);
    system("cls");
  }
  return 0;
}

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值