codeforces 659F . Polycarp and Hay 搜索
题目链接
遍历每个点, 如果这个点的值能被k整除并且k/a[i][j]后小于等于n*m, 那么就对这个点进行搜索。
将这个点加入队列, 将周围的所有大于等于这个点的值的点也加入队列。 不断重复, 直到队列空或者数量满足要求。
可以加一个额外的数组, 如果搜索的过程中, 这个值和a[i][j]相同, 那么就把这个格子标记, 减少额外的操作。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1005;
int vis[maxn][maxn], a[maxn][maxn], used[maxn][maxn];
int n, m;
ll sum;
int solve() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int cnt = 0;
if(sum % a[i][j] == 0 && sum/a[i][j] <= n*m && !vis[i][j]) {
queue <pll> q;
q.push(mk(i, j));
mem(used);
while(!q.empty()) {
int x = q.front().fi, y = q.front().se;
q.pop();
if(used[x][y])
continue;
cnt++;
used[x][y] = 1;
if(cnt == sum/a[i][j]) {
return a[i][j];
}
for(int k = 0; k < 4; k++) {
int tmpx = x+dir[k][0];
int tmpy = y+dir[k][1];
if(tmpx<=0||tmpx>n||tmpy<=0||tmpy>m||a[tmpx][tmpy]<a[i][j]) {
continue;
}
if(a[tmpx][tmpy] == a[i][j]) {
vis[tmpx][tmpy] = 1;
}
q.push(mk(tmpx, tmpy));
}
}
}
}
}
return -1;
}
int main()
{
cin>>n>>m>>sum;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
int ans = solve();
if(ans == -1) {
puts("NO");
} else {
puts("YES");
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(used[i][j]) {
printf("%d ", ans);
} else {
printf("0 ");
}
}
puts(" ");
}
}
return 0;
}
codeforces 659F . Polycarp and Hay 搜索的更多相关文章
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- Codeforces 659F Polycarp and Hay【BFS】
有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...
- CodeForces 659F Polycarp and Hay
并查集,$dfs$. 从大的数字往里加,每加一个数字合并一下连通块,判断连通块内数字个数是否够,以及k能不能被当前加入的数字整除.然后$dfs$一下构造答案. #pragma comment(link ...
- codeforces 659F F. Polycarp and Hay(并查集+bfs)
题目链接: F. Polycarp and Hay time limit per test 4 seconds memory limit per test 512 megabytes input st ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...
- 【17.69%】【codeforces 659F】Polycarp and Hay
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- Codeforces 723C. Polycarp at the Radio 模拟
C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- CodeForces - 586D Phillip and Trains 搜索。vis 剪枝。
http://codeforces.com/problemset/problem/586/D 题意:有一个3*n(n<100)的隧道.一个人在最左边,要走到最右边,每次他先向右移动一格,再上下移 ...
随机推荐
- Java中两种实现多线程方式的对比分析
本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...
- Git 和 SVN之间的五个基本区别
GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...
- JavaScript判断IE各版本完美解决方案
解决方案 IE知道自身毛病很多,于是提供的一套官方的HTML hack方式: <!--[if IE]> // 全部IE版本可见 <![endif]--> <!--[if ...
- javascript定时器(上)
(一).setInterval 间隔性 function show(){ alert(‘a’); } setInterval(show,1000); 每隔1000毫秒(1秒)执行一次show这个函数: ...
- hdu 5755 Gambler Bo 高斯消元
题目链接 给n*m的方格, 每个格子有值{0, 1, 2}. 然后可以对格子进行操作, 如果选择了一个格子, 那么这个格子的值+2, 这个格子上下左右的格子+1, 并且模3. 问你将所有格子变成0的操 ...
- [方法]本来好的中文在winEdt中打开变成乱码
场景:本来在winEdt中使用中文的tex文件,使用xelatex可以编译成pdf,今天打开该tex文件,所有中文变得不可读,统统乱码. 解决方法:在保存tex文件时使用的是utf-8保存的,所以在打 ...
- Android各层推荐开发书籍及参考资料
Android各层推荐开发书籍及参考资料 转自:http://blog.csdn.net/fancylovejava/article/details/8657058 Android系统按照架构来说一共 ...
- position:relative可以默认提升元素的z-index;
position:relative可以默认提升元素的z-index; 相对没有添加position的元素来说:
- Tkinter类之窗口部件类
Tkinter类之窗口部件类 Tkinter支持15个核心的窗口部件,这个15个核心窗口部件类列表如下:窗口部件及说明:Button:一个简单的按钮,用来执行一个命令或别的操作.Canvas:组织图形 ...
- iOS 唯一设备号
https://github.com/fabiocaccamo/FCUUID 目前比较好的解决方案.