题目链接: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. Windbg 脚本命令简介 一

    Windbg  脚本命令简介 一 Windbg command r: registers的简写,可以显示或修改寄存器的值.浮点寄存器的值.定义别名变量. 可以显示当前线程下的寄存器值. The r c ...

  2. 读EntityFramework.DynamicFilters源码_心得_示例演示02

    上次对EntityFramework.DynamicFilters整体的项目结构有了一个认识,这次我们就通过阅读说明文档,示例项目,和单元测试,来动手构建一个我们的体验项目,通过对动态过滤器的使用,使 ...

  3. hdu 4055 Number String(递推DP)

    给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...

  4. 第4章 css文字text与字体font-face

    text-overflow 与 word-wrap text-overflow:用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出 ...

  5. nginx服务器绑定多个域名、支持pathinfo路由、隐藏index.php入口文件

    这篇文章仅仅是操作,解释说明部分待更新. 1. 修改nginx的配置文件(我的配置文件在/etc/nginx/nginx.conf) [root@xxx ~]# find / -name nginx. ...

  6. bat中实现代码拷贝到指定目录后启动命令行并更改默认路径

      ### window书写shell脚本,实现判断指定文件是否存在,存在就删除,然后复制新文件到此目录 ``` if exist "G:\test\test2\1.txt" (d ...

  7. 移动端 line-height 不垂直居中问题

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 一般情况下, 我们把 line-height 的值设置为 height 的 ...

  8. JAVA语法基础要点

  9. Linux ->> uname命令 -- 查看当前系统信息

    uname命令是用来打印当前Linux操作系统的信息,如果操作系统内核版本,操作系统是32位还是64位,计算机名字,操作系统安装时间,Linux操作系统类型 1) 操作系统是32位还是64位,感觉-m ...

  10. windows下安装配置RabbitMQ

    安装部署 1.当前环境以及参考资料出处 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com/install- ...