B. Inna and New Matrix of Candies
来源:互联网 发布:mac os x 懒人版 编辑:程序博客网 时间:2024/05/16 18:58
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.
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*SG**S*G*S
2
1 3S*G
-1
解题说明:此题的意思是通过向右移动G直到G与S重合或者到达最右端为止。这里可以同时移动多行,最终实现每一行的G都移动到指定位置。做法是首先判断S是否在G的左侧,如果在左侧肯定无法满足条件,否则就记录下G和S的位置差,相同的可以进行合并,用一个数组进行标记即可。
#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include <algorithm>#include<cstring>using namespace std;int d[1001];int main(){int n, m;int i, j, f = 0;int a = 0, b = 0;scanf("%d%d", &n, &m);memset(d, 0, sizeof(d));getchar();for (i = 1; i <= n; i++){a = 0;b = 0;for (j = 1; j <= m; j++){char c;scanf("%c", &c);if (c == 'G'){a = j;}if (c == 'S'){if (a == 0){f = 1;break;}else{d[j - a] = 1;}}}getchar();if (f){break;}}int k = 0;if (f){printf("-1\n");}else{for (i = 1; i <= 1000; i++){if (d[i]){k++;}}printf("%d\n", k);}return 0;}
- B. Inna and New Matrix of Candies
- B. Inna and New Matrix of Candies
- B - Inna and New Matrix of Candies
- code forces 400B Inna and New Matrix of Candies
- codeforces 400B. Inna and New Matrix of Candies
- (CF)B. Inna and New Matrix of Candies
- codeforces 400B - Inna and New Matrix of Candies
- CF 400B - Inna and New Matrix of Candies
- codeforces 400B Inna and New Matrix of Candies
- Inna and New Matrix of Candies(CodeForces - 400B)
- Codeforces 400B Inna and New Matrix of Candies 【模拟】
- codeforces 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 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 400B Inna and New Matrix of Candies (英语水平题)
- codeforces#234_div2_B Inna and New Matrix of Candies
- 变量的存储类型
- 关于HTML5令人激动的3项预测
- PostgreSQL 9.3:在windows(包括win7)下的安装与正确配置
- VisualNet可视化的管网资源管理系统
- TeraSort实验--测试Map和Reduce Task数量对Hadoop性能的影响
- B. Inna and New Matrix of Candies
- malloc/free与new/delete的相同点和不同点
- jsp 引入 jstl标签库报 can not find the liblary 问题
- 近期算法和数据结构面试题解答汇总(更新)
- jQuery ajax - serialize() 表单传值
- window.showModalDialog();会弹出当前页面脚本发生错误部分解决方法
- R 语言文件操作
- JSON 转换成Java对象的方法
- Thinking in Java 泛型