problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
acwing::acwing3370 命名空间参考

AcWing 3370. 牛年 更多...

struct  cow
 

函数

int dfs (cow *c)
 
int main (istream &cin, ostream &cout)
 
 TEST (acwing3370, case1)
 
 TEST (acwing3370, case2)
 

详细描述

AcWing 3370. 牛年

函数说明

◆ dfs()

int acwing::acwing3370::dfs ( cow c)

在文件 acwing.cpp5451 行定义.

5451 {
5452 if(c->name == "Elsie") {
5453 return c->val;
5454 }
5455 for(auto *prev: c->previous) {
5456 if(prev->val == -1) {
5457 if(c->zodiac == prev->zodiac) {
5458 prev->val = c->val - 12;
5459 } else {
5460 prev->val = c->val - (c->zodiac + 12 - prev->zodiac) % 12;
5461 }
5462 const int ret = dfs(prev);
5463 if(ret != -1) {
5464 return ret;
5465 }
5466 }
5467 }
5468 for(auto *nxt: c->next) {
5469 if(nxt->val == -1) {
5470 if(c->zodiac == nxt->zodiac) {
5471 nxt->val = c->val + 12;
5472 } else {
5473 nxt->val = c->val + (nxt->zodiac + 12 - c->zodiac) % 12;
5474 }
5475 const int ret = dfs(nxt);
5476 if(ret != -1) {
5477 return ret;
5478 }
5479 }
5480 }
5481 return -1;
5482 }
bool dfs(vector< unordered_set< int > > &g, int node, vector< int > &color, int c)
Definition: acwing.cpp:7481
vector< cow * > next
Definition: acwing.h:1774
vector< cow * > previous
Definition: acwing.h:1773

◆ main()

int acwing::acwing3370::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing.cpp5397 行定义.

5397 {
5398 int n;
5399 cin >> n;
5400 unordered_map<string, int> zodiacs;
5401 unordered_map<string, cow *> cows;
5402 zodiacs["Ox"] = 0;
5403 zodiacs["Tiger"] = 1;
5404 zodiacs["Rabbit"] = 2;
5405 zodiacs["Dragon"] = 3;
5406 zodiacs["Snake"] = 4;
5407 zodiacs["Horse"] = 5;
5408 zodiacs["Goat"] = 6;
5409 zodiacs["Monkey"] = 7;
5410 zodiacs["Rooster"] = 8;
5411 zodiacs["Dog"] = 9;
5412 zodiacs["Pig"] = 10;
5413 zodiacs["Rat"] = 11;
5414 cows["Bessie"] = new cow("Bessie", 0, zodiacs["Ox"]);
5415 for(int i = 0; i < n; i++) {
5416 string str;
5417 string name1;
5418 string name2;
5419 cin >> name1 >> str >> str >> str;
5420 const bool previous = str == "previous";
5421 cin >> str;
5422 const int zodiac = zodiacs[str];
5423 cin >> str >> str >> name2;
5424 cow *cow1;
5425 cow *cow2;
5426 if(!cows.contains(name1)) {
5427 cow1 = new cow(name1, -1, zodiac);
5428 cows[name1] = cow1;
5429 } else {
5430 cow1 = cows[name1];
5431 cow1->zodiac = zodiac;
5432 }
5433 if(!cows.contains(name2)) {
5434 cow2 = new cow(name2, -1, -1);
5435 cows[name2] = cow2;
5436 } else {
5437 cow2 = cows[name2];
5438 }
5439 if(previous) {
5440 cow2->previous.push_back(cow1);
5441 cow1->next.push_back(cow2);
5442 } else {
5443 cow2->next.push_back(cow1);
5444 cow1->previous.push_back(cow2);
5445 }
5446 }
5447 cout << abs(dfs(cows["Bessie"]));
5448 return 0;
5449 }

被这些函数引用 TEST().

◆ TEST() [1/2]

acwing::acwing3370::TEST ( acwing3370  ,
case1   
)

在文件 acwing_test.cpp2523 行定义.

2523 {
2524 istringstream in("4\n"
2525 "Mildred born in previous Dragon year from Bessie\n"
2526 "Gretta born in previous Monkey year from Mildred\n"
2527 "Elsie born in next Ox year from Gretta\n"
2528 "Paulina born in next Dog year from Bessie");
2529 auto out = ostringstream();
2530 main(in, out);
2531 const auto ans = out.str();
2532 ASSERT_EQ("12", ans);
2533 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/2]

acwing::acwing3370::TEST ( acwing3370  ,
case2   
)

在文件 acwing_test.cpp2535 行定义.

2535 {
2536 istringstream in("10\n"
2537 "Aa born in previous Dog year from Bessie\n"
2538 "Ab born in next Monkey year from Aa\n"
2539 "Ac born in previous Dog year from Bessie\n"
2540 "Ad born in next Monkey year from Ab\n"
2541 "Ae born in next Rat year from Ad\n"
2542 "Af born in next Goat year from Bessie\n"
2543 "Ag born in next Rat year from Ab\n"
2544 "Ah born in next Dog year from Ag\n"
2545 "Elsie born in previous Rabbit year from Ah\n"
2546 "Aj born in previous Monkey year from Ag");
2547 auto out = ostringstream();
2548 main(in, out);
2549 const auto ans = out.str();
2550 ASSERT_EQ("14", ans);
2551 }

引用了 main().