B. Inna and New Matrix of Candies
2014-03-08 20:39
393 查看
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需要的最少的步数。
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; else Set.insert(b-a-1); } if(flag==0) cout<<Set.size()<<endl; else cout<<"-1"<<endl; } return 0; }
相关文章推荐
- CF 400B - Inna and New Matrix of Candies
- (CF)B. Inna and New Matrix of Candies
- 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的妙用
- codeforces#234_div2_B Inna and New Matrix of Candies
- B. Inna and New Matrix of Candies
- codeforces#234_div2_B Inna and New Matrix of Candies
- Codeforces 400B Inna and New Matrix of Candies 【模拟】
- Codeforces Round #234 (Div. 2):B. Inna and New Matrix of Candies
- Codeforces 400 B. Inna and New Matrix of Candies 【 Codeforces Round #234 (Div. 2)】
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies
- Inna and New Matrix of Candies(CodeForces - 400B)
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
- B - Inna and New Matrix of Candies
- codeforces 400B - Inna and New Matrix of Candies
- warning: format ‘%s’ expects argument of type ‘char *’和warning: backslash and newline separated by
- A brief preview of the new features introduced by OpenGL 3.3 and 4.0
- Image Retrieval: Ideas, influences, and trends of the new age 图像检索综述 文献翻译(一)
- 每日英语:China's New Anti-Graft Website: A Tale of Tigers, Flies and Bath Tubs