NC200069. Binbin's treasure map
描述
Binbin is a clever boy who likes to wear a skirt. One day, he saw some beautiful skirts on Taobao, but he had not enough money, which made him sad.
In order to make Binbin feel happy again, his's friend Xu1024 gave him a treasure map. The treasure map is composed of three characters: '.', '#', '$'.
Character '.' indicates that this position is empty and passable;
Character '#' indicates that this position is blocked by a stone and not passable;
Character '$' indicates that this position has 1 coin on it and is passablle.
The locations outside the treasure map are all empty and passable.
Because Binbin wanted to wear a skirt so much, he had a chance to teleport to a certain grid as another starting point.
Binbin wanted to know how many coins he could eventually collect to buy skirts.
Can you help him?输入描述
The first line of each group contains two numbers N and M,(1<=N, M<=500), representing the number of rows and the number of columns of treasure map.
Then there are next N lines, each line contains M characters. which describe the terasure map.
输出描述
Ouput a integer representing
the maxinum money Binbin would collected.
示例1
输入:
4 4 .$.$ .##. #$#. .#.#
输出:
3
说明:
Binbin can start at point(1,1) so he can collect 2 coins.示例2
输入:
4 4 .#$# #### ..$$ $$$$
输出:
7
说明:
Binbin can start at point(1,3) so he can collect 1 coin.C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 480K, 提交时间: 2020-01-11 16:07:00
#include<iostream> #include<string> using namespace std; const int MAX=500+10; char a[MAX][MAX]; int main() { int n,m,sum=0; cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>a[i][j]; if(a[i][j]=='$') sum++; } } cout<<sum<<endl; }
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 492K, 提交时间: 2020-01-11 16:20:35
#include<stdio.h> int main() { char a[505][505]; int n,m; scanf("%d %d",&n,&m); for(int i=0;i<n;i++) { scanf("%s",&a[i]); } int sum=0; for(int i=0;i<n;i++) { for(int u=0;u<m;u++) { if(a[i][u]=='$') sum++; } } printf("%d",sum); }
C(clang 3.9) 解法, 执行用时: 3ms, 内存消耗: 220K, 提交时间: 2020-01-11 13:19:03
#include<stdio.h> int main () { char ch; int n,m,i,j,h=0; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) { scanf("\n"); for(j=1;j<=m;j++) { scanf("%c",&ch);if(ch=='$') h++;} } printf("%d",h); return 0; }