Codeforces Round #234 (Div. 2):B. Inna and New Matrix of Candies_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 12:01:50
原创
1317人浏览过

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

    Sample test(s)

    Input

    3 4*G*SG**S*G*S
    登录后复制

    Output

    Input

    1 3S*G
    登录后复制

    Output

    -1开始题意理解错了,以为只要一行有个G到了S就停止,记录经过多少个S。。。额。。英语渣真心无脑脑补。。2333333。。。<pre class="n"><p></p>
                        <div class="aritcle_card">
                            <a class="aritcle_card_img" href="/xiazai/js/5998">
                                <img src="https://img.php.cn/upload/jscode/000/120/096/5e1d600e63286830.jpg" alt="仿B站视频帧预览插件">
                            </a>
                            <div class="aritcle_card_info">
                                <a href="/xiazai/js/5998">仿B站视频帧预览插件</a>
                                <p>仿B站视频帧预览插件</p>
                                <div class="">
                                    <img src="/static/images/card_xiazai.png" alt="仿B站视频帧预览插件">
                                    <span>49</span>
                                </div>
                            </div>
                            <a href="/xiazai/js/5998" class="aritcle_card_btn">
                                <span>查看详情</span>
                                <img src="/static/images/cardxiayige-3.png" alt="仿B站视频帧预览插件">
                            </a>
                        </div>
                    <p>题目意思:给n*m的方格,每行中都且仅有一个方格含S和G,每一步可以使所有的G向右移,只到发生下列情况:</p><p>1、某个G已到了最后一列。</p><p>2、某个G已到了S。求把所有的G已到S需要的最少的步数。 </p><p>解题思路:</p><p>统计S-G差出现的个数。</p><p>因为两个差相等的话,他们会一起到达S。也就不要算额外的步数。</p>
    登录后复制



    #include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<algorithm>#include<vector>using namespace std;const int N = 1020;int n , m;int a, b;int s[N];char str[N][N];int flag;int ans;int main(){    while( scanf("%d%d", &n, &m)!=EOF )    {        int flag = 0;        memset( s, 0, sizeof(s) );        ans = 0;        for(int i=0; i<n; i++)        {            scanf("%s", str[i]);            for(int j=0; j<m; j++)            {                if( str[i][j]=='G' )                    a = j;                if(  str[i][j]=='S' )                    b = j;            }            if( a<b )                s[ b-a ]++;            else                flag=1;        }        for( int i=0; i<m; i++ )        {            if( s[i] )                ans++;        }        if( flag )            printf("-1\n");        else            printf("%d\n", ans);    }    return 0;}
    登录后复制






    HTML速学教程(入门课程)
    HTML速学教程(入门课程)

    HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号