ECJTU 2018 Summer Training 2

Thanks a lot for helping Harry Potter in finding the Sorcerer‘s Stone of Immortality in October. Did we not tell you that it was just an online game ? uhhh! now here is the real onsite task for Harry. You are given a magrid S ( a magic grid ) having R rows and C columns. Each cell in this magrid has either a Hungarian horntail dragon that our intrepid hero has to defeat, or a flask of magic potion that his teacher Snape has left for him. A dragon at a cell (i,j) takes away |S[i][j]| strength points from him, and a potion at a cell (i,j) increases Harry‘s strength by S[i][j]. If his strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.

Harry starts from the top-left corner cell (1,1) and the Sorcerer‘s Stone is in the bottom-right corner cell (R,C). From a cell (i,j), Harry can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and he can not move outside the magrid. Harry has used magic before starting his journey to determine which cell contains what, but lacks the basic simple mathematical skill to determine what minimum strength he needs to start with to collect the Sorcerer‘s Stone. Please help him once again.

Input (STDIN):

The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers. Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with S[i][j] < 0 contain dragons, others contain magic potions.

Output (STDOUT):

Output T lines, one for each case containing the minimum strength Harry should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).

Constraints:

1 ≤ T ≤ 5

2 ≤ R, C ≤ 500

-10^3 ≤ S[i][j] ≤ 10^3

S[1][1] = S[R][C] = 0

Sample Input:

3
2 3
0 1 -3
1 -2 0
2 2
0 1
2 0
3 4
0 -2 -3 1
-1 4 0 -2
1 -2 -3 0

Sample Output:

2
1
2

要用dp来做,dfs会超时。同时不能从起点计算,要从终点往回推。因为如果用正向来做的话,不仅仅要保留该点的当前分数值,还要保留起点到该点所需的分数值,这样就控制不了了,但是逆向思维,从终点往回退,只需要保留该点到终点所需要的分值,而不需要考虑该点还剩多少分值。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
#define INF 1e9+7
int a[510][510];
int dp[510][510];//dp[i][j]表示从该点到终点所需要的最少分数
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                scanf("%d",&a[i][j]);
        }
        dp[n-1][m-1]=1;//每个点最小是1,初始化终点是1
        for(int i=n-1;i>=0;i--)//逆向思维。。
        {
            for(int j=m-1;j>=0;j--)
            {
                if(i!=n-1&&j==m-1)
                {
                    dp[i][j]=max(1,dp[i+1][j]-a[i][j]);
                }
                else if(i==n-1&&j!=m-1)
                {
                    dp[i][j]=max(1,dp[i][j+1]-a[i][j]);
                }
                else if(i!=n-1&&j!=m-1)
                {
                    dp[i][j]=max(1,min(dp[i+1][j]-a[i][j],dp[i][j+1]-a[i][j]));
                }
            }
        }
        printf("%d\n",dp[0][0]);
    }
    return 0;
}

      

The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn‘s History of Magic lesson to be no less boring than you found your own history classes.  Recently Binns has been droning on about Goblin wars, and which goblin civilization fought which group of centaurs where etc etc.  The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams.  Can you help them?

civilization fought which group of centaurs where etc etc.  The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams.  Can you help them?

The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a ‘#‘ in the grid. All the other cells - to which the civilizations can move  - are represented by a ‘.‘ in the grid.

A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid.   Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a ‘*‘. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.

Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.

Input (STDIN):

The first line contains T, the number of cases. This is followed by T test case blocks.

Each test case contains two integers, R, C.

This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.

Each cell is either a

1. ‘.‘ which represents an unoccupied cell

2. ‘#‘ which represents a cell that cannot be occupied

3. A civilization represented by a lowercase letter (‘a‘ - ‘z‘)

Output (STDOUT):

For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use ‘*‘ to denote that a battle is being waged in that particular cell.

Print a blank line at the end of each case.

Constraints:

1 <= R, C <= 500

1 <= T <= 5

Time Limit:  3 s

Memory Limit: 64 MB

Sample Input:

5

3 5

#####

a...b

#####

3 4

####

a..b

####

3 3

#c#

a.b

#d#

3 3

#c#

...

a.b

3 5

.....

.#.#.

a...b

Sample Output:

#####

aa*bb

#####

####

aabb

####

#c#

a*b

#d#

#c#

acb

a*b

aa*bb

a#.#

aa*bb

The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a ‘#‘ in the grid. All the other cells - to which the civilizations can move  - are represented by a ‘.‘ in the grid.

A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid.   Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a ‘*‘. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.

Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.

Input (STDIN):

The first line contains T, the number of cases. This is followed by T test case blocks.

Each test case contains two integers, R, C.

This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.

Each cell is either a

1. ‘.‘ which represents an unoccupied cell

2. ‘#‘ which represents a cell that cannot be occupied

3. A civilization represented by a lowercase letter (‘a‘ - ‘z‘)

Output (STDOUT):

For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use ‘*‘ to denote that a battle is being waged in that particular cell.

