Advertisement
boook

迷宮 2016 /5 / 26 /12:59

May 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.44 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. //#include"cksh.h"
  3. using namespace std;
  4. #define a first
  5. #define b second
  6. #define mp make_pair
  7. const char U=' ',V='#';
  8. const int H=23,W=49,CURL=2;///5(very) ~ 10(low)
  9. const int base=5,range=20; ///for pace
  10. const int pit_base=10,pit_range=10;
  11. const int BCOL=15,WCOL=4,SCOL=10,TOP=1,BLANK=2;
  12. const int INF=100000000,m[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
  13. typedef pair<int,int> PII;
  14. char p[H][W];
  15. deque<int> way;
  16. class pos
  17. {
  18.     public:
  19.     int x;int y;
  20.     bool equal(pos p)
  21.     {
  22.         return (this->x==p.x&&this->y==p.y)?true:false; ///要玩玩看嗎w why i can't type chinese你要改輸入法我用許是反正你改誠心注音喔喔喔
  23.     }
  24. };///= =好玩嗎成發作玩了 我還要把那個自動解的結果讓他會動 可能還要有個主畫面 就差這些 可以做多人遊戲 你突然開了一個很大的坑 pit 對吧所以要先做什麼 動畫好吧 我物理作業還沒寫= =
  25. ///我有兩題不太會寫空著 哪兩個 ?最後一題-2跟第九題 最後一題我會 第九題的畫 其實我還在找考卷 要圖嗎 我先找找 然後我發現我寶典不見了 悲劇
  26. class MAKE_PIC
  27. {
  28.     public:
  29.  
  30.         bool solve_maze()
  31.         {
  32.             bfs();
  33.             pos now,zero;now.x=H-2,now.y=W-2;zero.x=0;zero.y=0;
  34.             while(!from[now.x][now.y].equal(now)&&!from[now.x][now.y].equal(zero))
  35.             {
  36.                 walk.push_front(now);
  37.                 now=from[now.x][now.y];
  38.             }
  39.             if(walk.empty())return false;
  40.             walk.push_front(now);
  41.             for(int i=1;i<walk.size();i++)
  42.             {
  43.                 for(int j=0;j<4;j++)
  44.                 {
  45.                     now=walk[i-1];
  46.                     now.x+=m[j][0];now.y+=m[j][1];
  47.                     if(now.equal(walk[i]))way.push_back(j);
  48.                 }
  49.             }
  50.             way.push_front(2);
  51.             way.push_back(2);
  52.             for(int i=0;i<way.size();i++)
  53.             {
  54.                 if(way[i]==0)printf("上");
  55.                 if(way[i]==1)printf("左");
  56.                 if(way[i]==2)printf("右");
  57.                 if(way[i]==3)printf("下");
  58.             }
  59.             printf("\n");
  60.             walk.clear();
  61.             //way.clear();
  62.             return true;
  63.         }
  64.         void make_pic()
  65.         {
  66.             pre(),Build();
  67.             while(check())reBuild(),Build();
  68.         }
  69.         void put()
  70.         {
  71.             for(int i=0;i<H;i++)
  72.             {
  73.                 for(int j=0;j<W;j++)
  74.                     printf("%c",p[i][j]);puts("");
  75.             }
  76.         }
  77.         bool N_dig_hole(PII k)
  78.         {
  79.             if(p[k.a-1][k.b-1]!=V && p[k.a-1][k.b]!=V && p[k.a][k.b-1]!=V)return 1;
  80.             if(p[k.a-1][k.b]!=V && p[k.a][k.b+1]!=V && p[k.a-1][k.b+1]!=V)return 1;
  81.             if(p[k.a+1][k.b]!=V && p[k.a][k.b-1]!=V && p[k.a+1][k.b-1]!=V)return 1;
  82.             if(p[k.a+1][k.b]!=V && p[k.a][k.b+1]!=V && p[k.a+1][k.b+1]!=V)return 1;
  83.             return 0;
  84.         }
  85.         void dig_hole()
  86.         {
  87.             int pit=pit_base+rand()%pit_range;
  88.             vector<PII> vd;
  89.             for(int i=1;i<H-1;i++)
  90.                 for(int j=1;j<W-1;j++)
  91.                     if(p[i][j]==V)vd.push_back(mp(i,j));
  92.             while(pit-- && vd.size())
  93.             {
  94.                 int q=rand()%(vd.size());
  95.                 PII now=vd[q];
  96.                 vd.erase(vd.begin()+q,vd.begin()+q+1);
  97.                 if(N_dig_hole(now))pit++;
  98.                 else p[now.a][now.b]=U;
  99.             }
  100.         }
  101.     private:
  102.     int step[H][W];
  103.     pos from[H][W];
  104.     deque<pos> Q,walk;
  105.     void bfs()
  106.     {
  107.         for(int i=0;i<H;i++)
  108.         {
  109.             for(int j=0;j<W;j++)
  110.             {
  111.                 step[i][j]=INF;
  112.                 from[i][j].x=0;
  113.                 from[i][j].y=0;
  114.             }
  115.         }
  116.         pos A,B;A.x=1;A.y=1;
  117.         step[1][1]=0;
  118.         from[1][1].x=1;from[1][1].y=1;
  119.         Q.push_back(A);
  120.         while(!Q.empty())
  121.         {
  122.             A=Q.front();Q.pop_front();
  123.             for(int i=0;i<4;i++)
  124.             {
  125.                 B.x=A.x+m[i][0],B.y=A.y+m[i][1];
  126.                 if(p[B.x][B.y]==V)continue;
  127.                 if(step[A.x][A.y]+1>=step[B.x][B.y])continue;
  128.                 from[B.x][B.y]=A;
  129.                 step[B.x][B.y]=step[A.x][A.y]+1;
  130.                 Q.push_back(B);
  131.             }
  132.         }
  133.     }
  134.     vector<PII> v; /// what need to do
  135.     PII st,en;
  136.     int Z[4]={-1,0,1,0},X[4]={0,-1,0,1};
  137.     void pre()
  138.     {
  139.         st=mp(1,0),en=mp(H-2,W-1);
  140.         for(int i=0;i<H;i++)for(int j=0;j<W;j++)p[i][j]=U;
  141.         for(int i=0;i<W;i++)p[0][i]=p[H-1][i]=V;
  142.         for(int j=0;j<H;j++)p[j][0]=p[j][W-1]=V;
  143.         p[st.a][st.b]=U,p[en.a][en.b]=U;
  144.         p[0][0]=U,p[0][W-1]=U,p[H-1][0]=U,p[H-1][W-1]=U;
  145.         for(int i=0;i<H;i++)
  146.             for(int j=0;j<W;j++)
  147.                 if(i%2==0 && j%2==0 && p[i][j]==V)v.push_back(mp(i,j));
  148.         p[0][0]=V,p[0][W-1]=V,p[H-1][0]=V,p[H-1][W-1]=V;
  149.     }
  150.     bool CAN_GO(PII now,int dir)
  151.     {
  152.         int x=now.a+Z[dir]*2,y=now.b+X[dir]*2;
  153.         if(Z[dir]==0)return (p[x][y]!=V && p[x-1][y]!=V && p[x+1][y]!=V);
  154.         return (p[x][y]!=V && p[x][y-1]!=V && p[x][y+1]!=V);
  155.     }
  156.     bool check()
  157.     {
  158.         for(int i=1;i<H-1;i++)
  159.             for(int j=1;j<W-1;j++)
  160.                 if(p[i][j]==U && p[i-1][j]==U && p[i][j-1]==U && p[i-1][j-1]==U)return 1;
  161.         return 0;
  162.     }
  163.     void reBuild()
  164.     {
  165.         for(int i=1;i<H-1;i++)
  166.             for(int j=1;j<W-1;j++)
  167.                 if(i%2==0 && j%2==0 && p[i][j]==V )v.push_back(mp(i,j));
  168.     }
  169.     void Build()  /// 都是偶數可以轉彎
  170.     {
  171.         srand(time(NULL));
  172.         while(v.size())
  173.         {
  174.             int q=rand()%((int)v.size()),dir,pace;
  175.             PII now=v[q];
  176.             v.erase(v.begin()+q,v.begin()+q+1);
  177.             if(now.a==0)dir=2;
  178.             else if(now.b==0)dir=3;
  179.             else if(now.a==H-1)dir=0;
  180.             else if(now.b==W-1)dir=1;
  181.             else dir=rand()%4;
  182.             pace=rand()%range+base;
  183.             while(pace--)
  184.             {
  185.                 if(CAN_GO(now,dir)==0)
  186.                 {
  187.                     if(now.a%2==1 || now.b%2==1)break;
  188.                     if(CAN_GO(now,(dir+1)%4))dir=(dir+1)%4;
  189.                     else if(CAN_GO(now,(dir+3)%4))dir=(dir+3)%4;
  190.                     else break;
  191.                 }
  192.                 now.a+=Z[dir],now.b+=X[dir],p[now.a][now.b]=V;
  193.                 if(!(now.a%2 || now.b%2))
  194.                 {
  195.                     int x=rand()%CURL;
  196.                     if(x==0)dir=(dir+1)%4;
  197.                     else if(x==1)dir=(dir+3)%4;
  198.                 }
  199.             }
  200.         }
  201.     }
  202. };
  203.  
  204. //void show_maze()
  205. //{
  206. //  for(int i=0;i<H;i++)
  207. //  {
  208. //      for(int j=0;j<W;j++)
  209. //      {
  210. //          if(p[i][j]==U)
  211. //          {
  212. //              SetColor(0,BCOL);
  213. //              printf(" ");
  214. //          }
  215. //          if(p[i][j]==V)
  216. //          {
  217. //              SetColor(0,WCOL);
  218. //              printf(" ");
  219. //          }
  220. //      }
  221. //      printf("\n");
  222. //  }
  223. //  SetColor(15,0);
  224. //  gotoxy(TOP+1,W*2+BLANK);
  225. //    printf("高度: %d 寬度: %d",H,W);
  226. //    gotoxy(TOP+2,W*2+BLANK);
  227. //    printf("使用步數: 0");
  228. //}
  229. //
  230. //void edit_step(int s)
  231. //{
  232. //  SetColor(15,0);
  233. //    gotoxy(TOP+2,W*2+BLANK);
  234. //    printf("使用步數: %d",s);
  235. //}
  236. //
  237. //void print_box(int x, int y,int col)
  238. //{
  239. //  SetColor(0,col);
  240. //  gotoxy(x,y*2);
  241. //  printf(" ");
  242. //}
  243. //
  244. //void user_play()
  245. //{
  246. //  int nx=1,ny=0,s=0;
  247. //  clrscr();
  248. //  show_maze();
  249. //  print_box(nx,ny,SCOL);
  250. //  while(!(nx==(H-2)&&ny==(W-1)))
  251. //  {
  252. //      if(kbhit())
  253. //      {
  254. //          int w=getkey();
  255. //          if(p[nx+m[w][0]][ny+m[w][1]]==U)
  256. //          {
  257. //              print_box(nx,ny,BCOL);
  258. //              nx+=m[w][0],ny+=m[w][1];
  259. //              print_box(nx,ny,SCOL);
  260. //              edit_step(++s);
  261. //          }
  262. //      }
  263. //      SetColor(15,0);
  264. //  }
  265. //  gotoxy(H+1,0);
  266. //  printf("YOU WIN\n");
  267. //}
  268. //
  269. //void pc_play()
  270. //{
  271. ////    for(int i=0;i<way.size();i++)printf("%d",way[i]);
  272. ////    getchar();
  273. //    int nx=1,ny=0,s=0;
  274. //  clrscr();
  275. //  show_maze();
  276. //  print_box(nx,ny,SCOL);
  277. //  while(!(nx==(H-2)&&ny==(W-1)))
  278. //  {
  279. //      Sleep(200);
  280. //        int w=way.front();way.pop_front();
  281. //        print_box(nx,ny,BCOL);
  282. //      nx+=m[w][0],ny+=m[w][1];
  283. //      print_box(nx,ny,SCOL);
  284. //      edit_step(++s);
  285. //      SetColor(15,0);
  286. //  }
  287. //  gotoxy(H+1,0);
  288. //  printf("YOU WIN\n");
  289. //}
  290. int main()
  291. {
  292.     MAKE_PIC maze;
  293.     maze.make_pic();
  294.     maze.put();
  295. //    maze.solve_maze();
  296.     maze.dig_hole();
  297.     maze.put();
  298.     maze.solve_maze();
  299. //    user_play();
  300.     return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement