ACM2017icpc青岛站(计蒜课重现赛)-The Squared Mosquito Coil(dfs)

此处传送门

The Squared Mosquito Coil

Lusrica designs a mosquito coil in a board with n × n grids. The mosquito coil is a series of consecutive grids, each two neighboring grids of which share a common border. If two grids in the mosquito coil are not consecutive, they do not share any border, but they can share a common endpoint.
The mosquito coil Lusrica designed starts from the upper left corner of the board. It goes right to the last available grid. Alter the direction and go downward to the last available grid, and alter the direction again going left to the last available grid. To carry on after altering the direction and go upward to the last available grid. Then it goes right again and repeats the above turns.
It ends up in a grid such that the above process cannot be continued any more. Your mission now is to print the whole blueprint of Lusrica’s mosquito coil.
Input
This problem has several test cases and the first line contains an integer t (1 ≤ t ≤ 36) which is the number of test cases. For each case a line contains an integer n (1 ≤ n ≤ 36) indicating the size of the board.
Output
For each case with input n, output n lines to describe the whole board. Each line contains n characters. If a grid is a part of Lusrica’s mosquito coil, the corresponding character is ‘#’, or ’ ’ (a single blank) if not.
这里写图片描述

我这种弱鸡去不了比赛,只能过后补补题了QAQ…学校弱人也弱什么都弱简直蓝廋,写的代码可能很麻烦,但应该比较容易理解吧

题意:

给一个n,让你输出一个外圈长度为n的蚊香,蚊香都知道是啥样把!而且蚊香每一圈互相不能粘在一起。

想法:

模拟题,我用dfs写的,用dir数组存一下蚊香走的方向(右下左上),走的时候判定一下下一次该走的那个位置能不能走。然后第一次肯定是向右走,当走到n长度时,换下一个方向看能不能走,不能走就结束,能走的话即下次该向下走,依次类推向左向上,这时候如果向上继续走长度为n的话肯定不行是把,到了下标为visit[2][0]的时候本来该走visit[1][0],但是由于visit[0][0]走过了而且与visit[1][0]连在一起的,所以visit[1][0]不能走,那么就换下一个方向,如果下一个方向也走不了,就结束了。

#include <iostream>
#include <cstring>

using namespace std;

int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool visit[50][50];
int l = 0;
int num = 1;
int n;

void dfs(int x, int y)
{
    visit[x][y] = true;//用这个标记表示在这个位置有蚊香
    if(num < n)
    {
        int nx = x+dir[l][0];
        int ny = y+dir[l][1];//依照这个方向继续走到下一个位置
        num++;
        if(num == n)
        {
            num = 1;
            l = (l+1)%4;
        }
        int flag = 0;
        int i;
        for(i = 0; i < 4; i++)//看看这个位置四周除了刚才过来的位置以外其他位置有没有和外圈的蚊香粘在一起
        {
            int nnx = nx + dir[i][0];
            int nny = ny + dir[i][1];
            if(nnx == x && nny == y)
            {
                continue;
            }
            if(nnx>=0&&nny>=0&&visit[nnx][nny])
            {
                flag = 1;
            }
        }
        if(!flag)
        {
            dfs(nx, ny);//这个位置能走
        }
        else//粘在一起的话就换下一个方向继续走
        {
            num = 1;
            l = (l+1)%4;
            nx = x+dir[l][0];
            ny = y+dir[l][1];
            flag = 0;
            if(!visit[nx][ny])
            {
                for(i = 0; i < 4; i++)
                {
                    int nnx = nx + dir[i][0];
                    int nny = ny + dir[i][1];
                    if(nnx == x && nny == y)
                    {
                        continue;
                    }
                    if(nnx>=0&&nny>=0&&visit[nnx][nny])
                    {
                        flag = 1;
                    }
                }
                if(!flag)
                {
                    dfs(nx, ny);
                }
                else
                {
                    return;
                }
            }
        }
    }
}

int main()
{
    int t;

    cin >> t;
    while(t--)
    {
        l = 0;
        num = 1;
        memset(visit, false, sizeof(visit));
        cin >> n;
        dfs(0, 0);//利用dfs走的结果把能走的位置标记为true
        int i;
        int j;
        for(i = 0; i < n; i++)//利用visit数组输出
        {
            for(j = 0; j < n; j++)
            {
                if(visit[i][j])
                {
                    cout << "#";
                }
                else
                {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaotudeluobo/article/details/78520917