[Botzone AI]扫雷(暴力,枚举)

如题,在botzone上的扫雷源代码,决定今天在这里公开一下…

Botzone的链接:https://www.botzone.org/game/Minesweeper

  1 #include "jsoncpp/json.h"
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 
  5 // 0-8:数字
  6 // 9:雷
  7 // 10 / A:未点开
  8 
  9 typedef struct P {
 10     int x, y;
 11     int mine;
 12     P() {}
 13     P(int x, int y, int mine) : x(x), y(y), mine(mine) {}
 14     bool operator < (const P& p) const {
 15         return this->mine < p.mine;
 16     }
 17 }P;
 18 
 19 typedef pair<int, int> pii;
 20 const int MAX_WIDTH = 80;
 21 const int MAX_HEIGHT = 40;
 22 const int dx[9] = {0,0,1,-1,1,-1,1,-1};
 23 const int dy[9] = {1,-1,0,0,-1,1,1,-1};
 24 
 25 int fieldHeight, fieldWidth, mineCount;
 26 int G[MAX_HEIGHT][MAX_WIDTH];
 27 int decidedRow, decidedCol;
 28 vector<P> p;
 29 vector<pii> untouch;
 30 
 31 void Init() {
 32     for(int row = 0; row < fieldHeight; row++) {
 33         for(int col = 0; col < fieldWidth; col++) {
 34             G[row][col] = 10;
 35         }
 36     }
 37 }
 38 
 39 inline bool isValid(int& row, int &col) {
 40     return row >= 0 && row < fieldHeight && col >= 0 && col < fieldWidth;
 41 }
 42 
 43 void Check(int row, int col) {
 44     int totMine = 0, totUnrevealed = 0;
 45     for(int i = 0; i < 8; i++) {
 46         int x = row + dx[i];
 47         int y = col + dy[i];
 48         if(!isValid(x, y))
 49             continue;
 50         if(G[x][y] == 9)
 51             totMine++;
 52         if(G[x][y] == 10)
 53             totUnrevealed++;
 54     }
 55     if(totUnrevealed == 0)    //邻接都被扫过
 56         return;
 57     if(totMine + totUnrevealed == G[row][col]) {
 58         for(int i = 0; i < 8; i++) {
 59             int x = row + dx[i];
 60             int y = col + dy[i];
 61             if(!isValid(x, y))
 62                 continue;
 63             if(G[x][y] == 10)
 64                 G[x][y] = 9; //空白都是雷
 65         }
 66     }
 67     if(totMine == G[row][col]) { //雷够了,随便点开一个
 68         for(int i = 0; i < 8; i++) {
 69             int x = row + dx[i];
 70             int y = col + dy[i];
 71             if(!isValid(x, y))
 72                 continue;
 73             if(G[x][y] == 10) {
 74                 decidedRow = x;
 75                 decidedCol = y;
 76                 return;
 77             }
 78         }
 79     }
 80 }
 81 
 82 void Decide() {
 83     p.clear();
 84     for(int row = 0; row < fieldHeight; row++)
 85         for(int col = 0; col < fieldWidth; col++)
 86             if(G[row][col] >= 1 && G[row][col] <= 9)
 87                 p.push_back(P(row, col, G[row][col]));
 88     if(p.size() == 0)
 89         return;
 90     sort(p.begin(), p.end());
 91     for(int i = 0; i < p.size(); i++) {
 92         Check(p[i].x, p[i].y);
 93         if(!(decidedRow == -1 || decidedCol == -1))
 94             return;
 95     }
 96 }
 97 
 98 int main() {
 99 //    freopen("test.in", "r", stdin);
100     srand(time(0));
101     string str;
102     getline(cin, str);
103     Json::Reader reader;
104     Json::Value input, output, lastInput;
105     reader.parse(str, input);
106     int len = input["requests"].size(); // 读取程序曾经接到的输入总数
107     lastInput = input["requests"][len - 1]; // 取出最后一次获得的输入
108     fieldHeight = lastInput["height"].asInt(); // 读取雷区高度(始终不变)
109     fieldWidth = lastInput["width"].asInt(); // 读取雷区高度(始终不变)
110     mineCount = lastInput["minecount"].asInt(); // 读取雷数(始终不变)
111     Init();
112 
113     for(int i = 0; i < len; i++) {
114         Json::Value changed = input["requests"][i]["changed"];
115         if(changed.isArray()) {
116             int changedLen = changed.size();
117             for(int j = 0; j < changedLen; j++) {
118                 G[changed[j]["row"].asInt()][changed[j]["col"].asInt()] = changed[j]["val"].asInt();
119             }
120         }
121     }
122     decidedRow = -1, decidedCol = -1;
123     Decide();
124     if(decidedRow == -1 || decidedCol == -1) {
125         //随一对
126         untouch.clear();
127         for(int row = 0; row < fieldHeight; row++)
128             for(int col = 0; col < fieldWidth; col++)
129                 if(G[row][col] == 10)
130                     untouch.push_back(pii(row, col));
131         int choice = rand() % untouch.size();
132         decidedRow = untouch[choice].first;
133         decidedCol = untouch[choice].second;
134     }
135     output["response"]["row"] = decidedRow;
136     output["response"]["col"] = decidedCol;
137     Json::FastWriter writer;
138     cout << writer.write(output) << endl;
139 }
原文地址:https://www.cnblogs.com/kirai/p/6478603.html