【例7-15 UVA-1603】Square Destroyer

正方形网格问题求解
本文介绍了一种解决正方形网格问题的方法,通过预处理所有可能的正方形及其边长,并使用深度优先搜索来确定最少的操作次数使得所有正方形不再完整。

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


先预处理出所有的正方形(长度为1,2...n的)所包含哪些边。
然后记录每个正方形的应有边长和实际边长(有些边被删掉了);
然后搜的时候,每次找到第一个还是完整的正方形。
枚举删掉它的哪一条边。
然后看看哪些正方形会受到影响。
修改那些受影响的正方形的实际边长。
然后进入下一层。继续搜。
然后回溯影响。
直到所有的正方形都变成不完整的就可以了。
暴力就好。不用加优化。

【代码】

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
    8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 150;

int T,n,cnt[N],tot,fullsize[N],realsize[N],ans,totm;
bool contain[N][N];

int GetColumn(int x,int y){
    int temp1 = (x-1)*(n+1) + x*n + y;
    return temp1;
}

int GetRow(int x,int y){
    int temp1 = (x-1)*(n+1) + (x-1)*n +y;
    return temp1;
}

int findfirst(){
    for (int i = 1;i <= tot;i++)
        if (fullsize[i]==realsize[i]) return i;
    return -1;
}

void dfs(int dep){
    if (dep >= ans) return;
    int idx = findfirst();
    if (idx==-1){
        ans = dep;
        return;
    }

    for (int i = 1;i <= totm;i++)
        if (cnt[i] == 1 && contain[idx][i]){
            cnt[i] = 0;
            for (int j = 1;j <= tot;j++) if (contain[j][i]) realsize[j]--;


            dfs(dep+1);

            cnt[i] = 1;
            for (int j = 1;j <= tot;j++) if (contain[j][i]) realsize[j]++;
        }
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> T;
    while (T--){
        tot = 0;
        memset(contain,0,sizeof contain);
        memset(fullsize,0,sizeof fullsize);
        memset(realsize,0,sizeof realsize);

        cin >>n;
        totm = 2*n*(n+1);
        for (int i = 1;i <= totm;i++) cnt[i] = 1;

        int k;cin >> k;
        while(k--){
            int x;cin >>x;cnt[x] = 0;
        }
        for (int len = 1;len <= n;len++){
            for (int i = 1;i <= n-len+1;i++){
                for (int j = 1;j <= n - len+1;j++){
                    //(i,j)
                    tot++;
                    int x;
                    fullsize[tot] = len*4;
                    for (int k = 0;k < len;k++){

                        x = GetColumn(i+k,j);
                        contain[tot][x] = 1;
                        realsize[tot]+=cnt[x];

                        x = GetColumn(i+k,j+len);
                        contain[tot][x] = 1;
                        realsize[tot]+=cnt[x];

                        x = GetRow(i,j+k);
                        contain[tot][x] = 1;
                        realsize[tot]+=cnt[x];

                        x = GetRow(i+len,j+k);
                        contain[tot][x] = 1;
                        realsize[tot]+=cnt[x];
                    }
                }
            }
        }

        ans = 1000;
        dfs(0);
        cout << ans << endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/8066351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值