Codeforces Round #234 (Div. 2) 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 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; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步