http://noi.openjudge.cn/ch0108/17/

总时间限制: 10000ms   单个测试点时间限制: 1000ms  内存限制: 65536kB
描述

奶牛Bessie计划好好享受柔软的春季新草。新草分布在R行C列的牧场里。它想计算一下牧场中的草丛数量。

在牧场地图中,每个草丛要么是单个“#”,要么是有公共边的相邻两个“#”。给定牧场地图,计算有多少个草丛。

例如,考虑如下5行6列的牧场地图

.#....
..#...
..#..#
...##.
.#....

这个牧场有5个草丛:一个在第一行,一个在第二列横跨了二、三行,一个在第三行,一个在第四行横跨了四、五列,最后一个在第五行。

输入
第一行包含两个整数R和C,中间用单个空格隔开。
接下来R行,每行C个字符,描述牧场地图。字符只有“#”或“.”两种。
输出
输出一个整数,表示草丛数。
样例输入
5 6
.#....
..#...
..#..#
...##.
.#....
样例输出
5
来源
USACO Open 2008 Bronze

解析:

广搜。具体步骤就是扫描二维数组,发现'#'号则从该坐标出发进行广搜,搜索的同时把搜过的'#'号修改为'.'   。

 #include <stdio.h>
int count=,R,C;
void bfs(char a[][],int i,int j);//从a[i][j]出发做bfs
int main(int argc, char *argv[])
{
int i,j;
char a[][];
//freopen("17.in","r",stdin);
//freopen("17.txt","w",stdout);
scanf("%d%d",&R,&C);
for(i=;i<R;i++) scanf("%s",a[i]); for(i=;i<R;i++)
{
for(j=;j<C;j++)
{
if(a[i][j]=='#') bfs(a,i,j);
}
}
printf("%d\n",count);
return ;
}
void bfs(char a[][],int i,int j)
{
int queueArr[][];
int begin,end,number;//队头,队尾,队列元素数目
int x,y,xx,yy; count++;//草块数目增加1 a[i][j]='.';
queueArr[][]=i;
queueArr[][]=j;
begin=;
end=;
number=; while(number>)//当队列不为空时继续循环
{
//读取队头元素的值
x=queueArr[begin][];
y=queueArr[begin][]; //查询队头元素的周围节点是否'#'
xx=x;
yy=y-;
if(yy>=&&a[xx][yy]=='#')
{
a[xx][yy]='.';
queueArr[end][]=xx;
queueArr[end][]=yy;
end++;
number++;
} xx=x;
yy=y+;
if(yy<C&&a[xx][yy]=='#')
{
a[xx][yy]='.';
queueArr[end][]=xx;
queueArr[end][]=yy;
end++;
number++;
} xx=x-;
yy=y;
if(xx>=&&a[xx][yy]=='#')
{
a[xx][yy]='.';
queueArr[end][]=xx;
queueArr[end][]=yy;
end++;
number++;
} xx=x+;
yy=y;
if(xx<R&&a[xx][yy]=='#')
{
a[xx][yy]='.';
queueArr[end][]=xx;
queueArr[end][]=yy;
end++;
number++;
} begin++;//队头出队
number--;//队列元素数目减少1个
}
}

深搜代码:

 #include <stdio.h>
int count=,R,C;
char a[][];
int dx[]={-,,,};//上下左右
int dy[]={,,-,};
void dfs(int i,int j);//从a[i][j]出发做bfs
int main(int argc, char *argv[])
{
int i,j; freopen("1.in","r",stdin);
//freopen("1.txt","w",stdout);
scanf("%d%d",&R,&C);
for(i=;i<R;i++) scanf("%s",a[i]); for(i=;i<R;i++)
{
for(j=;j<C;j++)
{
if(a[i][j]=='#') { count++; dfs(i,j); }
}
}
printf("%d\n",count);
return ;
}
void dfs(int i,int j)//从a[i][j]出发做dfs
{
int xx,yy;
a[i][j]='.';
for(int k=;k<;k++)
{
xx=dx[k]+i; yy=j+dy[k];
if(xx>=&&xx<R&&yy>=&&yy<C&&a[xx][yy]=='#') dfs(xx,yy);
}
}

简化的广搜代码

 #include <stdio.h>
