SRM559

来源:互联网 发布:牛头702在淘宝上叫什么 编辑:程序博客网 时间:2024/06/05 08:33

Problem Statement

 In the Land of Winter lives the Toy King. The Toy King owns many fields and in each field he has control over different plots of land. The Toy King also has nine lords under him. To each of his lords he has given control over some of the plots in his kingdom. Now he wishes to use some of those plots to make train tracks for a winter celebration.

To build his train tracks the king can use three types of track pieces. Each of these pieces resembles a puzzle piece where each end can be positive or negative.


  1. A type A piece of track rotates by 90 degrees and has two positive ends.
  2. A type B piece of track rotates by 90 degrees and has two negative ends.
  3. A straight piece has both one positive and one negative end.

Notice how the positive end fits snugly together with the negative end. The ends of two pieces of track can connect if and only if one end is positive and one end is negative. This makes creating connected tracks very easy. Simply place two pieces of track side by side and make sure the positive and negative ends interlock.

Currently, some plots of land are empty and others contain pieces of track of various types. Each plot of land can contain at most one piece of track. While forming the new tracks, the king allowed you to rotate the existing pieces of track in increments of 90 degrees. Additionally, you are allowed to add new straight pieces onto empty plots of land. Each of these new pieces can have any of the four possible orientations. You arenot allowed to add new pieces of types A and B.

You are given a String[] field that describes a rectangular area of the country, divided into unit square plots of land.

Each character of each element of field describes one plot of land. The meanings of individual characters follow.
  • The character '.' represents an empty plot of land.
  • The character 'A' represents a plot occupied by a type A piece.
  • The character 'B' represents a plot occupied by a type B piece.
  • The character 'S' represents a plot of land occupied by a straight piece.
  • If the character is a digit 'x' (where 'x' is from '1' to '9'), it represents a plot of land owned by the lord number x.

The king would like to have a non-empty set of closed track segments. More precisely, in the final configuration there must be at least one track piece and each track piece has to be connected to two other track pieces. Note that in the final configuration there may be more than one closed sequence of tracks. Sometimes it may happen that it is impossible to build the tracks without using plots that were assigned to some of the lords. If the king wishes to use one or more plots that were assigned to lord i, he will pay the lord exactly i gold coins. Paying the i coins once allows the king to use all the plots that were assigned to lord i. Adding new straight pieces does not cost anything. The king would like to know the minimum total amount of gold coins he would have to pay to make the train tracks. Return this minimum amount. If it is not possible to build the train tracks, return -1 instead.

Definition

 Class:ToyTrainMethod:getMinCostParameters:String[]Returns:intMethod signature:int getMinCost(String[] field)(be sure your method is public)  

Constraints

-field will contain between 1 and 50 elements, inclusive.-Each element of field will contain between 1 and 50 characters, inclusive.-Each element of field will contain the same number of characters.-Each character of each element of field will be 'A', 'B', 'S', '.' or a digit between '1' and '9', inclusive.

Examples

0)  
{"BA", "SS", "AB"}
Returns: 0
1)  
{"."}
Returns: -1
It is not possible to build any closed train tracks.2)  
{"ABBA", "BAAB"}
Returns: 0
The King can make two separate tracks for this field.3)  
{"AA", "AA"}
Returns: -1
4)  
{"..AB", "B..A", "....", "A.B."}
Returns: -1
5)  
{"A1B8A2B", "16A.B22", "BAB.9.A", "ABA.7.B", "B12345A"}
Returns: 31
6)  
{"..A.B", ".1.2.", "A.B..", ".3.4.", "B...A"}
Returns: 0
7)  
{"ASBSBSA", "S.S.S.S", "BSASASB"}
Returns: -1

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.     

真的看不懂题目啊。只好拿别人的来看了。

#define FOR(i,a) for(int i=0;i<a;++i)

char c[50][50]={0};

class ToyTrain {

public:

int getMinCost  (vector <string> field){

int empty=1;

int h=field.size(), w= field[0].size();

FOR(i,h)  FOR(j,w) c[i][j] =0 ;


FOR(i,h){

char last=-1;

FOR(j,w){

if(field[i][j]=='A' ||field[i] [j]=='B'){

empty=0;

if(last==-1)last=field[i][j];

else if (last ==field[i][j] return -1;

else last =-1;

}

else if(last!=-1) c[i][j]='h';

}

if(last!=-1) return -1;

}//我想大概是A,B 22 互补,A与B之间的标记h(横向)

if(empty) return -1;

//rows ok


FOR(j,w){

char last=-1;

FOR(i,h){

if(field[i][j]=='A' ||field[i] [j]=='B'){

if(last==-1)last=field[i][j];

else if (last ==field[i][j] return -1;

else last =-1;

}

else if(last!=-1) { if(c[i][j]=='h') return -1;c[i][j]='v';}

}

if(last!=-1) return -1;

}//cols ok

//每2个A与B之间的标记V(纵向)


int cost =0.paid[10]={0};

FOR(i,h) FOR(j,w){

if(!c[i][j]&&field[i][j]=='S') return -1;//如果S不是在AB之间就错误

if(c[i][j]&&field [i][j]>='1'&&field[i][j]<=9){

int n=field[i][j]-'0';

if (!paid[n]){paid[n]=1;cost+=n;}//支付一次就够了

}

return cost;

}

}



原创粉丝点击