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)的隧道.一个人在最左边,要走到最右边,每次他先向右移动一格,再上下移 ...
随机推荐
- sqlserver 2008存储过程 多个可空条件任意组合
很多程序员在实际开发中,经常遇到这种情况,列表上方有很多条件,包含下拉框,输入框等等,这些条件都可以不输入,如果我们需要写一个存储过程,很多条件挨个判断是否为空,且进行任意组合,任何一个开发人员都会疯 ...
- Nodejs随笔(三):全局对象之global
首先,进入node REPL: mesogene@mesogene-team:~$ node > 查看global对象,发现其他全局对象(包括Buffer.require对象)以及全局方法都包含 ...
- iOS断点及打印日志
首先,最简单的断点就是在Xcode项目文件中任意一行行号那点一下,就是加了一个断点 再次点击会变成浅蓝色,表示disable掉了 disable掉的断点不会起作用,但会在左上角蓝色的标签那留下记录,这 ...
- c++数组操作
一.数组定义和初始化 : 一维数组初始化: : 标准方式一: ]; // value[i]的值不定,没有初始化 : 标准方式二: ] = {,}; // value[0]和value[1]的值分别为1 ...
- effective c++ 条款06 不想自动生成函数,就明确拒绝
编辑器会主动的生成三个/五个函数,如果不需要我们应该主动拒绝 使用私有属性来拒绝 ``` include int main() { return 0; } ``` 使用继承的方式来拒绝
- MySQL安全问题
使用MySQL,安全问题不能不注意.以下是MySQL提示的23个注意事项:1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信.2.用set pass ...
- MYSQL设置字段数据过长自动截断
自动截断如下设置: windows: 修改my.ini: [mysqld] sql-mode="STRICT_TRANS_TABLES" linux: 修改/ect/mysql/m ...
- jquery插件的编写
今天尝试了一下自己编写插件.最简单的jquery效果,返回顶部的按钮. 增加多个全局函数 添加多个全局函数,可采用如下定义: Java代码 jQuery.foo = function() { aler ...
- 初学swift笔记 流程控制(五)
import Foundation ; i<=; i++ { println(i) } let str1="adl;fjasdfl;ouewrouqwperuadf" for ...
- java改变图片文件尺寸
package test.common; import java.awt.Graphics; import java.awt.Image; import java.awt.image.Buffered ...