B. Inna and New Matrix of Candies
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".

The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy,
the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's
go!". After that, all the dwarves from the chosen lines start tosimultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current
cell. The movement continues until one of the following events occurs:

  • some dwarf in one of the chosen lines is located in the rightmost cell of his row;
  • some dwarf in the chosen lines is located in the cell with the candy.

The point of the game is to transport all the dwarves to the candy cells.

Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).

Next n lines each contain m characters — the game
field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G"
represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character
"G" and one character "S".

Output

In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the
given game field.

Sample test(s)
input
3 4
*G*S
G**S
*G*S
output
2
input
1 3
S*G
output
-1
大意:

给n*m的方格,每行中都且仅有一个方格含S和G,每一步能够使全部的G向右移,仅仅到发生下列情况:1、某个G已到了最后一列。2、某个G已到了S。求把全部的G已到S须要的最少的步数。

解题思路:

统计不同的S-G差出现的个数。


#include<iostream>
#include<cstring>
#include<cstdio>
#define M 1005
using namespace std;
int main()
{
char map[M][M];
int n,m,i,j,a[M],e,s;
while(cin>>n>>m)
{
int a[M];
memset(a,0,sizeof(a));
int ans=0,k=0;
for(i=0;i<n;i++)
cin>>map[i];
/* for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
cout<<map[i][j];
cout<<endl;
}*/
for(i=0;i<n;i++)
{
e=-1;
s=0;
for(j=0;j<m;j++)
{
if(map[i][j]=='G')
{
s=j;
// cout<<i<<" G "<<s<<endl;
}
if(map[i][j]=='S')
{
e=j;
// cout<<i<<" S "<<e<<endl;
} }
if(s<e)
{
a[i]=e-s;
}
else
{
k=1;
break;
}
} if(k)
cout<<"-1"<<endl;
else
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(!a[i])
continue;
if(a[i]==a[j])
a[j]=0; }
for(i=0;i<n;i++)
if(a[i]!=0)
ans++;
cout<<ans<<endl;
}
} return 0;
}

Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies的更多相关文章

  1. Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies SET的妙用

    B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...

  2. Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. Codeforces Round #234 (Div. 2) A. Inna and Choose Options

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  4. Codeforces Round #234 (Div. 2):B. Inna and New Matrix of Candies

    B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...

  5. Codeforces Round #234 (Div. 2)

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. Codeforces Round #234 (Div. 2) :A. Inna and Choose Options

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  8. Codeforces Round #220 (Div. 2) D - Inna and Sequence

    D - Inna and Sequence 线段数维护区间有几个没有被删除的数,利用线段树的二分找第几个数在哪里,然后模拟更新就好啦. #include<bits/stdc++.h> #d ...

  9. Codeforces Round #223 (Div. 2) A

    A. Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. jQuery 弹出窗口的形式一直是具体案件的中心

    在网上查 多 不是不符合无效;因此,一些自己总结,解决这个问题   原则: 常见问题: 弹出层居中了,背景也是半透明的  可是发现一拉动滚动栏立即就露馅了发现背景仅仅设置了屏幕所在段,其它部分都是原来 ...

  2. SWFUpload简单使用样例 Java版(JSP)

    SWFUpload官方的样例都是PHP的,在这里提供一个Java版的最简单的使用样例,使用JSP页面完毕全部操作. 实现上传,分为三步: 1.JavaScript设置SWFUpload部分(与官方样例 ...

  3. 2014鞍山现场赛C题HDU5072(素筛+容斥原理)

    Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  4. H3C TE老版本OSPF正确配置

    R1配置: ---------------------------------------------------- # sysname RT1# super password level 3 cip ...

  5. 项目之软件project(我专业四年都未曾知道这四个字的含义,几句话便懂了)

    潘鹏在CSDN上原创.如其它站点转载请注意排版和写明出处: 软件project的本质 一级标题 控制 质量 二级标题  成本  扩展  高内聚低耦合 效率 控制: 成本:企业要求的是以最快的速度完毕可 ...

  6. Java EE (12) -- 系统质量的分类

    明显的 性能(Performance): 对响应用户的应答时间的度量.可靠性(Reliability): 对包括后台存储和给用户的表示结果在内的数据正确的可能性的度量.可用性(Availability ...

  7. Android permission 访问权限

    程序执行需要读取到安全敏感项必需在androidmanifest.xml中声明相关权限请求, 完整列表如下: android.permission.ACCESS_CHECKIN_PROPERTIES ...

  8. Spark1.0.0 属性配置

    1:Spark1.0.0属性配置方式       Spark属性提供了大部分应用程序的控制项,而且能够单独为每一个应用程序进行配置.       在Spark1.0.0提供了3种方式的属性配置: Sp ...

  9. C++ Primer 学习笔记_44_STL实践与分析(18)--再谈迭代器【下】

    STL实践与分析 --再谈迭代器[下] 三.反向迭代器[续:习题] //P355 习题11.19 int main() { vector<int> iVec; for (vector< ...

  10. Spring jdbc 对象Mapper的简单封装

    一般查询实体的时候,都需要这么使用/**      * 根据id查询      *       * @return      */     public Emp queryEmpById(Intege ...