题目链接

遍历每个点, 如果这个点的值能被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. table中超长字符串省略号表示两种方法

    写在前面:   1.第一种从网上找到的解决方式添加table-layout:fixed   2.第二种添加div   3.字符串过长产生省略号的css语句为如下三种合用:overflow:hidden ...

  2. asp.net MVC 验证注解

    对于Web系统,对于用户的输入验证是必须的.不仅需要在客户端对用户的输入进行验证,在服务端也需要对用户的执行进行验证. asp.net MVC中对于验证提供了一种注解机制.注解是一种通用机制,可以用来 ...

  3. C#读取csv格式文件

    方法1:用一个System.Web.UI.HtmlControls.HtmlInputFile去handle文件选取 以下是button click event中的code,用来执行当文件选取了之后读 ...

  4. lua学习笔记(1)-基本语法

    ==============变量类型nilnumber(实数)    1 2 3.14 7.65e8string            "hello world" "\n ...

  5. 使用正则表达式限制TextBox输入

    /// <summary> /// 使用正则表达式限制输入 /// </summary> public class TextBoxRegex : TextBox { // 正则 ...

  6. 大型项目使用Automake/Autoconf完成编译配置

    http://www.cnblogs.com/xf-linux-arm-java-android/p/3590770.htmlhttp://blog.csdn.net/zengraoli/articl ...

  7. 如何用ATL创建ActiveX控件

    演示截图: 代码简介或代码解析: 如何用ATL创建ActiveX控件 实现了一个ActiveX控件,它在一个圆内部有个正多边形,当用户在多变形内部单击将会使多边形的边数在当前的基础上+1,在多变形外部 ...

  8. 我使用过的Linux命令之date - 显示、修改系统日期时间

    原文地址:http://www.cnblogs.com/diyunpeng/archive/2011/11/20/2256538.html 用途说明 ate命令可以用来显示和修改系统日期时间,注意不是 ...

  9. Magento布局layout.xml文件详解

    解析顺序 布局xml文件一般位于app/design/{area}/{package}/{theme}/layout/目录下.Layout文件一般包含block.reference.action三种标 ...

  10. 在win7下php查询数据库, 连接被重置

    解决方法一 将 php5 目录下的libmysql.dll拷贝至 windows/system32和apache/bin下 解决方法二 在apache/conf/httpd.conf文件中添加 Loa ...