Luogu

codeforces

前言

模拟赛原题。。

好好一道送分被我硬打成爆蛋。。

\(\sf{Solution}\)

看了一波数据范围,感觉能 dfs 骗分。

骗成正解了。

大概就是将这个 \(n\times n\) 的矩阵全扫一遍,可以选择填 \(1\) 或不填。回溯一下就行啦。

  • 位置问题

假设现在的坐标为 \((x,y)\)

若 \(x>n\) ,则结束 dfs ,比较一下矩阵字典序,较为简单不题。

若 \(y>n\) ,则 \(x→x+1,y=1\) ,即跳到下一行第一位。

  • 对称处理 (定义 \(a_{x,y}\) 为当前搜到的地方)

    • 在对角线上

    只填这个地方。

    • 其他

    填 \(a_{x,y}\) 和 \(a_{y,x}\) 。

填 \(1\) 的话就要相应处理 \(k\) 。

  • 特判

\(k=0\) ,输出全 \(0\) 矩阵。

\(\sf{Code}\)

写得有点麻烦。。

#include<iostream>
#include<cstdio>
using namespace std;
int n,k;
bool flag,m[105][105],q[105][105];
inline bool p()
{
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(m[i][j]<q[i][j])
return flag=true;
return false;
}
inline void dfs(int x,int y,int d)
{
if(y>n)
++x,y=1;
if(d==0)
{
if(p())
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
m[i][j]=q[i][j];
return ;
}
if(x>n)
return ;
if(x==y&&d>=1&&!q[x][y])
q[x][y]=1,dfs(x,y+1,d-1),q[x][y]=0;
else if(x!=y&&d>=2&&(!q[x][y]||!q[y][x]))
{
if(q[x][y]==0&&q[y][x]==0)
q[x][y]=1,q[y][x]=1,dfs(x,y+1,d-2),q[x][y]=0,q[y][x]=0;
else if(q[x][y]==1&&q[y][x]==0)
q[y][x]=1,dfs(x,y+1,d-1),q[y][x]=0;
else if(q[x][y]==0&&q[y][x]==1)
q[x][y]=1,dfs(x,y+1,d-1),q[x][y]=0;
}
else dfs(x,y+1,d);
return ;
}
signed main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
if(k==0)
{
for(int i=1;i<=n;++i,cout<<"\n")
for(int j=1;j<=n;++j)
cout<<"0 ";
return 0;
}
if(k>n*n)
{
cout<<-1;
return 0;
}
q[1][1]=1;
dfs(1,2,k-1);
if(flag)
for(int i=1;i<=n;++i,cout<<"\n")
for(int j=1;j<=n;++j)
cout<<m[i][j]<<" ";
else cout<<"-1";
return 0;
}

题解 CF803A Maximal Binary Matrix的更多相关文章

  1. Educational Codeforces Round 20 A. Maximal Binary Matrix

    A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. Maximal Binary Matrix CodeForces - 803A (贪心+实现)

    题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...

  3. CodeForces 803A Maximal Binary Matrix

    枚举. 枚举对角线上放多少个$1$,剩余的贪心放,更新答案. #include <iostream> #include <cstdio> #include <cstrin ...

  4. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  5. 【题解】Sonya and Matrix Beauty [Codeforces1080E]

    [题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...

  6. 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

    题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...

  7. 【leetcode】1253. Reconstruct a 2-Row Binary Matrix

    题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...

  8. LeetCode 题解 Search a 2D Matrix II。巧妙!

    [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...

  9. [LeetCode 题解]: Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. Http 前端向后端传递List参数

    场景 在日常项目开发中,前端向后端传参时,可能会遇到需要传 List 类型的参数.比如批量删除时将多个 ID 以集合的形式传给后台. 前端传参 此时前端传参有两种方式: 1.多个同名 key key ...

  2. 项目一共30个模块,你叫我maven版本一个个手动改?

    大家好呀,我是铂赛东,一个乱入公众号博主的开源作者.今天分享一个maven小技巧,希望帮助到大家. 之前有个群友私聊问我,如何快速统一去更改项目中所有的maven版本号,他说之前都是手动一个个去修改, ...

  3. java中使用 POI导出excel表格的简单实现

    大概流程分7步: 1.创建工作簿 --> 2.创建sheet表 --> 3.创建row行(建议使用循环) --> 4.用row行逐一创建单元格(建议使用循环) --> 5.单元 ...

  4. HDU4372 Count the Buildings (+题解:斯特林数)

    题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...

  5. node前后端交互(Express)

    1. Express框架是什么 1.1 Express是一个基于Node平台的web应用开发框架,它提供了一系列的强大特性,帮助你创建各种Web应用.我们可以使用 npm install expres ...

  6. 第三十六篇:vue3响应式(关于Proxy代理对象,Reflect反射对象)

    好家伙,这个有点难. 1.代理对象Proxy Proxy 对象用于创建一个对象的代理,从而实现基本操作的拦截和自定义(如属性查找.赋值.枚举.函数调用等). 拦截对象中任意属性的变化,包括:查get, ...

  7. python 基于aiohttp的异步爬虫实战

    钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. 之前我们使用requests库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...

  8. [深度学习]-Dataset数据集加载

    加载数据集dataloader from torch.utils.data import DataLoader form 自己写的dataset import Dataset train_set = ...

  9. Windows Powershell安装错误

    今天需要更新一下VMware的 powercli.使用命令install-module -Name VMware.PowerCLI -AllowClobber但是遇到一个错误. Unable to r ...

  10. AVL tree 高度上下界推导

    1. 最大高度对应 Node 数量 \(N_{h}\) 的递归公式 设有一棵 AVL tree 的高度为 \(h\), 对于该树, 构成该树的最少 node 数量为 \(N_{h}\) . 有: 最坏 ...