1 // majiangTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 2 // 3 4 5 #include <iostream> 6 #include <string> 7 #include <map> 8 #include <assert.h> 9 #include <vector> 10 #include <chrono> 11 12 using namespace std; 13 using namespace std::chrono; 14 15 #define HANDPAI_LIMIT 100 16 17 map<string, int> strValMj; 18 map<int, string> valStrMj; 19 20 map<int,int> valWight; 21 22 vector<int> handPai; 23 24 vector<string> shunziVec; 25 vector<string> keziVec; 26 27 vector<string> final; 28 29 int winsum = 0; 30 int wintotal = 0; 31 32 int gidx = 0; 33 int winidx = -1; 34 35 void Init() 36 { 37 strValMj["一万"] = 11; 38 strValMj["二万"] = 12; 39 strValMj["三万"] = 13; 40 strValMj["四万"] = 14; 41 strValMj["五万"] = 15; 42 strValMj["六万"] = 16; 43 strValMj["七万"] = 17; 44 strValMj["八万"] = 18; 45 strValMj["九万"] = 19; 46 47 strValMj["一筒"] = 31; 48 strValMj["二筒"] = 32; 49 strValMj["三筒"] = 33; 50 strValMj["四筒"] = 34; 51 strValMj["五筒"] = 35; 52 strValMj["六筒"] = 36; 53 strValMj["七筒"] = 37; 54 strValMj["八筒"] = 38; 55 strValMj["九筒"] = 39; 56 57 58 strValMj["一条"] = 51; 59 strValMj["二条"] = 52; 60 strValMj["三条"] = 53; 61 strValMj["四条"] = 54; 62 strValMj["五条"] = 55; 63 strValMj["六条"] = 56; 64 strValMj["七条"] = 57; 65 strValMj["八条"] = 58; 66 strValMj["九条"] = 59; 67 68 strValMj["红中"] = 63; 69 strValMj["发财"] = 64; 70 strValMj["白板"] = 65; 71 72 strValMj["东风"] = 72; 73 strValMj["南风"] = 75; 74 strValMj["西风"] = 78; 75 strValMj["北风"] = 81; 76 77 for (auto& e : strValMj) 78 { 79 string s = e.first; 80 int num = e.second; 81 valStrMj[num] = s; 82 } 83 84 vector<int> h(HANDPAI_LIMIT,0); 85 86 int idx = strValMj["九筒"]; 87 h[idx] = 1; 88 89 idx = strValMj["四万"]; 90 h[idx] = 2; 91 92 idx = strValMj["五万"]; 93 h[idx] = 1; 94 95 idx = strValMj["一条"]; 96 h[idx] = 1; 97 98 idx = strValMj["二条"]; 99 h[idx] = 1; 100 101 idx = strValMj["三条"]; 102 h[idx] = 1; 103 104 idx = strValMj["四条"]; 105 h[idx] = 1; 106 107 idx = strValMj["七条"]; 108 h[idx] = 1; 109 110 idx = strValMj["九条"]; 111 h[idx] = 1; 112 113 idx = strValMj["红中"]; 114 h[idx] = 1; 115 116 idx = strValMj["发财"]; 117 h[idx] = 2; 118 119 idx = strValMj["白板"]; 120 h[idx] = 1; 121 122 handPai = h; 123 124 int count = 0; 125 for (int i = 0; i < HANDPAI_LIMIT; i++) { 126 if (handPai[i] != 0) { 127 ////cout << "[" << i << "] = " << handPai[i] << endl; 128 } 129 count += handPai[i]; 130 } 131 132 assert(count == 14); 133 } 134 135 136 void Dfs(); 137 138 139 void CutShunzi() 140 { 141 int start = strValMj["一万"]; 142 int end = strValMj["北风"]; 143 int i = 0; 144 for (i = start; i <= end; i++) { 145 if (handPai[i] != 0 && handPai[i + 1] != 0 && handPai[i + 2] != 0) 146 { 147 string s; 148 s += valStrMj[i]; s += " "; 149 s += valStrMj[i + 1]; s += " "; 150 s += valStrMj[i+2]; 151 shunziVec.push_back(s); 152 handPai[i]--; handPai[i+1]--; handPai[i+2]--; 153 Dfs(); 154 handPai[i]++; handPai[i + 1]++; handPai[i + 2]++; 155 shunziVec.pop_back(); 156 } 157 } 158 } 159 160 void CutKezi() 161 { 162 int start = strValMj["一万"]; 163 int end = strValMj["北风"]; 164 int i = 0; 165 for (i = start; i <= end; i++) { 166 if (handPai[i]>=3) 167 { 168 string s; 169 s += valStrMj[i]; s += " "; s += valStrMj[i]; s += " "; s += valStrMj[i]; s += " "; 170 keziVec.push_back(s); 171 handPai[i] -= 3; 172 Dfs(); 173 handPai[i] += 3; 174 keziVec.pop_back(); 175 } 176 } 177 } 178 179 180 void Dfs() 181 { 182 //拆除顺子 183 CutShunzi(); 184 //拆除刻子 185 CutKezi(); 186 187 vector<string> tmp; 188 189 //算分 190 //cout << "=======================================" << endl; 191 int i = 0; int count = 0; 192 for (i = 0; i < HANDPAI_LIMIT; i++) { 193 count += handPai[i]; 194 } 195 196 vector<int> handPaiCopy = handPai; 197 int kezishunziNum = (14 - count) / 3; 198 int daziCount = 0; int duiziCount = 0; int danzhangCount = 0; 199 for (i = 0; i < 100; i++) { 200 if (handPai[i] != 0 && handPai[i + 1] != 0) { 201 daziCount++; handPai[i]--; handPai[i + 1]--; 202 string s = "dazi"; 203 s += valStrMj[i]; 204 s += " "; s += valStrMj[i + 1]; 205 tmp.push_back(s); 206 //cout << "dazi " << valStrMj[i] << " " << valStrMj[i + 1] << endl; 207 } 208 if (handPai[i] != 0 && handPai[i + 2] != 0) { 209 daziCount++; handPai[i]--; handPai[i + 2]--; 210 string s = "dazi"; 211 s += valStrMj[i]; 212 s += " "; s += valStrMj[i + 2]; 213 tmp.push_back(s); 214 //cout << "dazi " << valStrMj[i] << " " << valStrMj[i + 2] << endl; 215 } 216 if (handPai[i] == 2) { 217 duiziCount++; handPai[i] -= 2; 218 string s = "duizi"; 219 s += valStrMj[i]; 220 s += " "; s += valStrMj[i]; 221 tmp.push_back(s); 222 //cout << "duizi " << valStrMj[i] << " " << valStrMj[i] << endl; 223 } 224 if (handPai[i] == 1) { 225 danzhangCount++; handPai[i] -= 1; 226 string s = "danzhang"; 227 s += valStrMj[i]; 228 tmp.push_back(s); 229 //cout << "danzhang " << valStrMj[i] << endl; 230 } 231 if (handPai[i] != 0) { 232 assert(0); 233 } 234 } 235 236 for (auto& e : shunziVec) { 237 string s = "顺子 : "; 238 s += e; 239 tmp.push_back(s); 240 //cout << "顺子 : " << e << endl; 241 } 242 243 for (auto& e : keziVec) { 244 string s = "刻子 : "; 245 s += e; 246 tmp.push_back(s); 247 //cout << "刻子 : " << e << endl; 248 } 249 250 int sum = kezishunziNum * 10 + daziCount * 5 + duiziCount * 5 + danzhangCount * 1; 251 252 //cout << "sum = " << sum << endl; 253 254 int total = 0; 255 handPai = handPaiCopy; 256 257 for (i = 0; i < 100; i++) { 258 if (handPai[i] != 0) { 259 if (i == gidx) 260 continue; 261 if (i >= 63 && i <= 81) { 262 total += 1; 263 } 264 else if (i == 11 || i == 19 || i == 31 || i == 39 || i == 51 || i == 59 || i == 63 || i == 65) { 265 total += 2; 266 } 267 else { 268 total += 3; 269 } 270 } 271 } 272 //cout << "totoal = " << total << endl; 273 //cout << "chupai : " << valStrMj[gidx] << endl; 274 //cout << "=======================================" << endl; 275 276 if (sum > winsum) { 277 winsum = sum; 278 wintotal = total; 279 final = tmp; 280 winidx = gidx; 281 } 282 else if (sum == winsum) { 283 if (total > wintotal) { 284 wintotal = total; 285 final = tmp; 286 winidx = gidx; 287 } 288 } 289 } 290 291 292 293 int main() 294 { 295 ////cout << "Hello World!\n"; 296 Init(); 297 298 auto start = system_clock::now(); 299 300 301 302 for (gidx = 0; gidx < 100; gidx++) { 303 if (handPai[gidx] != 0) { 304 handPai[gidx]--; 305 Dfs(); 306 handPai[gidx]++; 307 } 308 } 309 310 //cout << "chupai : " << valStrMj[winidx] << endl; 311 for (auto& e : final) { 312 //cout << e << endl; 313 } 314 315 316 auto end = system_clock::now(); 317 auto duration = duration_cast<microseconds>(end - start); 318 cout << "次出手。共花费了" 319 << double(duration.count()) * microseconds::period::num / microseconds::period::den 320 << "秒" << endl;//print: 花费了2.00011秒 321 system("pause"); 322 }
自用 一个计算麻将出牌的代码片段
猜你喜欢
转载自www.cnblogs.com/itdef/p/10794190.html
今日推荐
周排行