KSGT

来源:互联网 发布:广州易娱网络怎么样 编辑:程序博客网 时间:2024/06/09 17:11

考试月日常瘫痪,想起还有女神的约会,爬到机房,被线段树狠狠摩擦。

改开一道水题,UVa11464,暴力题目
第一眼见到和开关题目十分类似,但是开关题目同一行后面一个不会影响前一个状态,所以这个变化不好确定,就试着暴力写题

逻辑上很简单,第一行枚举,后面几行对应变化就可以
想着太少,以至于后面代码写得很长很长,虽然写完直接就能解决,但是代码还是写了很久,函数不断嵌套

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int maxn = 20;const int INF = 1e9 + 10;const int dx[] = {0, 0, -1};const int dy[] = {1, -1, 0};int grid[maxn][maxn], tem_grid[maxn][maxn];int cnt[maxn][maxn];int n;int ans;int tem_ans;bool check(int x, int y){    if (x < 0 || x >= n || y < 0 || y >= n) return false;    return tem_grid[x][y];}int count_1(int x, int y){    int res = 0;    for (int i = 0; i < 3; i++)    {        int newx = x + dx[i], newy = y + dy[i];        if (check(newx, newy)) res++;    }    return res;}void get_cnt(int cur){    for (int i = 0; i < n; i++)    {        cnt[cur][i] = count_1(cur, i);    }}void update_ans(){    for (int i = 0; i < n; i++)    {        if (cnt[n - 1][i] & 1) return;    }    ans = min(ans, tem_ans);}void next_solve(){    for (int i = 1; i < n; i++)    {        for (int j = 0; j < n; j++)        {            if (cnt[i - 1][j] & 1)            {                if (tem_grid[i][j] == 0)                {                    tem_grid[i][j] = 1;                    tem_ans++;                }            }            else            {                if (tem_grid[i][j]) return;            }        }        get_cnt(i);    }    update_ans();}void solve(){    for (int i = 0; i < (1 << n); i++)    {        tem_ans = 0;        memcpy(tem_grid, grid, sizeof(tem_grid));        memset(cnt, 0, sizeof(cnt));        for (int j = 0; j < n; j++)        {            if (((1 << j) & i) && tem_grid[0][j] == 0)            {                tem_grid[0][j] = 1;                tem_ans++;            }        }        get_cnt(0);        next_solve();    }}int main(){    int t; scanf("%d", &t);    int kase = 0;    while (t--)    {        ans = INF;        scanf("%d", &n);        for (int i = 0; i < n; i++)            for (int j = 0; j < n; j++)                scanf("%d", &grid[i][j]);        solve();        printf("Case %d: ", ++kase);        if (ans == INF) puts("-1");        else printf("%d\n", ans);    }    return 0;}

说一下问题:
1.强行缩短main函数,这个main函数基本废掉了,看不出整体的思路,被solve架空了,所以solve中间又嵌套了next_solve
2.按照暴搜的写法来完成的,dx[] 、 dy[] 、 check 都是很明显的搜索题目特征,可以看出写代码前构思不是很清晰,中间强行拼凑出来的

写完这个代码之后,很不满意,抄写了一遍刘汝佳的代码,刘汝佳的代码确实优美……

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int maxn = 20;const int INF = 1e9 + 10;int n, a[maxn][maxn], b[maxn][maxn];int check(int s){    memset(b, 0, sizeof(b));    for (int c = 0; c < n; c++)    {        if (s & (1 << c)) b[0][c] = 1;        else if (a[0][c] == 1) return INF;    }    for (int r = 1; r < n; r++)    {        for (int c = 0; c < n; c++)        {            int sum = 0;            if (r > 1) sum += b[r - 2][c];            if (c > 0) sum += b[r - 1][c - 1];            if (c < n - 1) sum += b[r - 1][c + 1];            b[r][c] = sum % 2;            if (a[r][c] == 1 && b[r][c] == 0) return INF;        }    }    int cnt = 0;    for (int r = 0; r < n; r++)    {        for (int c = 0; c < n; c++)        {            if (a[r][c] != b[r][c]) cnt++;        }    }    return cnt;}int main(){    int t; scanf("%d", &t);    int kase = 0;    while (t--)    {        scanf("%d", &n);        for (int r = 0; r < n; r++)            for (int c = 0; c < n; c++)                scanf("%d", &a[r][c]);        int ans = INF;        for (int s = 0; s < (1 << n); s++)        {            ans = min(ans, check(s));        }        if (ans == INF) ans = -1;        printf("Case %d: %d\n", ++kase, ans);    }    return 0;}

总体思路上是一样的,但是细节上的处理非常漂亮
1.在最开始的第一行枚举之时,就进行了剪枝,去掉了那些不可能的情况,减少了重复
2.在只判断奇偶的题目中,数目是不重要的,只用0/1就可以完成任务
3.a[0]为1的地方,b[0]必然为1,这个点我思考了很久,后来枚举证明了…非常巧妙
4.中途剪枝,只要意图把1变成0,就直接退出,这样最后就只剩下了0-0,0-1,1-1的情况,只要a、b不一样就是ans++,简单而快捷


解决完11464,再次成刘汝佳迷妹,再开下一题,LA3401
题目长到爆表…输入也是各种吓人,但是看图能够直接猜出意思,然而到了饭点,精神有点涣散,慢慢悠悠、开开心心读完题目,发现只会暴力解,其它算法?不认识的
瞧了瞧范围,很小很小,只有4,暴力是可做的……但是,状态也太多了…手推吗,懒着动,想着抄抄状态,写后面就好了
这才发现,状态也是可以写个程序打表出来的…脑子太僵硬了,程序本身就是一个辅助的东西嘛
懒癌突然就犯了,不想自己推,瞧瞧刘汝佳的证明就算,突然感觉这个证明不通顺,有些不对,细细证明了好半天,怎么也不对,还是决定自己推算,最终结果和给出的公式是一样的,就假装是书本的错误好了
这道题目就此为止,后面就是枚举的问题了


感概:
前段时间抛开书本,去做了几个kuangbin的专题,突然就想着回来写写一开始就买回来的蓝书,刘汝佳果然时刻能给人惊喜,选题上,代码上都是如此
这本真的值得好好一学!!!


To Do List

女神的约会得写啦,麻烦点就麻烦点,大体思路都出来了,努力憋一憋!

数据结构的代码量真的可怕,还是DP可爱啦,然而DP的思路真是千奇百怪

原创粉丝点击