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的互动 ...
随机推荐
- sql转db,后台坑货
打开 创建一个db文件然后点击文件--新建---Sqlite 导入空db成功后点击左侧栏 点击表 点击右上角+号把sql文件的语句复制粘贴到 然后点击运行,运行完成后保存ok
- hdu 2066
ps:我天...之前看了迪杰斯特拉..现在这题要用到floyd..就是先建一个图,然后从列开始遍历,每列里遍历行,行又对应每列... 从A列开始遍历每行,比如遍历到B,这时候B->A知道是2,接 ...
- 1st贝塞尔函数的使用
x=-100:0.1:100; y1=besselj(7,x);y2=besselj(10,x);y3=besselj(20,x);y4=besselj(40,x);y5=besselj(60,x); ...
- Valid Palindrome ---- LeetCode 125
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- IBM Domino 9 出现 Domino Designer 您正在试图升级多用户安装。请获取正确的安装包以完成升级。 解决方案
如果网上搜索的其他方法解决不了,那么我的这个方法可以试一下. 出现的场景: 先装好了Notes,后准备装Designer. 在装Designer解压包之后,出现下面的错误,不能安装: 您正在试图升级多 ...
- Windows平台下PHP环境搭建
在Windows平台上搭建PHP的开发环境可以下载WAMP(Windows.Apache.MySQL.PHP的首字母缩写)集成化安装包.这样就不需要单独安装Apache.MySQL和PHP了. 这款软 ...
- maven - Eclipse构建maven项目
前面的博文已经介绍了如何安装maven,本文将记录如何在Eclipse下构建maven项目. 一.Eclipse maven插件安装 关于安装Eclipse maven插件,网上有很多方法,这里推荐一 ...
- PAT (Advanced Level) Practise:1027. Colors in Mars
[题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...
- HTML5地理定位
1.通过navigator.geolocation来获取设备当前位置,返回一个位置对象,可以获得当前经纬度相关信息: 2.navgatior.geolocation的三个方法: getCurrentP ...
- Java方法调用中的别名处理
将一个对象传递到方法内部时,也会产生别名现象.//: PassObject.java// Passing objects to methods can be a bit tricky62class L ...