题目链接

遍历每个点, 如果这个点的值能被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 搜索的更多相关文章

  1. Codeforces 659F Polycarp and Hay 并查集

    链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...

  2. Codeforces 659F Polycarp and Hay【BFS】

    有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...

  3. CodeForces 659F Polycarp and Hay

    并查集,$dfs$. 从大的数字往里加,每加一个数字合并一下连通块,判断连通块内数字个数是否够,以及k能不能被当前加入的数字整除.然后$dfs$一下构造答案. #pragma comment(link ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【17.69%】【codeforces 659F】Polycarp and Hay

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  8. Codeforces 723C. Polycarp at the Radio 模拟

    C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  9. CodeForces - 586D Phillip and Trains 搜索。vis 剪枝。

    http://codeforces.com/problemset/problem/586/D 题意:有一个3*n(n<100)的隧道.一个人在最左边,要走到最右边,每次他先向右移动一格,再上下移 ...

随机推荐

  1. asp.net mvc ajax提交例子

    @{ Layout = null; } <script src="../../Scripts/jquery-1.10.2.min.js" type="text/ja ...

  2. iOS self和super的区别

    self和super的区别 #import <Foundation/Foundation.h> 首先先写两个类 fist和two,two继承fist类 @interface First:N ...

  3. NSRangeFromString(<#NSString * _Nonnull aString#>) 和rangeOfString

    NSRangeFromString NSString *str1 = @"abcdef"; NSString *str2 = @"1-105"; NSStrin ...

  4. (c#)WinForm遍历所有控件

    遍历WinForm中的所有控件,只执行一次以下语句是不行的 foreach (Control ctl in this.Controls) { } 这样只能遍历到第一层控件,子控件是遍历不到的,要想遍历 ...

  5. js的两种数据类型

    js中的数据类型总体来说分为两种,他们分别是: 1 值类型(基本类型):数值型(Number),字符类型(String),布尔值型(Boolean),null 和 underfined 2 引用类型( ...

  6. 图的最短路径问题————树上奶牛(tree.cpp)

    和往常一样,继续从题目引入 树上奶牛 (tree.cpp) [题目描述] 农夫John的奶牛不是住在地上而是住在树上的QWQ. 奶牛之间需要串门,不过在串门之前他们会向John询问距离的大小.可是Jo ...

  7. Javascript基本概念(语句和函数)

    语句 for语句 for语句中的初始化表达式,控制表达式和循环后表达式都是可选的,将这三个表达式省略,就会创建一个无线循环. ECMAScript中不存在块级作用域,因此在循环内容部定义的变量也可以在 ...

  8. php 截取字符串

    /** * 方法库-截取字符串-[该函数作者未知] * @param string $string 字符串 * @param int $length 字符长度 * @param string $dot ...

  9. jchat:linux聊天程序2:MySQL

    该软件使用的数据库为MySQL,因为它免费.开源,在linux下几乎就是最好的选择. 首先要在mysql中root用户新建数据库并赋权给本用户: create database jchat; gran ...

  10. linux查看网卡个数及速度

    # lspci | grep Ethernet03:00.0 Ethernet controller: Broadcom Corporation NetXtreme II BCM5708 Gigabi ...