美文网首页
P1911 L国的战斗之排兵布阵

P1911 L国的战斗之排兵布阵

作者: 张的笔记本 | 来源:发表于2020-02-26 09:26 被阅读0次

1、思路

①当n为1时,很简单直接输出;

②当n为2时,无非下边四种情况
其他情况可以通过旋转得到。而上图黑色框与n=1时的情况等价。
③当n为3时,

2、代码

#include<iostream>
using namespace std;
static int c = -1;
void solve(int **a, int temp1, int special, int x1, int y1, int x, int y){//注:输入指挥营位置下标从0开始
    if(temp1 == 2){
        a[x1][y1] = c;
        a[x1+1][y1] = c;
        a[x1][y1+1] = c;
        a[x1+1][y1+1] = c;
        a[x][y] = special;
        --c;
        return;
    }else if(temp1 == 4){
        if(x < x1 + 2){
            if(y < y1 + 2){
                solve(a, 2, special, x1, y1, x, y);
                a[x1][y1 + 2] = a[x1][y1 + 3] = a[x1 + 1][y1 + 3] = c;
                a[x1 + 1][y1 + 2] = a[x1 + 2][y1 + 1] = a[x1 + 2][y1 + 2] = c - 1;
                a[x1 + 2][y1] = a[x1 + 3][y1] = a[x1 + 3][y1 + 1] = c - 2;
                a[x1 + 3][y1 + 2] = a[x1 + 2][y1 + 3] = a[x1 + 3][y1 + 3] = c - 3;
            }else{
                solve(a, 2, special, x1, y1 + 2, x, y);
                a[x1][y1] = a[x1][y1 + 1] = a[x1 + 1][y1] = c;
                a[x1 + 1][y1 + 1] = a[x1 + 2][y1 + 1] = a[x1 + 2][y1 + 2] = c - 1;
                a[x1 + 2][y1] = a[x1 + 3][y1] = a[x1 + 3][y1 + 1] = c - 2;
                a[x1 + 3][y1 + 2] = a[x1 + 2][y1 + 3] = a[x1 + 3][y1 + 3] = c - 3;
            }
        }else{
            if(y < y1 + 2){
                solve(a, 2, special, x1 + 2, y1, x, y);
                a[x1][y1] = a[x1][y1 + 1] = a[x1 + 1][y1] = c;
                a[x1][y1 + 2] = a[x1][y1 + 3] = a[x1 + 1][y1 + 3] = c - 1;
                a[x1 + 1][y1 + 1] = a[x1 + 1][y1 + 2] = a[x1 + 2][y1 + 2] = c - 2;
                a[x1 + 3][y1 + 2] = a[x1 + 2][y1 + 3] = a[x1 + 3][y1 + 3] = c - 3;
            }else{
                solve(a, 2, special, x1 + 2, y1 + 2, x, y);
                a[x1][y1] = a[x1][y1 + 1] = a[x1 + 1][y1] = c;
                a[x1][y1 + 2] = a[x1][y1 + 3] = a[x1 + 1][y1 + 3] = c - 1;
                a[x1 + 1][y1 + 1] = a[x1 + 1][y1 + 2] = a[x1 + 2][y1 + 1] = c - 2;
                a[x1 + 2][y1] = a[x1 + 3][y1] = a[x1 + 3][y1 + 1] = c - 3;
            }
        }
        c -= 4;
        return;
    }else{
        int temp2 = temp1 / 2, temp3 = c;
        --c;
        if(x < x1 + temp2){
            if(y < y1 + temp2){
                solve(a, temp2, temp3, x1 + temp2, y1 + temp2, x1 + temp2, y1 + temp2);
                solve(a, temp2, temp3, x1, y1 + temp2, x1 + temp2 - 1, y1 + temp2);
                solve(a, temp2, temp3, x1 + temp2, y1, x1 + temp2, y1 + temp2 - 1);
                solve(a, temp2, special, x1, y1, x, y);
            }else{
                solve(a, temp2, temp3, x1, y1, x1 + temp2 - 1, y1 + temp2 - 1);
                solve(a, temp2, temp3, x1 + temp2, y1, x1 + temp2, y1 + temp2 - 1);
                solve(a, temp2, temp3, x1 + temp2, y1 + temp2, x1 + temp2, y1 + temp2);
                solve(a, temp2, special, x1, y1 + temp2, x, y);
            }
        }else{
            if(y < y1 + temp2){
                solve(a, temp2, temp3, x1, y1, x1 + temp2 - 1, y1 + temp2 - 1);
                solve(a, temp2, temp3, x1, y1 + temp2, x1 + temp2 - 1, y1 + temp2);
                solve(a, temp2, temp3, x1 + temp2, y1 + temp2, x1 + temp2, y1 + temp2);
                solve(a, temp2, special, x1 + temp2, y1, x, y);
            }else{
                solve(a, temp2, temp3, x1, y1, x1 + temp2 - 1, y1 + temp2 - 1);
                solve(a, temp2, temp3, x1, y1 + temp2, x1 + temp2 - 1, y1 + temp2);
                solve(a, temp2, temp3, x1 + temp2, y1, x1 + temp2, y1 + temp2 - 1);
                solve(a, temp2, special, x1 + temp2, y1 + temp2, x, y);
            }
        }
    }
}
int main(){
    int n, x, y, i, j, temp1 = 2, count = 1;
    cin>>n>>x>>y;

    while(--n > 0) temp1 = temp1 << 1;
    
    int **a = new int *[temp1];
    int **mask = new int *[temp1];
    for(i = 0; i < temp1; ++i){
        a[i] = new int [temp1];
        mask[i] = new int [temp1]();//全部初始化为0
    }
    mask[x - 1][y - 1] = 1;
    
    solve(a, temp1, 0, 0, 0, x - 1, y - 1);

    for(i = 0; i < temp1; ++i){//重新排序并输出
        for(j = 0; j < temp1; ++j){
            if((i == x - 1 && j == y - 1) || mask[i][j] != 0){
                cout<<a[i][j]<<" ";
                continue;
            }
            if(i + 1 < temp1 && mask[i + 1][j] == 0 && a[i + 1][j] == a[i][j]){
                if(j - 1 >= 0 && mask[i + 1][j - 1] == 0 && a[i + 1][j - 1] == a[i][j]){
                    a[i + 1][j] = a[i][j] = a[i + 1][j - 1] = count;
                    mask[i + 1][j] = mask[i][j] = mask[i + 1][j - 1] = 1;
                }else if(mask[i + 1][j + 1] == 0 && a[i + 1][j + 1] == a[i][j]){
                    a[i + 1][j] = a[i][j] = a[i + 1][j + 1] = count;
                    mask[i + 1][j] = mask[i][j] = mask[i + 1][j + 1] = 1;
                }else{
                    a[i + 1][j] = a[i][j] = a[i][j + 1] = count;
                    mask[i + 1][j] = mask[i][j] = mask[i][j + 1] = 1;
                }
            }else{
                a[i][j + 1] = a[i][j] = a[i + 1][j + 1] = count;
                mask[i][j + 1] = mask[i][j] = mask[i + 1][j + 1] = 1;
            }
            ++count;
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    for(int i = 0; i < temp1; ++i)//释放内存空间
    {
        delete [] a[i]; 
        delete [] mask[i]; 
    }
    return 0;
}

3、可改进

我这个解法相当于把n=2当作递归的边界,其实n=2也可以划分为四个n=1的情况,所以可以进一步将n=1作为递归的边界;可以不用静态变量;

相关文章

  • P1911 L国的战斗之排兵布阵

    1、思路 ①当n为1时,很简单直接输出; 2、代码 3、可改进 我这个解法相当于把n=2当作递归的边界,其实n=2...

  • 跟打篮球一样去战斗

    1、跟打篮球一样去战斗! 2、打球也需要排兵布阵,善总结!

  • 排兵布阵

    排兵布阵曾经是我的强项,曾经,在那个一年一度的大型纪念性庆祝活动上,主场的那一块,排兵布阵多少年都成为了我的专利,...

  • 排兵布阵

    一日三变。莫名的为领导们的焦虑同情。 本来我同意做班主任了。并且把原来我建的几个语文学习群都解散...

  • 表白

    字排兵布阵 组成句 便有了温度

  • 论排兵布阵

    相信大家都会下象棋吧。在遵守马走日、象走田的规矩下更重要的是其他车马炮的布局。这几天我们在推百日战役,随着推进工作...

  • 如何排兵布阵

    如何排兵布阵,让老人和新人都能发挥各自所长? 把信得过的老人放在离得远的地方,新人放在离得近的地方。 因为企业文化...

  • 52玩手游:超凡召唤师-战斗技巧

    玩过《超凡召唤师》的朋友都知道战斗的目标就是推倒对方城墙,还要同时守住自己的城墙。在这攻守之间,合理的排兵布阵就是...

  • 能自救者方能获救

    生活与我而言就是一场战斗!需要谋篇布局,需要排兵布阵……这是我自己给自己设定的一个彩蛋!可是我好像已经战败了!此刻...

  • 【攻城掠地SF】公益服排兵布阵新手攻略,武将的正确组合方法

    【攻城掠地SF】公益服排兵布阵新手攻略,武将的正确组合方法 【攻城掠地SF】公益版本:游戏入口 如何排兵...

网友评论

      本文标题:P1911 L国的战斗之排兵布阵

      本文链接:https://www.haomeiwen.com/subject/sqipchtx.html