Best Grass
Description
Bessie is planning her day of munching tender spring grass and is gazing
out upon the pasture which Farmer John has so lovingly partitioned into a
grid with R (1 <= R <= 100) rows and C (1 <= C <= 100) columns. She wishes
to count the number of grass clumps in the pasture. Each grass clump is shown on a map as either a single '#' symbol or perhaps
two '#' symbols side-by-side (but not on a diagonal). Given a map of the
pasture, tell Bessie how many grass clumps there are. By way of example, consider this pasture map where R=5 and C=6: .#....
..#...
..#..#
...##.
.#.... This pasture has a total of 5 clumps: one on the first row, one that spans
the second and third row in column 2, one by itself on the third row, one
that spans columns 4 and 5 in row 4, and one more in row 5.
Input
* Line 1: Two space-separated integers: R and C * Lines 2..R+1: Line i+1 describes row i of the field with C
characters, each of which is a '#' or a '.'
Output
* Line 1: A single integer that is the number of grass clumps Bessie
can munch
5 6
.#....
..#...
..#..#
...##.
.#....
#include<cstdio>
#include<iostream>
using namespace std;
int x[4]={1,0,-1,0},y[4]={0,1,0,-1};//四个方向
char Map[105][105];
int C,R,ans;
void DFS(int i,int j){
Map[i][j]='.';
for(int t=0;t<4;t++){
int xn=i+x[t];
int yn=j+y[t];
if(xn>=0&&xn<R&&yn>=0&&yn<C&&Map[xn][yn]=='#')
DFS(xn,yn);
}
}
int main ()
{
while(~scanf("%d%d",&R,&C)){
ans=0;
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
cin>>Map[i][j];
for(int i=0;i<R;i++)
for(int j=0;j<C;j++){
if(Map[i][j]=='#'){
DFS(i,j);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
#include<iostream>
using namespace std;
int x[4]={1,0,-1,0},y[4]={0,1,0,-1};//四个方向
char Map[105][105];
int C,R,ans;
void DFS(int i,int j){
Map[i][j]='.';
for(int t=0;t<4;t++){
int xn=i+x[t];
int yn=j+y[t];
if(xn>=0&&xn<R&&yn>=0&&yn<C&&Map[xn][yn]=='#')
DFS(xn,yn);
}
}
int main ()
{
while(~scanf("%d%d",&R,&C)){
ans=0;
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
cin>>Map[i][j];
for(int i=0;i<R;i++)
for(int j=0;j<C;j++){
if(Map[i][j]=='#'){
DFS(i,j);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
Best Grass的更多相关文章
- hdu----(1849)Rabbit and Grass(简单的尼姆博弈)
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1849 Rabbit and Grass 博弈论
水题,转化Nim 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include&l ...
- 10382 - Watering Grass
Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...
- Rabbit and Grass(杭电1849)(尼姆博弈)
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1849 Rabbit and Grass
题解:因为棋子可重叠,所以就等于取石子问题,即尼姆博弈,SG[i]=i,直接将输入数据异或即可. #include <cstdio> int main(){ int SG,n,a; whi ...
- HDU1849 Rabbit and Grass()
用异或看取得的值是否为0推断 思想换没搞懂 #include<stdio.h> int main() { int ans,n,a; while(scanf("%d",& ...
- [补档][Usaco2015 Jan]Grass Cownoisseur
[Usaco2015 Jan]Grass Cownoisseur 题目 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过? (一个点在路 ...
- Grass Cownoisseur[Usaco2015 Jan]
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
随机推荐
- VBS脚本代码(手工编写---在windows 7上调用系统对话框,来选择文件)
'=========================================================================='' VBScript Source File - ...
- js常用函数大全107个
1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...
- innerhtml 和value值有什么区别
value 值写在标签里面的,innerHTML写在<button type="button" onclick="myFunction()">Try ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- org.hibernate.exception.JDBCConnectionException: could not execute query
最近在做一个项目,测试的时候是没有问题的,但是放到服务器上以后,第二天就会出现下面的异常.重启Tomcat服务器后就正常了,但是下一天还是会出现同样的异常..... 我就查了一些资料最终把问题给解决了 ...
- 百度前端面试题-类似slack的在线聊天室
别人国庆出去玩,我在家写代码的感觉也是很不错哒. 首先介绍一下技术架构吧! 使用了js框架:FFF,zepto,jquery,md5.min.js 前端框架:Bootstrap 后端:野狗,部分PHP ...
- hibernate ——helloWorld程序(annotation配置)
在 <hibernate ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...
- MySQL与Oracle的区别
1.语法上的区别 变量类型定义.IN OUT的位置.变量定义的位置.游标的位置.异常的位置: 2.MySQL没有 return 关键字,采用leave label的方式结束循环或跳出存储 3.异常处 ...
- codeforces 689B Mike and Shortcuts 最短路
题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...
- Shell命令替换与变量替换
命令替换 命令替换是指Shell可以先执行命令,将输出结果暂时保存,在适当的地方输出.命令替换的语法: `command` 注意是反引号,不是单引号,这个键位于 Esc 键下方.下面的例子中,将命令执 ...