Codeforces 659F Polycarp and Hay【BFS】
有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发。。。好吧,其实也就这一次。。。
小白说的对,还是代码能力不足。。。
非常不足。。。
题目链接:
http://codeforces.com/contest/659/problem/F
题意:
n*m的格子,每个格子一个数,必须从格子中减去任意一个小于等于这个数的数。
给定数字k,要求:
- 剩下的格子数字和为k。
- 所有非零的格子的数字应该相同。
- 至少一个格子的数字没有改变。
- 含有非零数字的格子应该连通。
分析:
枚举每个能被k整除以及整除后小于n*m的格子的数字,bfs找格子,看联通块是否满足条件。
之前没有加任何优化,TLE on 95。
后来另设一个数组,记录已经枚举过的数,遇到与之前相同的数,说明这个数不满足,可以直接跳过,不用考虑。
代码:
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e3 + 5;
#define x first
#define y second
#define sa(a) scanf("%d", &a)
#define sal(a) scanf("%I64d", &a)
typedef pair<int, int> pii;
int vis[maxn][maxn];
int a[maxn][maxn], c[maxn][maxn];
int m, n;
long long k;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool bfs(pii pa, int res)
{queue<pii>q;memset(vis, false, sizeof(vis));vis[pa.x][pa.y] = true;q.push(pa);int cnt = 1;while(!q.empty()){pii t = q.front();q.pop();int x = t.x, y = t.y;if(cnt == res) return true;for(int i = 0; i < 4; i++){int nx = x + dx[i];int ny = y + dy[i];if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny]){if(a[nx][ny] < a[pa.x][pa.y]) continue;if(a[nx][ny] == a[pa.x][pa.y]) c[nx][ny] = 1;vis[nx][ny] = true;cnt ++;q.push(pii(nx, ny));if(cnt == res) return true;}}}return false;
}
int solve()
{for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(c[i][j]) continue;long long cnt = k/ a[i][j];if(k % a[i][j] == 0 && cnt <= m * n){if(bfs(pii(i, j), cnt)) return a[i][j];}}}return -1;
}
int main (void)
{sa(n),sa(m),sal(k);for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){sa(a[i][j]);}}int ans = solve();if(ans == -1) return printf("NO\n"), 0;printf("YES\n");for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(vis[i][j]) printf("%d%c", ans, j == m - 1? '\n':' ');else printf("0%c", j == m - 1?'\n':' ');}}return 0;
}
转载于:https://www.cnblogs.com/Tuesdayzz/p/5758681.html
Codeforces 659F Polycarp and Hay【BFS】相关推荐
- Codeforces Round #498 (Div. 3)【完结】
2022.3.6 题单地址:https://codeforces.com/contest/1006 目录 A. Adjacent Replacements B. Polycarp's Practice ...
- 【BFS】魔板(c++基础算法)
本专栏上一篇:[BFS]八数码问题(c++基础算法) 目录 一.读题 ①题面 ②题意 三.做题 ①算法原理 ②算法实现 Ⅰ三种基本操作 Ⅱ操作序列 Ⅲ输出 Ⅳ一个特殊情况 四.AC代码 最后 一.读题 ...
- POJ 3414 Pots【BFS】+ Python
原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...
- 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate...
[法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...
- Codeforces Round #490 (Div. 3)【完结】
2022.3.3 题单地址:https://codeforces.com/contest/999 目录 A. Mishka and Contest[模拟] B. Reversing Encryptio ...
- Codeforces Round #486 (Div. 3)【完结】
2022.3.2 题单地址:https://codeforces.com/contest/988 目录 A. Diverse Team[模拟] B. Substrings Sort[暴力枚举] C. ...
- Codeforces Round #481 (Div. 3)【完结】
2022.3.1 题目地址:https://codeforces.com/contest/978 目录 A. Remove Duplicates[模拟] B. File Name[贪心 / 双指针] ...
- Codeforces Round #479 (Div. 3)【完结】
2022.2.28 开始复盘div3 题目链接:https://codeforces.com/contest/977 目录 A. Wrong Subtraction[签到模拟题] B. Two-gra ...
- nyoj 284 坦克大战【bfs】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...
- [kuangbin]专题二 搜索进阶 Escape HDU - 3533【BFS】
[题目描述] The students of the HEU are maneuvering for their military training. The red army and the blu ...
最新文章
- Systemd:再一次的,回归第一进程
- Flutter底部导航栏BottomNavigationBar
- Spring注解开发-属性依赖注入指定名称的bean
- EasyHook远程代码注入
- [IDEA] 异常 Configuration is still incorrect. Do you want to edit it again? Error: module not specifie
- SAP成都研究院小伙伴们开发的一个SAP C4C POC - 通过名片扫描的方式录入联系人数据到系统
- java简易日历程序报告_简单的日历小程序(java编写)
- vant 引进单个样式_vue 公共列表选择组件,引用Vant-UI的样式方式
- leetcode331. Verify Preorder Serialization of a Binary Tree
- network location awareness 错误
- 思科交换机配置试题_思科交换机基本配置命令全集
- ip切换及时刷新交换机的arp表方法
- openwrt 添加usb网卡_如何在双网卡的蜗牛星际上打一个openwrt软路由和NAS一体机
- Weblogic配置jms服务文档,是自己总结网上搜集到的资料以及自己亲自动手配置测试的总结。
- ggplot2设置坐标轴范围_6.6 坐标轴:设置坐标轴上刻度的显示位置
- 一夜没睡,仍然精力充沛——工作规划-2013.07.23
- 用python绘制叠加等边三角形_使用turtle库绘制叠加等边三角形
- iOS 极光推送没有声音怎么办?
- 织梦DEDECMS QQ一键登录插件返回空白解决方法
- kotlin中的var和val与编译时常量
热门文章
- php 清除浮动,清除浮动的几种方法
- img标签显示不出图片_前端开发,原生 JS 实现最简单的图片懒加载
- silverlight html 传参,Silverlight与html、JavaScript三者交互
- IEquatable「T」和Equal详解
- Mac Idea批量删除空行
- python cursor游标_python tuble、lambda及cursor游标相关
- 设计模式之——观察者模式
- logback按等级输出到不同日志文件
- python需要excel基础吗_Python实现和Excel基础功能对应关系
- java调python代码_java调用python代码