关于想dfs结果写到一半写不下去....
查看原帖
关于想dfs结果写到一半写不下去....
320697
AMIRIOX無暝楼主2021/6/9 20:23

思路大概是三角形向左下右下扩展->只向左下/右下扩展->向左下右下扩展>只向左下/右下扩展->.... 反复交替

但是突然发现不两个下角都扩展的三角形不是隔一层一个的,然后就写不下去了

比如这组数据(n=4)

正确:

               /\
              /__\
             /\  /\
            /__\/__\
           /\      /\
          /__\    /__\
         /\  /\  /\  /\
        /__\/__\/__\/__\
       /\              /\
      /__\            /__\
     /\  /\          /\  /\
    /__\/__\        /__\/__\
   /\      /\      /\      /\
  /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\

错误(我的):

               /\
              /__\
             /\  /\
            /__\/__\
           /\      /\
          /__\    /__\
         /\  /\  /\  /\
        /__\/__\/__\/__\
       /\      /\      /\
      /__\    /__\    /__\
     /\  /\  /\  /\  /\  /\
    /__\/__\/__\/__\/__\/__\
   /\      /\      /\      /\
  /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

char mp[5000][5000];
int flo = 0;
void dfs(int lr, int curf, int x, int y, bool isExt) {
    if (curf > flo)
        return;

    mp[x][y] = '/';
    mp[x + 1][y] = '\\';
    mp[x - 1][y + 1] = '/';
    mp[x][y + 1] = '_';
    mp[x + 1][y + 1] = '_';
    mp[x + 2][y + 1] = '\\';

    if (curf == 2)
        cout << "(" << isExt << ")" << endl;
    if (isExt) {
        dfs(1, curf + 1, x - 2, y + 2, !isExt);
        dfs(2, curf + 1, x + 2, y + 2, !isExt);
    } else {
        if (lr == 1)
            dfs(0, curf + 1, x - 2, y + 2, !isExt);
        else if (lr == 2)
            dfs(0, curf + 1, x + 2, y + 2, !isExt);
    }
}
signed main() {
    for (int i = 1; i < 5000; i++) {
        for (int j = 1; j < 5000; j++) {
            mp[i][j] = '*';
        }
    }
    int n;
    scanf("%d", &n);
    flo = int(pow(2, n)) / 2;
    dfs(0, 1, flo * 4 / 2, 1, true);
    int cnt = flo * 4 / 2 + 1;
    for (int j = 1; j <= flo * 2; j++) {
        for (int i = 1; i <= cnt; i++) {
            if (mp[i][j] == '*')
                printf(" ");
            else
                printf("%c", mp[i][j]);
        }
        printf("\n");
        cnt++;
    }
    return 0;
}
2021/6/9 20:23
加载中...