• 题意:给一个\(n\)X\(m\)的矩阵,矩阵中某个数字\(k\)表示其四周恰好有\(k\)个不为0的数字,你可以使任意位置上的数字变大,如果操作后满足条件,输出新矩阵,否则输出NO.

  • 题解:贪心,既然能使任意位置加大任意数值,那么我们可以将所有位置都给他填满,这样的话,只要是满足条件的情况就都能这样输出,所以我们遍历每个位置,然后判断周围能填多少个,如果某个数大于周围能填的个数,那么就不满足条件.

  • 代码:

    int t;
    int n,m;
    int a[400][400];
    int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>t;
    while(t--){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
    for(int j=1;j<=m;++j){
    cin>>a[i][j];
    }
    }
    bool ok=1;
    int cnt=0;
    for(int i=1;i<=n;++i){
    for(int j=1;j<=m;++j){
    cnt=0;
    for(int k=0;k<4;++k){
    int x=i+dx[k];
    int y=j+dy[k];
    if(x>=1 && x<=n && y>=1 && y<=m) cnt++;
    }
    if(a[i][j]>cnt){
    ok=0;
    break;
    }
    else a[i][j]=cnt;
    }
    if(!ok){
    break;
    }
    }
    if(!ok){
    cout<<"NO"<<endl;
    }
    else{
    cout<<"YES"<<endl;
    for(int i=1;i<=n;++i){
    for(int j=1;j<=m;++j){
    cout<<a[i][j]<<" ";
    }
    cout<<endl;
    }
    }
    } return 0;
    }

Codeforces Global Round 9 B. Neighbor Grid (构造,贪心)的更多相关文章

  1. Codeforces Global Round 9 B. Neighbor Grid

    题目链接:https://codeforces.com/contest/1375/problem/B 题意 给出一个 $n \times m$ 的方阵,每个方格中有一个非负整数,一个好方格定义如下: ...

  2. Codeforces Global Round 9 A. Sign Flipping (构造)

    题意:有一个长度为\(n\)(odd)的序列,可以更改所有的数的正负,要求最少\(\frac{n-1}{2}\)个\(a_{i+1}-a_i\ge0\),并且要求最少\(\frac{n-1}{2}\) ...

  3. Codeforces Global Round 11 B. Chess Cheater(贪心)

    题目链接:https://codeforces.com/contest/1427/problem/B 题意 给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 ...

  4. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  5. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  6. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  7. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  8. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  9. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

随机推荐

  1. JWT令牌简介及demo

    一.访问令牌的类型 二.JWT令牌 1.什么是JWT令牌 ​ JWT是JSON Web Token的缩写,即JSON Web令牌,是一种自包含令牌. JWT的使用场景: 一种情况是webapi,类似之 ...

  2. 指针锁定 Pointer Lock API 用法

    指针锁定 Pointer Lock API 通过它可以访问原始的鼠标运动(基于指针的相对位移 movementX / movementY),把鼠标事件的目标锁定到一个特定的元素,同时隐藏视图中的指针光 ...

  3. HTTP Keep-Alive模式客户端与服务器如何判定传输完成

    目录 长连接是什么 服务器如何知道已经完全接受客户端发送的数据 客户端如何知道已经完全接受服务端发送的数据 Transfer-Encoding transfer-coding与Content-Leng ...

  4. 【Redis系列】Spring boot实现监听Redis key失效事件

    talk is cheap, show me the code. 一.开启Redis key过期提醒 方式二:修改配置文件 redis.conf # 默认 notify-keyspace-events ...

  5. 07. struts2中对Action的管理方式

    web.xml配置文件的常用代码 <filter> <filter-name>struts2</filter-name> <filter-class>o ...

  6. ModuleNotFoundError 模块寻找路径

    t = os.path.dirname(os.path.dirname((os.path.dirname(os.path.abspath(__file__)))))os.path.sys.path.a ...

  7. 剖析 CopyOnWriteArrayList

    原文链接:https://www.changxuan.top/?p=1252 CopyOnWriteArrayList 是 JUC 中唯一一个支持并发的 List. CopyOnWriteArrayL ...

  8. 使用Docker部署监控系统,Prometheus,Grafana,监控服务器信息及Mysql

    使用Docker部署监控系统,Prometheus,Grafana,监控服务器信息及Mysql 一.docker部署prometheus监控系统 1.1 配置安装环境 1.1.1 安装promethe ...

  9. kubernetes 身份与权限认证 (ServiceAccount && RBAC)

    Kubernetes中提供了良好的多租户认证管理机制,如RBAC.ServiceAccount还有各种Policy等.   ServiceAccount Service Account为Pod中的进程 ...

  10. SpringMVC请求参数的获取方式

    一.GET请求参数获取 1. 通过HttpServletRequest获取参数 2. 直接方法参数获取 3. RequestParam注解方式获取请求参数 4. Bean方式获取参数 5. Model ...