其他分享
首页 > 其他分享> > 残缺棋盘游戏

残缺棋盘游戏

作者:互联网

分支算法

题意描述:

在2^k * 2^k的正方形棋盘上有一个缺口,这里可以认为是已经被填充了,要求给出使用三角板去填满这个棋盘的方案

#include <iostream>
 
using namespace std;
 
int amount,Board[100][100];
 
void Cover(int tr,int tc,int dr,int dc,int size)
{
    int s,t;
    if(size<2)
        return ;
    amount++;
    t=amount;
    s=size/2;
    if(dr<tr+s&&dc<tc+s)//残缺在左上角
    {
        //覆盖中间位置
        Board[tr+s-1][tc+s]=t;
        Board[tr+s][tc+s-1]=t;
        Board[tr+s][tc+s]=t;
 
        Cover(tr,tc,dr,dc,s);//覆盖左上
        Cover(tr,tc+s,tr+s-1,tc+s,s);//覆盖右上
        Cover(tr+s,tc,tr+s,tc+s-1,s);//覆盖左下
        Cover(tr+s,tc+s,tr+s,tc+s,s);//覆盖右下
    }
    else if(dr<tr+s&&dc>=tc+s)//残缺在右上角
    {
        Board[tr+s-1][tc+s-1]=t;
        Board[tr+s][tc+s-1]=t;
        Board[tr+s][tc+s]=t;
 
        Cover(tr,tc,tr+s-1,tc+s-1,s);
        Cover(tr,tc+s,dr,dc,s);
        Cover(tr+s,tc,tr+s,tc+s-1,s);
        Cover(tr+s,tc+s,tr+s,tc+s,s);
    }
    else if(dr>=tr+s&&dc<tc+s)//残缺在左下
    {
        Board[tr+s-1][tc+s-1]=t;
        Board[tr+s-1][tc+s]=t;
        Board[tr+s][tc+s]=t;
 
        Cover(tr,tc,tr+s-1,tc+s-1,s);
        Cover(tr,tc+s,tr+s-1,tc+s,s);
        Cover(tr+s,tc,dr,dc,s);
        Cover(tr+s,tc+s,tr+s,tc+s,s);
    }
    else
    {
        Board[tr+s-1][tc+s-1]=t;
        Board[tr+s-1][tc+s]=t;
        Board[tr+s][tc+s-1]=t;
 
        Cover(tr,tc,tr+s-1,tc+s-1,s);
        Cover(tr,tc+s,tr+s-1,tc+s,s);
        Cover(tr+s,tc,tr+s,tc+s-1,s);
        Cover(tr+s,tc+s,dr,dc,s);
    }
}
 
void Print(int s)
{
    for(int i=1;i<=s;i++)
    {
        for(int j=1;j<=s;j++)
            printf("%5d ",Board[i][j]);
        printf("\n");
    }
}
 
int main()
{
    int s=1,k,x,y;
    printf("输入2残缺棋盘的规模:2^k,k=");
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
        s*=2;
    printf("输入棋盘残缺位置(x,y):");
    scanf("%d%d",&x,&y);
    Board[x][y]=0;
    Cover(1,1,x,y,s);
    Print(s);
    return 0;
}

标签:游戏,int,Cover,tr,dc,Board,残缺,棋盘,tc
来源: https://www.cnblogs.com/sherkevin/p/16295148.html