int count=,R,C;
int dx[]={-,,,};//上下左右
int dy[]={,,-,};
void bfs(char a[][],int i,int j);//从a[i][j]出发做bfs
int main(int argc, char *argv[])
{
int i,j;
char a[][];
freopen("1.in","r",stdin);
//freopen("1.txt","w",stdout);
scanf("%d%d",&R,&C);
for(i=;i<R;i++) scanf("%s",a[i]); for(i=;i<R;i++)
{
for(j=;j<C;j++)
{
if(a[i][j]=='#') bfs(a,i,j);
}
}
printf("%d\n",count);
return ;
}
void bfs(char a[][],int i,int j)
{
int queueArr[][];
int begin,end,number;//队头,队尾,队列元素数目
int x,y,xx,yy; count++;//草块数目增加1 a[i][j]='.';
queueArr[][]=i;
queueArr[][]=j;
begin=;
end=;
number=; while(number>)//当队列不为空时继续循环
{
//读取队头元素的值
x=queueArr[begin][];
y=queueArr[begin][];
//查询队头元素的周围节点是否'#'
for(int k=;k<;k++)
{
xx=dx[k]+x; yy=dy[k]+y;
if(xx>=&&xx<R&&yy>=&&yy<C&&a[xx][yy]=='#')
{
a[xx][yy]='.';
queueArr[end][]=xx;
queueArr[end][]=yy;
end++;
number++;
}
}
begin++;//队头出队
number--;//队列元素数目减少1个
}
}

使用STL队列的广搜:

 #include<stdio.h>
#include<queue>
using namespace std; struct obj
{
int x,y;
}; int count=,R,C;
int dx[]={-,,,};//上下左右
int dy[]={,,-,}; void bfs(char a[][],int i,int j);//从a[i][j]出发做bfs
int main(int argc, char *argv[])
{
int i,j;
char a[][];
freopen("1.in","r",stdin);
//freopen("1.txt","w",stdout);
scanf("%d%d",&R,&C);
for(i=;i<R;i++) scanf("%s",a[i]); for(i=;i<R;i++)
{
for(j=;j<C;j++)
{
if(a[i][j]=='#') bfs(a,i,j);
}
}
printf("%d\n",count);
return ;
}
void bfs(char a[][],int i,int j)
{
int xx,yy;
queue<struct obj> queA;
struct obj temp; count++;//草块数目增加1 a[i][j]='.';//访问当前出发点
temp.x=i;
temp.y=j;
queA.push(temp);//出发点入队 while(!queA.empty())//当队列不为空时继续循环
{
//查询队头元素的周围节点是否'#'
for(int k=;k<;k++)
{
xx=dx[k]+queA.front().x;
yy=dy[k]+queA.front().y;
if(xx>=&&xx<R&&yy>=&&yy<C&&a[xx][yy]=='#')
{
a[xx][yy]='.';
struct obj t;
t.x=xx;
t.y=yy;
queA.push(t);
}
}
queA.pop();//删除队头
}
}

下面同样是使用了STL队列的广搜,不过在结构体定义时使用了结构体构造函数,可以看看:

 #include<stdio.h>
#include<queue>
using namespace std; struct obj
{
int x,y;
obj(int xx,int yy){ x=xx;y=yy; }
obj(){ x=;y=; }
}; int count=,R,C;
int dx[]={-,,,};//上下左右
int dy[]={,,-,}; void bfs(char a[][],int i,int j);//从a[i][j]出发做bfs
int main(int argc, char *argv[])
{
int i,j;
char a[][];
freopen("1.in","r",stdin);
//freopen("1.txt","w",stdout);
scanf("%d%d",&R,&C);
for(i=;i<R;i++) scanf("%s",a[i]); for(i=;i<R;i++)
{
for(j=;j<C;j++)
{
if(a[i][j]=='#') bfs(a,i,j);
}
}
printf("%d\n",count);
return ;
}
void bfs(char a[][],int i,int j)
{
int xx,yy;
queue<struct obj> queA;
struct obj temp; count++;//草块数目增加1 a[i][j]='.';//访问当前出发点
//temp.x=i;
//temp.y=j;
//queA.push(temp);//出发点入队
queA.push(obj(i,j)); while(!queA.empty())//当队列不为空时继续循环
{
//查询队头元素的周围节点是否'#'
for(int k=;k<;k++)
{
xx=dx[k]+queA.front().x;
yy=dy[k]+queA.front().y;
if(xx>=&&xx<R&&yy>=&&yy<C&&a[xx][yy]=='#')
{
a[xx][yy]='.';
//struct obj t;
//t.x=xx;
//t.y=yy;
//queA.push(t);
queA.push(obj(xx,yy));
}
}
queA.pop();//删除队头
}
}

