openjudge-最好的草
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-最好的草的更多相关文章
- 【OpenJudge 8463】Stupid cat & Doge
http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...
- 【OpenJudge 191】【POJ 1189】钉子和小球
http://noi.openjudge.cn/ch0405/191/ http://poj.org/problem?id=1189 一开始忘了\(2^{50}\)没超long long差点写高精度Q ...
- 【OpenJudge 1665】完美覆盖
http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...
- 【OpenJudge 1793】矩形覆盖
http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...
- OpenJudge 2990:符号三角形 解析报告
2990:符号三角形 总时间限制: 1000ms 内存限制: 65536kB 描述 符号三角形的第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“ ...
- codevs 2801 LOL-盖伦的蹲草计划
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲 ...
- noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T11——T20
T11 图像旋转 描述 输入一个n行m列的黑白图像,将它顺时针旋转90度后输出. 输入 第一行包含两个整数n和m,表示图像包含像素点的行数和列数.1 <= n <= 100,1 <= ...
- OpenJudge解题经验交流
1.1编程基础之输入输出01:Hello, World! 02:输出第二个整数PS:a,b需用longint类型接收 03:对齐输出 04:输出保留3位小数的浮点数 05:输出保留12位小数的浮点数 ...
- Unity3D手游开发日记(9) - 互动草的效果
所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度. 前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动 ...
随机推荐
- Spark MLlib 之 Naive Bayes
1.前言: Naive Bayes(朴素贝叶斯)是一个简单的多类分类算法,该算法的前提是假设各特征之间是相互独立的.Naive Bayes 训练主要是为每一个特征,在给定的标签的条件下,计算每个特征在 ...
- ThinkPHP(3)SQL查询语句
ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...
- 使用PHPExcel导出文件
使用PHPExcel导出文件步骤及解析: 新建一个excel表格:实例化PHPExcel类 创建sheet(内置表):createSheet()方法,创建新的sheet方法 setActiveShee ...
- javascript语言精粹
内容选自:<javascript语言精粹> 1.6种值会为假(==false),分别是false,null,undefined,' ',0,NaN 2.typeof有6种值,分别是'num ...
- 如何给Firefox附加组件签名
如何给Firefox附加组件签名 https://developer.mozilla.org/zh-CN/Add-ons/Distribution 2.打开https://addons.mozilla ...
- 【转】C#线程同步示例
using System; using System.Threading; // 银行帐户类 class Account { int balance; ...
- java java.lang.NoClassDefFoundError 的解决办法
以简单而经典的 "HelloWorld.java" 为例 不含包层次的HelloWorld.Java public class HelloWorld { public static ...
- Linux装无线驱动
- (转)JS获取当前对象大小以及屏幕分辨率等
原文 JS获取当前对象大小以及屏幕分辨率等 <script type="text/javascript">function getInfo(){ var ...
- Memcache学习笔记
以下内容大部分来自网络,小部分是本人遇到的问题融合后的记录. 先贴一段涨姿势~ Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全 ...