连续出牌数量
标题:连续出牌数量 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
有这么一款单人卡牌游戏,牌面由颜色和数字组成,颜色为红、黄、蓝、绿中的一种,数字为0-9中的一个。游戏开始时玩家从手牌中选取一张卡牌打出,接下来如果玩家手中有和他上一次打出的手牌颜色或者数字相同的手牌,他可以继续将该手牌打出,直至手牌打光或者没有符合条件可以继续打出的手牌。
#include<iostream> #include<unordered_set> #include<algorithm> using namespace std; class NODE { public: int val; char color; bool operator==(const NODE &n) const { return (this->val == n.val && this->color == n.color); } }; typedef struct NODE Node; struct Node_Hash { size_t operator()(const Node &node) const { return hash<int>()(node.color ^ node.val); } }; unordered_set<Node, Node_Hash> s; int ans = 0; bool check(Node curCode, Node *nodes, int size) { for (int i = 0; i < size; i++) { Node node = *(nodes + i); if (s.find(node) != s.end()) continue; if (node.color == curCode.color || node.val == curCode.val) { return false; } } return true; } void backMy(Node curNode, Node *nodes, int curSum, int nodeLen) { if (s.size() == nodeLen || check(curNode, nodes, nodeLen)) { ans = max(ans, curSum); return; } for (int i = 0; i < nodeLen; i++) { Node node = *(nodes + i); if (s.find(node) != s.end()) continue; if (node.color == curNode.color || node.val == curNode.val) { s.insert(node); backMy(node, nodes, curSum + 1, nodeLen); s.erase(node); } } } int fillMy(Node *nodes, int size) { for (int i = 0; i < size; i++) { Node node = *(nodes + i); s.insert(node); backMy(node, nodes, 1, size); s.erase(node); } return ans; } int main() { string num; string color; getline(cin, num); getline(cin, color); int len = num.length() / 2 + 1; Node node[len]; int k = 0; ans = 0; for (int i = 0; i < color.size(); i++) { if (num[i] == ' ' || color[i] == ' ') continue; node[k].val = num[i] - '0'; node[k].color = color[i]; k++; } int res = fillMy(node, len); cout << res << endl; }
import sys number_str = sys.stdin.readline().strip().split(' ') color_str = sys.stdin.readline().strip().split(' ') def find(numbers, colors,last_num,last_color, card): maxdepth = 0 for i in range(len(card)): if card[i] !=0: if numbers[i] == last_num or colors[i] == last_color: card[i] = 0 maxdepth = max(find(numbers, colors, numbers[i], colors[i], card),maxdepth) card[i] = 1 return maxdepth + 1 cards = [1 for i in range(len(number_str))] maxiter = 0 for i in range(len(number_str)): cards[i] = 0 maxiter = max(find(number_str, color_str, number_str[i], color_str[i], cards),maxiter) cards[i] = 1 print(maxiter) //190