正文
增加 34 行删除 132 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139```cpp// CF71D Solitaire#include <algorithm>#include <iostream>#include <iterator>#include <map>#include <set>#include <string>#include <unordered_map>#include <unordered_set>#include <utility>#include <vector>// GLOBALint n, m, cnt;std::string s[20][20];std::set<std::string> cards;int j1x = -1, j1y = -1, j2x = -1, j2y = -1;bool Check(int x, int y){std::map<char, bool> mp;for (int i = x; i < x + 3; i++)for (int j = y; j < y + 3; j++)mp[s[i][j][0]] = true;if ((int)mp.size() == 9)return true;mp.clear();for (int i = x; i < x + 3; i++)for (int j = y; j < y + 3; j++)mp[s[i][j][1]] = true;if ((int)mp.size() == 1)return true;return false;}void OutputSquares(int x1, int y1, int x2, int y2){std::cout << "Put the first square to (" << x1 << ", " << y1 << ").\n";std::cout << "Put the second square to (" << x2 << ", " << y2 << ").";}template <typename PrintFunc>void FindTwoSquares(bool &flag, PrintFunc print){for (int i1 = 1; i1 <= n - 2; i1++)for (int j1 = 1; j1 <= m - 2; j1++)if (Check(i1, j1))for (int i2 = i1; i2 <= n - 2; i2++)for (int j2 = (i2 == i1 ? j1 + 3 : 1); j2 <= m - 2; j2++)if ((j2 > j1 + 2 || i2 > i1 + 2) && Check(i2, j2)){std::cout << "Solution exists.\n";print();OutputSquares(i1, j1, i2, j2);flag = true;return;}}void SolveTwoJokers(bool &flag, const std::string &str1, const std::string &str2){FindTwoSquares(flag, [&](){ std::cout << "Replace J1 with " << str1 << " and J2 with " << str2 << ".\n"; });}void SolveOneJoker(bool &flag, const std::string &str, bool isJ1){FindTwoSquares(flag, [&](){ std::cout << "Replace J" << (isJ1 ? "1" : "2") << " with " << str << ".\n"; });}void SolveNoJoker(bool &flag){FindTwoSquares(flag, [](){ std::cout << "There are no jokers.\n"; });}int main(){// READstd::cin >> n >> m;std::string nums = "A23456789TJQK";std::string cols = "CDHS";for (char num : nums)for (char col : cols)cards.insert(std::string(1, num) + col);for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){std::cin >> s[i][j];if (s[i][j][0] == 'J' && (s[i][j][1] == '1' || s[i][j][1] == '2')){cnt++;if (s[i][j][1] == '1')j1x = i, j1y = j;elsej2x = i, j2y = j;}elsecards.erase(s[i][j]);}}// MAINbool flag = false;if (cnt == 2){for (const std::string &str1 : cards)if (!flag)for (const std::string &str2 : cards)if (!flag)if (str1 != str2){s[j1x][j1y] = str1;s[j2x][j2y] = str2;SolveTwoJokers(flag, str1, str2);}}else if (cnt == 1){bool isJ1 = (j1x != -1);for (const std::string &str : cards){if (!flag){s[isJ1 ? j1x : j2x][isJ1 ? j1y : j2y] = str;SolveOneJoker(flag, str, isJ1);}}}elseSolveNoJoker(flag);if (!flag)std::cout << "No solution.";return 0;}```1234567891011121314151617181920212223242526272829303132333435363738394041$$(1) |x - 5| + |2x - 13| (x > 5)$$由于 $ x > 5 $,那么 $ |x - 5| = x - 5$。由于 $2x = 10$, 所以 $2x - 13$ 不确定它的符号,故分类讨论:$(1)$ $x \ge \frac{13}{2}$ 时,$ |2x - 13| = 2x - 13$,此时$$\begin{equation*}\begin{split}原式 &= x - 5 + 2x - 13\\&= 3x - 18\end{split}\end{equation*}$$$(2)$ $x \le \frac{13}{2}$ 时,$ |2x - 13| = 13 - 2x$,此时$$\begin{equation*}\begin{split}原式 &= x - 5 + 13 - 2x\\&= -x + 8\end{split}\end{equation*}$$这即是两种情况,综上:$$原式=\begin{cases}3x - 18 & (x \ge \frac{13}{2}) \\-x + 8 & (x \le \frac{13}{2})\end{cases}$$