题目链接:https://www.luogu.org/problemnew/show/P2919

1.搜索的时候分清楚全局变量和局部变量的区别

2.排序优化搜索

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2000;
const int inf = 0x7fffffff;
int map[maxn][maxn], n, m, ans, cnt, tot;
bool vis[maxn][maxn];
struct hail{
int x, y, h;
}f[maxn * maxn];
bool cmp(hail a, hail b)
{
return a.h > b.h;
}
void dfs1(int x, int y)
{
tot++;
vis[x][y] = 1;
if(map[x][y] >= map[x+1][y] && vis[x+1][y] == 0) dfs1(x+1, y);
if(map[x][y] >= map[x-1][y] && vis[x-1][y] == 0) dfs1(x-1, y);
if(map[x][y] >= map[x+1][y+1] && vis[x+1][y+1] == 0) dfs1(x+1, y+1);
if(map[x][y] >= map[x+1][y-1] && vis[x+1][y-1] == 0) dfs1(x+1, y-1);
if(map[x][y] >= map[x-1][y+1] && vis[x-1][y+1] == 0) dfs1(x-1, y+1);
if(map[x][y] >= map[x-1][y-1] && vis[x-1][y-1] == 0) dfs1(x-1, y-1);
if(map[x][y] >= map[x][y+1] && vis[x][y+1] == 0) dfs1(x, y+1);
if(map[x][y] >= map[x][y-1] && vis[x][y-1] == 0) dfs1(x, y-1);
}
int main()
{
scanf("%d%d",&n,&m); for(int i = 0; i <= n + 1; i++)
for(int j = 0; j <= m + 1; j++)vis[i][j] = 1; for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&map[i][j]), f[++cnt].x = i, f[cnt].y = j, f[cnt].h = map[i][j], vis[i][j] = 0; sort(f+1, f+1+n*m, cmp); cnt = 0; while(tot < n * m)
{
ans++;
while(vis[f[cnt].x][f[cnt].y] == 1) cnt++;
dfs1(f[cnt].x, f[cnt].y);
for(int i = 1; i <= n; i++)
{for(int j = 1; j <= m; j++)
cout<<vis[i][j]<<" ";
cout<<endl;
}
cout<<endl;
} printf("%d",ans);
return 0;
}

【luogu P2919 [USACO08NOV]守护农场Guarding the Farm】 题解的更多相关文章

  1. bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm

    P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 按海拔是否相同分块 每次bfs海拔相 ...

  2. 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm

    P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...

  3. 洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm

    https://www.luogu.org/problem/show?pid=2919 题目描述 The farm has many hills upon which Farmer John woul ...

  4. 洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm

    题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...

  5. P2919 [USACO08NOV]守护农场Guarding the Farm

    链接:P2919 ----------------------------------- 一道非常暴力的搜索题 ----------------------------------- 注意的是,我们要 ...

  6. BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 491  S ...

  7. 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 498  Solve ...

  8. 洛谷 P3079 [USACO13MAR]农场的画Farm Painting

    P3079 [USACO13MAR]农场的画Farm Painting 题目描述 After several harsh winters, Farmer John has decided it is ...

  9. Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    基环树森林,然而我比较菜,直接tarjan找环. 发现缩点之后变成了DAG,每一个点往下走一定会走到一个环,缩点之后搜一遍看看会走到哪个环以及那个环的编号是多少,答案就是环的$siz$$ + $要走的 ...

随机推荐

  1. rabbit工作队列模式

    工作队列比简单队列在消费者这边多了一个方法. channel.basicQos(1);公平队列消费(参数设置为1,表示消费者消费完一条才会去接受再次发来的消息) 生产者: package com.kf ...

  2. SQLSTATE[HY000] [2002] No such file or directory in

    这个错误将数据库配置信息的localhost改成127.0.0.1就行了

  3. Java内存溢出定位和解决方案(new)

    引起内存溢出的原因有很多种,列举一下常见的有以下几种: 1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据:2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收:3.代码中存在死循环 ...

  4. awk获取外部变量

    语法 awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file...] 获得普通外部变量 [xingxing.dx ...

  5. Linux制作deb

    1.新建一个我们临时的工作目录mkdir deb 2.新建我们程序的目录mkdir hello 3.编写我们的程序 我们以我们最熟悉的helloworld程序做起,hello.c代码如下#includ ...

  6. JSON中的安全问题

    Web中使用JSON时最常见的两个安全问题: 1.跨站请求伪造: 即CSRF,是一种利用站点对用户浏览器信任发起攻击的方式.典型的就是JSON数组,更多信息请自行上网百度. 2.跨站脚本攻击. 是注入 ...

  7. Dapper 批量操作sql

    static void Main(string[] args) { private static readonly string sqlconnection =""; //遍历循环 ...

  8. deb文件怎么安装

    deb 是 ubuntu .debian 的格式.rpm 是 redhat .fedora .suse 的格式.deb是debian发行版的软件包ubuntu是基于debian 发行的 所有可以用.d ...

  9. LESS嵌套中的Mixins和classes

    less的嵌套规则对于有效组织你的css代码有较好的作用.其中使用mixin或者class都可以作为被嵌套的实体,但是二者还是有区别的: mixin必须由.name+(){}的pattern来定义,而 ...

  10. linux定时备份MySQL数据库并删除七天前的备份文件

    1.创建备份文件夹 #cd /bak#mkdir mysqldata 2.编写运行脚本 #nano -w /usr/sbin/bakmysql.sh 注:如使用nano编辑此代码需在每行尾添加’&am ...