TopCoder SRM558 backup 1 DIV 2

Problem Statement

 Surrounding Game is a single-player game played on a rectangular grid of cells. Cells are considered adjacent if they share a common side. (Hence, each cell has at most four adjacent cells. The cells on the sides and in the corners of the grid have fewer adjacent cells than the ones inside the grid.)

The game is played by placing stones into some of the cells. Each cell may only contain at most one stone. A cell is called dominated if at least one of the following two conditions holds:
  • The cell contains a stone.
  • All cells adjacent to the cell contain stones.

Each cell of the grid contains two numbers, each from 0 to 9, inclusive: the cost of placing a stone into the cell, and the benefit from dominating the cell. At the end of the game, the overall score of the player is the sum of all benefits minus the sum of all costs.

You are given the vector <string>s cost and benefit. The characters cost[i][j] and benefit[i][j] represent the two digits written in the cell (i,j). For example, if character 7 of element 4 of cost is '3', the cost of placing a stone into the cell (4,7) is 3.

You are also given a vector <string> stone that describes the final state of the game. The character stone[i][j] is 'o' (lowercase letter oh) if the cell (i,j) contains a stone. Otherwise, stone[i][j] is '.' (a period). Calculate and return the overall score of the game.

Definition

 
Class:SurroundingGameEasy
Method:score
Parameters:vector <string>, vector <string>, vector <string>
Returns:int
Method signature:int score(vector <string> cost, vector <string> benefit, vector <string> stone)
(be sure your method is public)
 
 

Constraints

-cost will contain between 2 and 20 elements, inclusive.
-cost, benefit and stone will each contain the same number of elements.
-Each element of cost will contain between 2 and 20 characters, inclusive.
-Each element of cost will contain the same number of characters.
-Each element of benefit and stone will contain the same number of characters as each element of cost.
-Each character in cost and benefit will be a digit ('0'-'9').
-Each character in stone will either 'o' (lowercase letter oh) or '.'.

Examples

0) 
 
{"21","12"}
{"21","12"}
{".o","o."}
Returns: 4
All the cells are dominated, so the overall benefit is 2+1+1+2 = 6. Only two of the cells contain stones. The total cost of placing the stones is 1+1 = 2. Therefore the overall score is 6-2 = 4.
1) 
 
{"99","99"}
{"11","11"}
{".o","o."}
Returns: -14
A player may get a negative score.
2) 
 
{"888","888","888"}
{"000","090","000"}
{"...",".o.","..."}
Returns: 1
3) 
 
{"4362","4321"}
{"5329","5489"}
{"...o","..o."}
Returns: 22
4) 
 
{"5413","4323","8321","5490"}
{"0432","7291","3901","2310"}
{"ooo.","o..o","...o","oooo"}
Returns: -12

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

题目做起来倒不是很难,只是好久没做题了,搞了很久才在别人的提示下把题目读懂,原来石头是放好的,只要求一下而已。加上好久没写过vector,一开始还忘记了,觉得有必要在这里显示下vector的重要性,所以把代码贴在下面。如果石头不是放好的,那这道题就真有点难度了,那就还不知道能不能在有限的时间内求解。

 

代码如下:

#include<iostream>
#include<vector>
#include<cstring>

#define N 20

using namespace std;

class SurroundingGameEasy{
public:
    int score(vector<string> cost,vector<string> benefit,vector<string> stone){
        vector<string>::iterator it;
        it=cost.begin();
        string str=*it;
        int n=str.length(),m=cost.size();
        //cout<<"n="<<n<<",m="<<m<<endl;
        char c[32][32],b[32][32],s[32][32];
        for(int i=0;i<m;i++){
            str=*it;
            for(int j=0;j<str.length();j++)
                c[i][j]=str.at(j);
            it++;
        }
        it=benefit.begin();
        for(int i=0;i<m;i++){
            str=*it;
            for(int j=0;j<str.length();j++)
                b[i][j]=str.at(j);
            it++;
        }
        it=stone.begin();
        for(int i=0;i<m;i++){
            str=*it;
            for(int j=0;j<str.length();j++)
                s[i][j]=str.at(j);
            it++;
        }

        int sb=0,sc=0;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                if(s[i][j]=='o'){
                    sb+=b[i][j]-'0';
                    sc+=c[i][j]-'0';
                }
                else{
                    if((0<=i-1)&&(s[i-1][j]!='o'))
                        continue;
                    if((0<=j-1)&&(s[i][j-1]!='o'))
                        continue;
                    if((i+1<m)&&(s[i+1][j]!='o'))
                        continue;
                    if((j+1<n)&&(s[i][j+1]!='o'))
                        continue;
                    sb+=b[i][j]-'0';
                }
                return sb-sc;
    }
};

int main(){

    int n;
    cin>>n;
    string cs[N],bs[N],ss[N];

    for(int i=0;i<n;i++)
        cin>>cs[i];
    for(int i=0;i<n;i++)
        cin>>bs[i];
    for(int i=0;i<n;i++)
        cin>>ss[i];

    vector<string> c(cs,cs+n),b(bs,bs+n),s(ss,ss+n);

    SurroundingGameEasy SGE;
    cout<<SGE.score(c,b,s);


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值