codeforces 400B Inna and New Matrix of Candies (英语水平题)

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 to simultaneously 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.

Example
Input
3 4
*G*S
G**S
*G*S
Output
2
Input
1 3
S*G
Output
-1

 【题解】 这真是一道英语题,完全考察英语水平,,,,,

  题意很简单 ,就是一个M*N的矩阵,矩阵每行都有一个雕像和一个糖果,现在你来玩这个游戏,每喊一次,所有的雕像全部向右移动,停止移动的条件是遇到糖果或者有一个雕像到达最右端边界,这算一次移动!!!!!

 注意坑点:

 只要有一行达不到条件(拿不到糖果)就输出-1比如  *S*G  这种;

 要所有雕像都到达左右边  (因为这个我WA了好几次);

【AC代码】

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1005;
int m,n;
char str[N][N];
int a[N],b[N];

int min(int x,int y)
{
    return x>y?y:x;
}

int main()
{
    while(~scanf("%d%d%*c",&m,&n))
    {
        int flag=0;
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        for(int i=0;i<m;i++)
            scanf("%s",str[i]);
        int left,right;
        int mins=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(str[i][j]=='G')
                    left=j;
                if(str[i][j]=='S')
                    right=j;
            }
            if(right>left)
            {
                b[right-left]++;//每个距离都标记一下
            }
            else
                flag=1;
        }
        if(flag==1)//有一种情况不满足就直接输出-1
            printf("-1\n");
        else
        {
           for(int i=1;i<n;i++)//遍历所有的距离,不同距离计数一次
           {
              if(b[i])
                 mins++;
           }
           printf("%d\n",mins);
        }
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值