openjudge-最好的草的更多相关文章

  1. 【OpenJudge 8463】Stupid cat & Doge

    http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...

  2. 【OpenJudge 191】【POJ 1189】钉子和小球

    http://noi.openjudge.cn/ch0405/191/ http://poj.org/problem?id=1189 一开始忘了\(2^{50}\)没超long long差点写高精度Q ...

  3. 【OpenJudge 1665】完美覆盖

    http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...

  4. 【OpenJudge 1793】矩形覆盖

    http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...

  5. OpenJudge 2990:符号三角形 解析报告

    2990:符号三角形 总时间限制:  1000ms       内存限制:  65536kB 描述 符号三角形的第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“ ...

  6. codevs 2801 LOL-盖伦的蹲草计划

     时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲 ...

  7. noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T11——T20

    T11 图像旋转 描述 输入一个n行m列的黑白图像,将它顺时针旋转90度后输出. 输入 第一行包含两个整数n和m,表示图像包含像素点的行数和列数.1 <= n <= 100,1 <= ...

  8. OpenJudge解题经验交流

    1.1编程基础之输入输出01:Hello, World! 02:输出第二个整数PS:a,b需用longint类型接收 03:对齐输出 04:输出保留3位小数的浮点数 05:输出保留12位小数的浮点数 ...

  9. Unity3D手游开发日记(9) - 互动草的效果

    所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度. 前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动 ...

随机推荐

  1. window上利用pip安装pandas

    官网推荐的是直接使用Anoconda,它集成了pandas,可以直接使用.安装挺简单的,有windows下的安装包.如果不想安装庞大的Anoconda,那就一步一步用pip来安装pandas.下面我主 ...

  2. 最小系统加载工具 systemjs

    systemjs 是一个最小系统加载工具,用来创建插件来处理可替代的场景加载过程,包括加载 CSS 场景和图片,主要运行在浏览器和 NodeJS 中.它是 ES6 浏览器加载程序的的扩展,将应用在本地 ...

  3. jquery中的children()和contents()的区别

    1.children()只会返回元素节点 2.contents()还可以返回文本节点

  4. (转) 浅析HTML5在移动应用开发中的使用

    (转)浅析HTML5在移动应用开发中的使用 (原)http://www.iteye.com/magazines/67   2012-03-07  来自 UECD.163.com  编辑 wangguo ...

  5. php实现函数重载

    java..net等强类型预言中都有方法重载,但是PHP是弱类型语言,不能确定参数的类型, 而且如果php定义的方法接收一个参数,调用的时候传入多个也不会有问题,所以不能进行重载. 但是我们可以通过p ...

  6. 使用java发送邮件

    首先要加入mail.jar包 import java.io.UnsupportedEncodingException; import java.util.Properties; import java ...

  7. HTML 样式属性

    @charset "utf-8"; /* CSS Document */ <style> p{ /*背景与前景*/ background-color:#000;/*背景 ...

  8. 【Python】个人所得税

    以月收入1w,举例计算个税: #!/usr/bin/python #-*- encoding:UTF-8 -*- #========================================== ...

  9. bookstores网上书店测试缺陷报告1

    Bookstore网上书店系统测试缺陷报告   缺陷编号 01.01.0001 发现人 吴赵昕 记录日期 2016-06-10 所属模块 购物车 确认人 吴赵昕 确认日期 2016-06-10 当前状 ...

  10. RabbitMQ - 引入库产生的一次pthread_create错误

    最近在项目中使用rabbitMQ,在引入编译生成的libamqpcpp.so库文件,由于各个文件夹之间需要使用静态库进行连接,所以在引入libamqpcpp.so基础上再进行了一次.a文件生成.编译执 ...