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
  1. 3 4
  2. *G*S
  3. G**S
  4. *G*S
output
  1. 2
input
  1. 1 3
  2. S*G
output
  1. -1
  1. 大意:
  2. n*m的方格,每行中都且仅有一个方格含SG,每一步能够使全部的G向右移,仅仅到发生下列情况:1、某个G已到了最后一列。2、某个G已到了S。求把全部的G已到S须要的最少的步数。
  3. 解题思路:
  4. 统计不同的S-G差出现的个数。
  5. #include<iostream>
  6. #include<cstring>
  7. #include<cstdio>
  8. #define M 1005
  9. using namespace std;
  10. int main()
  11. {
  12.     char map[M][M];
  13.     int n,m,i,j,a[M],e,s;
  14.     while(cin>>n>>m)
  15.     {
  16.         int a[M];
  17.         memset(a,0,sizeof(a));
  18.       int ans=0,k=0;
  19.         for(i=0;i<n;i++)
  20.             cin>>map[i];
  21.     /* for(i=0;i<n;i++)
  22.        {         for(j=0;j<m;j++)
  23.                 cout<<map[i][j];
  24.                 cout<<endl;
  25.       }*/
  26.         for(i=0;i<n;i++)
  27.         {
  28.             e=-1;
  29.             s=0;
  30.             for(j=0;j<m;j++)
  31.            {
  32.              if(map[i][j]=='G')
  33.                  {
  34.                      s=j;
  35.                 // cout<<i<<" G "<<s<<endl;
  36.                  }
  37.               if(map[i][j]=='S')
  38.                   {
  39.                       e=j;
  40.                      // cout<<i<<" S "<<e<<endl;
  41.                 }
  42.            }
  43.            if(s<e)
  44.                   {
  45.                       a[i]=e-s;
  46.                   }
  47.               else
  48.                {
  49.                  k=1;
  50.                  break;
  51.                }
  52.         }
  53.         if(k)
  54.         cout<<"-1"<<endl;
  55.         else
  56.       {
  57.           for(i=0;i<n;i++)
  58.             for(j=i+1;j<n;j++)
  59.            {
  60.                if(!a[i])
  61.                continue;
  62.             if(a[i]==a[j])
  63.             a[j]=0;
  64.             }
  65.         for(i=0;i<n;i++)
  66.             if(a[i]!=0)
  67.             ans++;
  68.         cout<<ans<<endl;
  69.       }
  70.     }
  71.     return 0;
  72. }
  73.  

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. HDU 4643 GSM 简单计算几何

    今天比赛的时候略坑, admin告诉我询问Q的个数不超过n^2, 赛后敲了个 O(Q*m^3)的复杂度,但这个复杂度常数比较低,可能在除以个小常数, 300ms过了,真心无语,数据应该水了吧,比赛的时 ...

  2. CSS实现输入框的高亮效果-------Day50

    又到周末了,这一天天过的真快,明天应该回老家了.不知道会不会有机会进行编写.尽量争取吧,实在不想就这样间断.假设说从前会一天天无聊到爆,那如今自己应该是一天天忙的要死,欠缺了太多东西,那些浪费的时间可 ...

  3. 自顶向下分析Binder【1】—— Binder实例篇

    欢迎转载,转载请注明:http://blog.csdn.net/zhgxhuaa 一个Binder实例 我们Binder的学习将从以下的一个实例開始.依据Android文档中的描写叙述,创建一个Bin ...

  4. 基于CORS的geoserver同源访问策略

    这个问题理顺整个2天.终于攻克.记录下来. 1.下载文件 首先下载cors压缩包,解压,得到的是org/mortbay/servlets/CrossOriginFilter.class文件,把此文件拷 ...

  5. HTML表格标签的使用-&lt;table&gt;

    <html> <head> <title> 表格标签 </title> <!-- 标签名:table 定义一个表格 子标签:<caption ...

  6. orcl 删除重复的行

    delete from FOODDETAIL t where t.id in (select   t.id from FOODDETAIL where t.sendtime>=to_date(' ...

  7. Eclipse 快捷键整理

    Alt+/:代码提示Ctrl+/:注释/取消注释Ctrl+D:删除光标所在行Ctrl+K:将光标停留在变量上,按Ctrl+K键可以查找到下一个同样的变量Shift+Ctrl+K:和Ctrl+K查找的方 ...

  8. WPF命中测试示例(二)——几何区域命中测试

    原文:WPF命中测试示例(二)--几何区域命中测试 接续上次的命中测试,这次来做几何区域测试示例. 示例 首先新建一个WPF项目,在主界面中拖入一个按钮控件,并修改代码中的以下高亮位置: 当前设计视图 ...

  9. oracle看到用户的所有表名、表睐、字段名称、现场的目光、是空的、字段类型

    --oracle看到用户的所有表名.表睐.字段名称.现场的目光.是空的.字段类型 select distinct TABLE_COLUMN.*, TABLE_NALLABLE.DATA_TYPE, T ...

  10. php函数serialize()与unserialize() 数据序列化与反序列化

    php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可处理除了resource之外 ...