题目链接: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. [android] 切换界面的通用处理

    实现不改变activity,只切换View 抽取View界面的基类 利用面向对象多态的思路,实现通用 TitleManager.java 管理标题 package com.tsh.lottery.vi ...

  2. JS里的居民们4-数组((堆)队列

    编码1(队头在最右) 练习如何使用数组来实现队列,综合考虑使用数组的 push,pop,shift,unshift操作 基于代码,实现如按钮中描述的功能: 实现如阅读材料中,队列的相关入队.出队.获取 ...

  3. 1003. 猜数游戏 (Standard IO)

    题目描述 有一个“就是它”的猜数游戏,步骤如下:请你对任意输入的一个三位数x,在这三位数后重复一遍,得到一个六位数,467-->467467.把这个数连续除以7.11.13,输出最后的商. 输入 ...

  4. 1-4 Sass的基本特性-基础

    [Sass]声明变量 定义变量的语法: 在有些编程语言中(如,JavaScript)声明变量都是使用关键词“var”开头,但是在 Sass 不使用这个关键词,而是使用大家都喜欢的美元符号“$”开头.我 ...

  5. SSIS 使用OLEDB/ADO NET Source 数据流source控件 连接Oracle失败

    在做数据提取的时候发现一个非常奇怪的问题. Oracle客户端是安装正确并且Toad可以正常运行的,但是在新建OLEDB/ADO NET Source 数据流source控件连接Oracle的时候一直 ...

  6. 浅谈JavaScript之function用括号包起来

    (function a(){}) (命名函数表达式)会返回这个函数(不会执行),但是在括号外面无法调用该函数,需要一个变量接收这个函数,var fun = (function a(){}),一般这个用 ...

  7. Aysnc-callback with future in distributed system

    Aysnc-callback with future in distributed system

  8. 【阿里云产品公测】在ACE上部署WP测试体验

      ACE服务其实已经有很多类似的服务提供商了,无论收费的还是免费的,  但是到现在为止还没有体验过,正好借着这次机会,来体验一下阿里云的ACE服务. ' !2NSv   /IQ$[WR cx   B ...

  9. Ubuntu上如何搭建Android开发环境

    1.以下是开始Android应用程序编程之前需要的软件列表: a.Java JDK5 及以后版本 b.Java运行环境 c.Android Studio 2.安装Android Studio: x64 ...

  10. 通过游戏学敏捷:只通过Specification来传递需求

    转自:https://mp.weixin.qq.com/s/jAYbAMUTNYGh4RxGPAZ1AQ 活动把每个小组(4-5个人)中的2人留在屋子里,其他人到屋子外面等待.在屋子里的人,会得到一张 ...