Codeforces 400 B. Inna and New Matrix of Candies 【 Codeforces Round #234 (Div. 2)】

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的矩阵,其中*代表该格是空的,G代表该格有一个小矮人,S代表该格有一个糖果
每次都要选中所有有小矮人还没到达糖果格的那行,选中的这些行的小矮人都要向右走,如果有一行碰到这两种情况之一,所有行的小矮人都要停止向右运动,两种情况是:小矮人遇到糖果就停止运动,小矮人走到最右边了也停止运动。
求最少执行多少次才能让所有的小矮人都能到达糖果格子,如果不能完成输出-1

解题思路:
我们只需要找到从 G 到 S 的最大路线就行了,所以我们就用一个ok【】数组保存一下他们之间的长度将其变为true,
我们只需要数一下 true的个数就行了。。。如果G的坐标 大于 S的坐标就用一个标记变量将其变为true,然后判断一下就行了。。。

具体的在代码中显示:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e3+5;
const int mod = 1000000007;
const double eps = 1e0-7;
char s[maxn][maxn];
bool ok[maxn];
int main()
{
    int m, n, y1, y2;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=0; i<m; i++)
            scanf("%s",s[i]);
        memset(ok, false, sizeof(ok));
        bool yes = false;
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(s[i][j] == 'G')
                    y1 = j;
                if(s[i][j] == 'S')
                    y2 = j;
            }
            if(y1 < y2)
                ok[y2-y1] = true;
            if(y2 < y1)
            {
                ///cout<<666;
                yes = true;
                break;
            }
        }
        if(yes)
            puts("-1");
        else
        {
            int ret = 0;
            for(int i=0; i<maxn; i++)
                if(ok[i])
                    ret++;
            cout<<ret<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值