B - Inna and New Matrix of Candies
2015-10-31 15:12
411 查看
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97343#problem/B
B - Inna and New Matrix of Candies
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
400B
Description
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.
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 Input
Input
Output
Input
Output
G同时向右跑,存在G遇到S时算做一波,当所有G遇到S后停止,计算一共有多少波,如果G在S右边输出-1;;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
char str[1005][1005];
int ans[1005];
int main()
{
int n,m,flag,sout,t,x,y;
while(cin>>n>>m)
{
flag=0;
for(int i=0; i<n; i++)
{
x=0,y=0;
cin>>str[i];
for(int j=0; j<m; j++)
{
if(str[i][j]=='G')
{
x=j;
}
else if(str[i][j]=='S')
{
y=j;
}
}
if(x>y)
flag=1;
ans[i]=y-x;
}
if(flag==1)
cout<<"-1"<<endl;
else
{
sout=0;
sort(ans,ans+n);
cout<<ans[0]<<endl;
for(int i=0; i<n; i++)
{
if(ans[i]!=0)
{
int t=ans[i];
for(int j=i; j<n; j++)
{
ans[j]-=t;
}
sout++;
}
}
cout<<sout<<endl;
}
}
return 0;
}
B - Inna and New Matrix of Candies
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
400B
Description
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.
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 Input
Input
3 4 *G*S G**S *G*S
Output
2
Input
1 3 S*G
Output
-1
G同时向右跑,存在G遇到S时算做一波,当所有G遇到S后停止,计算一共有多少波,如果G在S右边输出-1;;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
char str[1005][1005];
int ans[1005];
int main()
{
int n,m,flag,sout,t,x,y;
while(cin>>n>>m)
{
flag=0;
for(int i=0; i<n; i++)
{
x=0,y=0;
cin>>str[i];
for(int j=0; j<m; j++)
{
if(str[i][j]=='G')
{
x=j;
}
else if(str[i][j]=='S')
{
y=j;
}
}
if(x>y)
flag=1;
ans[i]=y-x;
}
if(flag==1)
cout<<"-1"<<endl;
else
{
sout=0;
sort(ans,ans+n);
cout<<ans[0]<<endl;
for(int i=0; i<n; i++)
{
if(ans[i]!=0)
{
int t=ans[i];
for(int j=i; j<n; j++)
{
ans[j]-=t;
}
sout++;
}
}
cout<<sout<<endl;
}
}
return 0;
}
相关文章推荐
- cf 591C Median Smoothing
- 感知哈希算法
- 多个if与多个else if详解
- 从零开始创建属于自己的网站(转)
- selenium2断言类Assert的使用
- C#操作SQL Server通用类
- HTML语言基础
- POJ 百炼 保研机试 2723:不吉利日期
- 问题(bug)确实不在代码逻辑上面,往往是配置、权限或者业务逻辑之外的地方(转)
- 《软考》——汇编,编译,解释
- 电阻色环表_色环电阻识别方法
- SpringMVC 全局异常处理代码
- Shell脚本经典之Fork炸弹
- div+css创建导航栏
- final关键字的使用
- javascript制作一个用户登录表单提交检验网页(第一天)
- codeforces-580C-Kefa and Park【dfs】
- python 根据类的字符串名实例化对象
- 多项式加法
- 实现软件架构质量属性的战术