Print a blank line at the end of each case.

Constraints:

1 <= R, C <= 500

1 <= T <= 5

Sample Input:

5

3 5

#####

a...b

#####

3 4

####

a..b

####

3 3

#c#

a.b

#d#

3 3

#c#

...

a.b

3 5

.....

.#.#.

a...b

Sample Output:

#####

aa*bb

#####

####

aabb

####

#c#

a*b

#d#

#c#

acb

a*b

aa*bb

a#.#b

aa*bb

一道bfs的题。。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int T;
char map[505][505];//表示[i][j]该点
int flag[505][505];//表示所有国家到[i][j]的时间里面的最小时间
int direction[4][2]={0,1,0,-1,1,0,-1,0};//4个方向
int R,C;
struct Node
{
    int x,y,num;//x,y表示坐标,num表示第几个时间点到达。
    char s;//s表示字母
};
void BFS()
{
    memset(flag,0,sizeof(flag));
    queue<Node>q;
    for (int i=0;i<R;i++)
    for (int j=0;j<C;j++)
    {
        if(map[i][j]>=‘a‘&&map[i][j]<=‘z‘)//一开始把所有国家存储进去
        q.push((Node){i,j,0,map[i][j]});//i,j,0,map[i][j]分别对应Node中的4个坐标。
     }
     while (!q.empty())
     {
         Node t=q.front();
         q.pop();
         if(map[t.x][t.y]==‘*‘) continue;//如果这个地方已经发生战争就可以直接退出
         for(int i=0;i<4;i++)
         {
             int xx=t.x+direction[i][0];
             int yy=t.y+direction[i][1];
             if(xx<0||xx>=R||yy<0||yy>=C||map[xx][yy]==‘*‘||map[xx][yy]==‘#‘)//超出范围或者不能走或者在发生战争
             {
                 continue;
             }
             if(map[xx][yy]==‘.‘)//可以走,则改变那个点 ,入队
             {
                 map[xx][yy]=t.s;
                 q.push((Node){xx,yy,t.num+1,map[xx][yy]});
                 flag[xx][yy]=t.num+1;
             }
             if(map[xx][yy]!=t.s&&flag[xx][yy]==t.num+1)//满足突破口,不同的国家在同一个时间段同一个地方相遇,则要发生战争
             map[xx][yy]=‘*‘;
         }
     }
}
int main()
{
    while (scanf("%d",&T)!=EOF)
    {
        while (T--)
        {
            scanf("%d%d",&R,&C);
            for (int i=0;i<R;i++)
            scanf("%s",map[i]);
            BFS();
            for (int i=0;i<R;i++)
            printf("%s\n",map[i]);
        }
    }
    return 0;
 }

原文地址:https://www.cnblogs.com/txrtyy/p/9293603.html

时间: 2024-11-05 18:44:04

ECJTU 2018 Summer Training 2的相关文章

2018 UESTC Training for Search Algorithm &amp; String

Link A 题意 给两个字符串S,T,问S的所有前缀在T中出现的次数和 分析 kmp SAM后缀数组 2018 UESTC Training for Search Algorithm & String 原文地址:https://www.cnblogs.com/Deadline/p/9127493.html

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1001.Maximum Multiple (HDU6298)-数学思维题(脑子是个好东西,可惜我没有)

暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.Maximum Multiple 题目意思就是给你一个n,找出来三个数x,y,z, 使得n=x+y+z,而且x,y,z都是n的因数,并且x*y*z为最大值,让你输出来x*y*z的最大值.如果没有满足条件的情况就输出-1. 由1=1/2+1/3+1/6=1/3+1/3+1/3=1/2+1/4+1/4,所

2018 Multi-University Training Contest 4

Problem D. Nothing is Impossible Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 504    Accepted Submission(s): 302 Problem Description m students, including Kazari, will take an exam tomorrow

HDU 6396 Swordsman --------2018 Multi-University Training Contest 7 (模拟+读入挂)

原题地址: 打怪升级 一开始有N个怪物:主角有K个能力:只有K个能力都击败怪物才能斩杀怪物并获得K个能力的增值:问最多能杀几个怪物: 做法: 用优先队列把怪物能力装进去:能力小放前面: 最重要的是数据量要用读入挂才能过:(读入挂太神奇了!!) Swordsman Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2049    Acce

2018 Multi-University Training Contest 3

2018 Multi-University Training Contest 2 题解 A - Problem A. Ascending Rating 题目描述:给定一个序列,分别求出所有长度为\(m\)的区间的\(maxrating, count\),对于每个长度为\(m\)的区间,一开始\(maxrating=-1, count=0\),然后从左往右扫,扫到一个大于\(maxrating\)的值时,\(count+1, maxrating=\)那个数. solution 从左往右做,用单调队

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5312    Accepted Submission(s): 1823 Problem Description Chiaki has an array of

2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 830 Problem Description Ther

2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2150    Accepted Submission(s): 772Special Judge Problem De