Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies
1 second
256 megabytes
standard input
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.
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".
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.
3 4
*G*S
G**S
*G*S
2
1 3
S*G
-1
大意: 给n*m的方格,每行中都且仅有一个方格含S和G,每一步能够使全部的G向右移,仅仅到发生下列情况:1、某个G已到了最后一列。2、某个G已到了S。求把全部的G已到S须要的最少的步数。 解题思路: 统计不同的S-G差出现的个数。#include<iostream>
#include<cstring>
#include<cstdio>
#define M 1005
using namespace std;
int main()
{
char map[M][M];
int n,m,i,j,a[M],e,s;
while(cin>>n>>m)
{
int a[M];
memset(a,0,sizeof(a));
int ans=0,k=0;
for(i=0;i<n;i++)
cin>>map[i];
/* for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
cout<<map[i][j];
cout<<endl;
}*/
for(i=0;i<n;i++)
{
e=-1;
s=0;
for(j=0;j<m;j++)
{
if(map[i][j]=='G')
{
s=j;
// cout<<i<<" G "<<s<<endl;
}
if(map[i][j]=='S')
{
e=j;
// cout<<i<<" S "<<e<<endl;
} }
if(s<e)
{
a[i]=e-s;
}
else
{
k=1;
break;
}
} if(k)
cout<<"-1"<<endl;
else
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(!a[i])
continue;
if(a[i]==a[j])
a[j]=0; }
for(i=0;i<n;i++)
if(a[i]!=0)
ans++;
cout<<ans<<endl;
}
} return 0;
}
Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #234 (Div. 2)
A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...
- 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 ...
- 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 ...
- Codeforces Round #220 (Div. 2) D - Inna and Sequence
D - Inna and Sequence 线段数维护区间有几个没有被删除的数,利用线段树的二分找第几个数在哪里,然后模拟更新就好啦. #include<bits/stdc++.h> #d ...
- 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 ...
随机推荐
- Android中的动画具体解释系列【4】——Activity之间切换动画
前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...
- 通用型CRM还是行业型CRM?-定制为王
大数据时代,怎样利用工具摆脱繁杂的数据管理之苦,洞察有价值的销售信息,是每一个管理者的迫切须要.Zoho CRM问世10年来,见证了一个个行业客户怎样在CRM帮助下实现了效率和业绩提升.相同,广泛的 ...
- 阿里巴巴2015研究project普通笔试题,与答案
欢迎您对这篇文章的其他建议.我可以留言在以下平台. 个人博客网站:www.anycodex.com/blog/ Csdn博客网站:http://my.csdn.net/?ref=toolbar 微博: ...
- 番外:android模拟器连不上网
1.删除你PC端得备用DNS,只留一个即可.确保能够上网. 注意:这个虽然不是必须的,出错点也不一定在他,但是我建议这样做,因为我们不确定到底模拟器和我们的PC是否使用的是一个DNS,不是的话,就会造 ...
- Java PreparedStatement
PreparedStatement是一个用于运行sql语句的标准接口的对象.它是继承与Statement.依据里氏代换原则.用Statement运行的语句,一定能够用Prepared替换了.那么他们之 ...
- jquery关于表格隐藏和显示问题
1. 关于指定表格指定列隐藏显示 $(":checkbox[name*=month]").each(function(){ if(!$(this).attr("check ...
- Harris角点检测算原理
主要参考了:http://blog.csdn.net/yudingjun0611/article/details/7991601 Harris角点检测算子 本文将该文拷贝了过来,并做了一些数学方面的 ...
- Python倒计时器(转)
# Countdown using Tkinter from Tkinter import * import time import tkMessageBox class App: def __ini ...
- hdu1011(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs& ...
- prepareCall()运行存储过程
CallableStatement 对象为全部的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中.对已储存过程的调用是 CallableStatement对象所含的内容. ...