B. Inna and New Matrix of Candies

2024-06-09 04:58
文章标签 matrix candies new inna

本文主要是介绍B. 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 can 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向右移,直到某个G已到了最后一列或某个G已到了S。求所有G到S需要的最少的步数。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include<Set>
#include <cstdio>
#include <algorithm>
using namespace std;
char s[1005][1005];
set<int>Set;
int main()
{int n,m;int a,b;while(cin>>n>>m){int flag=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>s[i][j];if(s[i][j]=='G')a=j;else if(s[i][j]=='S')b=j;}if(b-a<0)flag=1;elseSet.insert(b-a-1);}if(flag==0)cout<<Set.size()<<endl;elsecout<<"-1"<<endl;}return 0;
}



这篇关于B. Inna and New Matrix of Candies的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1044272

相关文章

1_Image和Matrix的使用

参考博文: https://www.cnblogs.com/bomo/archive/2013/03/28/2986573.html

github 报错 git fatal: unable to write new index file

错误一:git fatal: unable to write new index file主要原因就是服务器磁盘空间不够导致的,增加服务器空间就OK了在百度上面搜索没得到什么有效信息,在gooogle上搜索得到很多有效信息 Finding large directories with something like the following helped clean up some log fi

Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接

在进行参数化读取时发现一个问题: 发现问题: requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8081): Max retries exceeded with url: /jwshoplogin/user/update_information.do (Caused by NewConn

c.toString() 和 String s = new String(c) 区别

String str = "abcd";char [] c = str.toCharArray();String s = new String(c);String s2 = c.toString();其中s和s2有什么区别???String str = "abcd";char [] c = str.toCharArray();String s = new String(c); //

获取时间戳是使用System.currentTimeMillis()还是使用new Date().getTime()(阿里开发规范)?

1.阿里规范 在阿里的Java开发手册中强制要求使用System.currentTimeMillis() 2.为什么(源码详解) new Date().getTime()它实际上也是调用的System.currentTimeMillis(),源码分析。 这个fastTime是它的成员变量,在new Date()的时候就被赋值了。 扩展一下这个transient这个关键字,它是为了保护

不需要new关键字创建实例?jQuery是如何做到的

这篇文章是jQuery源码专栏的开篇文章了,有人会问为什么都2024年了, 还要研究一个已经过时的框架呢,其实,jQuery对比vue和react这种响应式框架,其在使用上算是过时的,毕竟直接操作DOM远不如操作虚拟DOM来的方便,但是jQuery的框架设计和对于操作的封装以及浏览器的兼容这些,太值得我们去学习了。   这个专栏更新的速度不会快,这框架代码我是刚开始进行了解,所以只能边看边查

Xamarin.ios 解决new NSUrl 返回为空的方法。

var uri = new Uri (urlString);var nsurl = new NSUrl (uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped));UIApplication.SharedApplication.OpenUrl (nsurl);

SP2010开发和VS2010专家食谱--第六章节--Web Services和REST(5)--Inserting new contacts through REST

我们现在知道了我们可以使用REST请求从SharePoint列表获得数据,如何从客户端应用程序添加数据到列表呢?本文中,我们将探讨如何做到。

new BigDecimal时,请使用字符串

一、构造BigDecimal BigDecimal提供了丰富的构造函数,可以通过int、long、double、String等来构造一个BigDecimal对象。 但是,使用double作为参数的构造函数,无法精确构造一个BigDecimal对象,需要自己指定一个上下文的环境,也就是指定精确位。 例如: BigDecimal bg = new BigDecimal(1.1);System.

vue3 运用高德地图 自定义弹框 为信息窗体 添加 new AMaps.value.InfoWindow 添加事件

效果图 划过散点的时候出现每个三点位置的数据提示 点击具体散点获取展示信息弹框,并为其添加点击事件 注意点: 1 即使是用的vue,也不能使用@click为窗体添加点击事件,需要使用onclick, (原因:这是因为你的弹窗内容是以字符串的形式插入到 DOM 中的。此时你给它添加的点击事件不会被 Vue 的事件监听系统所识别和处理,因为这是在 Vue 的作用范围之外的。但是,如果你把这个函数