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. NET实现的DDD、CQRS与微服务架构

    WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间 ...

  2. 浅谈OCR之Onenote 2010

    原文:浅谈OCR之Onenote 2010 上一次我们讨论了Tesseract OCR引擎的用法,作为一款老牌的OCR引擎,目前已经开源,最新版本3.0中更是加入了中文OCR功能,再加上Google的 ...

  3. winXP JDK由1.8改为1.6

    (1)直接在环境变量中删除配置的相关路径 path的值: C:\Documents and Settings\Administrator>path PATH=C:\Documents and S ...

  4. 在word 中复选框划勾或叉的方法

    输入大写字母R.大写字母Q ,然后将字体改为Wingdings 2, 就分离得到带框的勾和叉.

  5. Spark SQL 初步

    已经Spark Submit 2013哪里有介绍Spark SQL.就在很多人都介绍Catalyst查询优化框架.经过一年的发展后,.今年Spark Submit 2014在.Databricks放弃 ...

  6. SVN的revert和update命令的区别

    svn中的revert和update 今天有人问到revert和update的问题. 刚开始还真被问住了. 因为感觉revert和update都可以将本地的copy更新到以前的一个版本,会有什么不同呢 ...

  7. 重新启动IIS服务的方法

    WINDOWS提供WEB服务的IIS有时候会出现訪问过大导致站点打不开,这时重新启动IIS是最好的选择. 1.界面操作 打开"控制面板"->"管理工具"- ...

  8. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  9. 数学思想方法-分布式计算-linux/unix技术基础(5)

    shell命令行参数 -bash-4.2$ cat test1.sh#!/bin/shecho "$0  "echo "$1  "echo "$2   ...

  10. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...