• 数据结构实习


      1 #include <iostream>
      2 #include <windows.h>
      3 #include <cstdio>
      4 #include <cmath>
      5 #include <time.h>
      6 #include <cstring>
      7 
      8 using namespace std;
      9 
     10 const int N = 1000;
     11 int fa[N], len_path, maze[N][N], book[N][N];
     12 int dx[4] = {1, 0, -1, 0};
     13 int dy[4] = {0, 1, 0, -1};
     14 
     15 void init()
     16 {
     17     for(int i = 0; i < N; i++)
     18         fa[i] = i;
     19 }
     20 
     21 int getfa(int x)
     22 {
     23     if(fa[x] == x)return x;
     24     else return fa[x] = getfa(fa[x]);
     25 }
     26 
     27 void Merge(int a, int b)
     28 {
     29     int af = getfa(a);
     30     int bf = getfa(b);
     31     if(af != bf)
     32         fa[bf] = af;
     33 }
     34 
     35 int tolist(int x, int y, int n)
     36 {
     37     return x*n+y;
     38 }
     39 
     40 void print(int n)
     41 {
     42     for(int i = 0; i < n; i++)
     43     {
     44         for(int j = 0; j < n; j++)
     45             cout<<maze[i][j]<<" ";
     46         cout<<endl;
     47     }
     48 }
     49 
     50 void mazeInit(int n)
     51 {
     52     int sx, sy, ex, ey, x, y;
     53     init();
     54     for(int i = 0; i < n; i++)
     55         for(int j = 0; j < n; j++)
     56             maze[i][j] = 1;
     57     for(int i = 1; i < n; i++)
     58     {
     59         if(i&1)
     60             for(int j = 1; j < n; j+=2)
     61                 maze[i][j] = 0;
     62     }
     63     print(n);
     64     cout<<"*********************"<<endl;
     65     srand(time(NULL));
     66     int d;
     67     int tx1, ty1, tx2, ty2;
     68     sx = sy = 1;
     69     ex = ey = n-3;
     70 
     71     // sx = rand()%(n-2)+1;
     72     // sy = rand()%(n-2)+1;
     73     // do
     74     // {
     75     //     ex = rand()%(n-2)+1;
     76     //     ey = (rand()+2333)%(n-2)+1;
     77     // }while(sx!=ex && sy!=ey);
     78     cout<<"ok"<<endl;
     79     maze[sx][sy] = maze[ex][ey] = 0;
     80     while(getfa(tolist(sx, sy, n)) != getfa(tolist(ex, ey, n)))
     81     {
     82         do
     83         {
     84             x = rand()%(n-2)+1;
     85             y = (rand()+2333)%(n-2)+1;
     86         }
     87         while(maze[x][y] != 1);
     88         d = x%2;
     89         if(!d)
     90         {
     91             tx1 = x+1;
     92             ty1 = y;
     93             tx2 = x-1;
     94             ty2 = y;
     95             if(getfa(tolist(tx1, ty1, n)) != getfa(tolist(tx2, ty2, n)))
     96             {
     97                 maze[x][y] = 0;
     98                 Merge(tolist(tx1, ty1, n), tolist(tx2, ty2, n));
     99             }
    100         }
    101         else
    102         {
    103             tx1 = x;
    104             ty1 = y+1;
    105             tx2 = x;
    106             ty2 = y-1;
    107             if(getfa(tolist(tx1, ty1, n)) != getfa(tolist(tx2, ty2, n)))
    108             {
    109                 maze[x][y] = 0;
    110                 Merge(tolist(tx1, ty1, n), tolist(tx2, ty2, n));
    111             }
    112         }
    113     }
    114     for(int i = 0; i < n; i++)
    115     {
    116         maze[i][n-1] = 1;
    117         maze[n-1][i] = 1;
    118     }
    119     maze[sx][sy] = 2;
    120     maze[ex][ey] = 3;
    121     print(n);
    122 }
    123 
    124 struct point
    125 {
    126     int x, y, pre;
    127 }q[N], path[N];
    128 
    129 void getPath(int pos)
    130 {
    131     while(pos != -1)
    132     {
    133         path[len_path].x = q[pos].x;
    134         path[len_path].y = q[pos].y;
    135         len_path++;
    136         pos = q[pos].pre;
    137     }
    138 }
    139 
    140 void bfs(int n)
    141 {
    142     int front, tail, sx, sy, ex, ey;
    143     for(int i = 0; i < n; i++)
    144         for(int j = 0; j < n; j++)
    145             if(maze[i][j] == 2)
    146             {
    147                 sx = i; sy = j;
    148             }
    149     front = tail = 0;
    150     q[tail].x = sx;
    151     q[tail].y = sy;
    152     q[tail].pre = -1;
    153     tail++;
    154     int x, y, nx, ny;
    155     bool fg = false;
    156     while(front < tail)
    157     {
    158         x = q[front].x;
    159         y = q[front].y;
    160         for(int i = 0; i < 4; i++)
    161         {
    162             nx = x+dx[i];
    163             ny = y+dy[i];
    164             if(nx>=0&&nx<n&&ny>=0&&ny<n&&maze[nx][ny]==0)
    165             {
    166                 maze[nx][ny] = 1;
    167                 q[tail].x = nx;
    168                 q[tail].y = ny;
    169                 q[tail].pre = front;
    170                 tail++;
    171             }
    172             if(maze[nx][ny] == 3){
    173                 q[tail].x = nx;
    174                 q[tail].y = ny;
    175                 q[tail].pre = front;
    176                 tail++;
    177                 fg = true;
    178                 len_path = 0;
    179                 path[len_path].x = nx;
    180                 path[len_path].y = ny;
    181                 len_path++;
    182                 getPath(front);
    183             }
    184         }
    185         if(fg)break;
    186         front++;
    187     }
    188 }
    189 
    190 void printPath()
    191 {
    192     for(int i = len_path-1; i >= 0; i--)
    193         cout<<path[i].x<<" "<<path[i].y<<endl;
    194 }
    195 
    196 int main()
    197 {
    198     int n = 10;
    199     mazeInit(n);
    200     bfs(n);
    201     printPath();
    202     return 0;
    203 }
  • 相关阅读:
    设置表单输入框中的提示信息
    动感图文轮显
    jquery插件之图片轮显(一)整理2011.02.24
    用sort()方法对数组的元素进行排序(可按字母升序降序与获得最大最小值)
    jquery插件之图片轮显(一)
    jquery插件之图片show and hide(一)
    Pdftk The PDF Toolkit
    error while loading shared libraries: libxcbaux.so.0: cannot open shared object file
    害人的RestoreDirectory
    Oracle CASE WHEN 用法介绍
  • 原文地址:https://www.cnblogs.com/Penn000/p/6197768.html
Copyright © 2020-2023  润新知