算法连载5:

单人包围游戏得分计算

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.     



#include<math.h>
#include<vector>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include <stdio.h>


using namespace std;




#define FOR(I,A,B) for (int I=int(A);I<int(B);I++)
#define PUSH(X,N,n) {FOR(q,0,N){X.push_back(n);}}
#define  LL _int64


template<class T>  T max (T A, T B){if(A>B) return A; else return B;} 
template<class T>  T min (T A, T B){if(A<B) return A; else return B;} 


class 
SurroundingGameEasy{
public:
int score(vector <string> cost, vector <string> benefit, vector <string> stone){
int i,j;
i=cost.size();j=cost[0].size();




int Ncost=0;//先算花费,再算得到
FOR( m,0,i){
FOR(n,0,j){
if(stone[m][n]=='o')
Ncost+=cost[m][n]-'0';}
}
int Nbenifit=0;
FOR( a,0,i){
FOR(b,0,j){
if(stone[a][b]=='o')
Nbenifit+=benefit[a][b]-'0';
else{
if(a-1<0||stone[a-1][b]=='o')
if(a+1>=i||stone[a+1][b]=='o')
if(b-1<0||stone[a][b-1]=='o')
if(b+1>=j||stone[a][b+1]=='o')
Nbenifit+=benefit[a][b]-'0';//一定要小心啊,字符和数字的转化
}
}
}
/**/
return Nbenifit-Ncost;


}


};

这道简单的,就是不熟练啊,花了40多分钟



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值