Codeforces 400B. Inna and New Matrix of Candies

Source: Internet
Author: User
Tags printf time limit

1, Http://codeforces.com/problemset/problem/400/B

2. The main idea of the topic:

There is a n*m matrix, where * represents that the lattice is empty, and g represents that the lattice has a little dwarf, s representing the lattice has a candy

Every time the dwarf has not reached the candy grid row, the selected lines of the Dwarves will go to the right, if there is one row in either case, all the dwarves will stop to the right, the two cases are: the dwarf met the candy to stop the movement, the dwarf went to the far right to stop the movement.

How many times do I have to execute at least to get all the dwarfs to get to the candy grid if it doesn't finish output-1

3. Title: B. Inna and New Matrix of candies time limit per test 1 second memory limit per test megabytes input standard in Put output 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 NXM. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line is EMP Ty. The game lasts for several moves. During each move the player can choose all lines of the matrix where dwarf are not on the cell with candy and shout "let ' s Go! ". After this, the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell, which is a located to the right of their current cell. The movement continues until one of the following events Occurs:some Dwarf in one of the chosen lines are located in the R Ightmost cell of his row; Some dwarf in the chosen lines are located in the cell with the candy.

The point of the game was to transport all the dwarves to the candy cells.

Inna is fabulous, as she came up with such an interesting game. But what's about you? Your task is to play this game optimally well. Specifically, should say by the given game field what minimum number of moves the player needs to reach the goal of th E game. Input

The first line of the input contains the 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 so 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 is 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

4. AC Code:

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std;
#define N 1005 int map[n][n];
int b[n];
    int cmp (int a,int b) {return a<b;} int main () {int n,m;
        while (scanf ("%d%d", &n,&m)!=eof) {//getchar ();
        int flag=0;
            for (int i=1; i<=n; i++) {int p=0,q=0;
            GetChar ();
                for (int j=1; j<=m; J + +) {scanf ("%c", &map[i][j]);
                if (map[i][j]== ' G ') {p=j;
                } else if (map[i][j]== ' S ') {q=j;
            }} if (P>q) flag=1;
        B[i]=q-p;
        } if (flag==1) printf (" -1\n");
            else {int sum=0;
            Sort (b+1,b+n+1,cmp);
               for (int i=1; i<=n; i++) {if (b[i]!=0) {int tmp=b[i];
                    for (int j=i; j<=n; J + +) {b[j]-=tmp;
                } sum++;
        }} printf ("%d\n", sum);
}} return 0;
 }


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.