Computer Programming Contest Preparation

ToolBox - Source for: 102/10279/b.cpp



/home/toolbox/public_html/solutions/102/10279/b.cpp
    1 #include <cstdio>
    2 #include <iostream>
    3 #include <vector>
    4 #include <cstring>
    5 #include <cstdlib>
    6 
    7 using namespace std;
    8 
    9 #define DEBUG
   10 //#undef DEBUG //uncomment this line to pull out print statements
   11 #ifdef DEBUG
   12 #define TAB '\t'
   13 #define debug(a, end) cout << #a << ": " << a << end
   14 #else
   15 #define debug(a, end)
   16 #endif
   17 
   18 typedef pair<int, int> point;
   19 typedef long long int64; //for clarity
   20 typedef vector<int> vi; //?
   21 typedef vector<point> vp; //?
   22 template<class T> void chmin(T &t, T f)
   23 {
   24     if (t > f) t = f;    //change min
   25 }
   26 template<class T> void chmax(T &t, T f)
   27 {
   28     if (t < f) t = f;    //change max
   29 }
   30 
   31 #define UN(v) SORT(v),v.erase(unique(v.begin(),v.end()),v.end())
   32 #define SORT(c) sort((c).begin(),(c).end())
   33 #define FOR(i,a,b) for (int  i=(a); i < (b); i++)
   34 #define REP(i,n) FOR(i,0,n)
   35 #define CL(a,b) memset(a,b,sizeof(a))
   36 #define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y)
   37 
   38 #define X 'x'
   39 #define BOMB '*'
   40 
   41 /*global variables*/
   42 char graph[12][12];
   43 char area[12][12];
   44 const char bomb = '*';
   45 int n;
   46 
   47 int rangex[] = {1, 1, 0, -1, -1, -1, 0, 1};
   48 int rangey[] = {0, -1, -1, -1, 0, 1, 1, 1};
   49 /*global variables*/
   50 bool show_bombs;
   51 
   52 void dump()
   53 {
   54     //dump data
   55     for (int i = 1; i < n; ++i)
   56         {
   57             for (int j = 1; j < n; ++j)
   58                 {
   59                     printf("%c", graph[i][j]);
   60                 }
   61             cout << endl;
   62         }
   63 }
   64 
   65 void getInput()
   66 {
   67     //get input
   68     CL2d(graph, 0, n, n); //zero out the map
   69     CL2d(area, 0, n, n); //zero out the map
   70     scanf("%d ", &n);
   71     //debug(width, TAB); debug(height, endl);
   72 
   73     n += 1;
   74     for (int i = 1; i < n; ++i)
   75         {
   76             for (int j = 1; j < n; ++j)
   77                 {
   78                     scanf("%c%*[ \n]", &graph[i][j]);
   79                     if (graph[i][j] == '.')
   80                         graph[i][j] = 0;
   81                 }
   82         }
   83 
   84     for (int i = 1; i < n; ++i)
   85         for (int j = 1; j < n; ++j)
   86             {
   87                 scanf("%c%*[ \n]", &area[i][j]);
   88                 show_bombs = show_bombs || ((X == area[i][j]) && (BOMB == graph[i][j]));
   89             }
   90     //dump();
   91 }
   92 
   93 void addonetoneighbors(int x, int y)
   94 {
   95     //process input
   96     for (int i = 0; i < 8; ++i)
   97         {
   98             if (graph[x+rangex[i]][y+rangey[i]] != bomb)
   99                 graph[x+rangex[i]][y+rangey[i]]++;
  100         }
  101 }
  102 
  103 void show_mines()
  104 {
  105     for (int i = 1; i < n; ++i)
  106         {
  107             for (int j = 1; j < n; ++j)
  108                 {
  109                     if (graph[i][j] == bomb)
  110                         {
  111                             area[i][j] = 'x';
  112                             graph[i][j] = bomb-'0';
  113                         }
  114                 }
  115         }
  116 }
  117 
  118 int main()
  119 {
  120     int num_cases;
  121     scanf("%d\n", &num_cases);
  122     while (num_cases-- > 0)
  123         {
  124             getInput();
  125             for (int i = 1; i < n; ++i)
  126                 {
  127                     for (int j = 1; j < n; ++j)
  128                         {
  129                             if (graph[i][j] == bomb)
  130                                 {
  131                                     addonetoneighbors(i, j);
  132                                 }
  133                         }
  134                 }
  135 
  136             /*output*/
  137             show_bombs = FALSE;
  138             for (int i = 1; i < n; ++i)
  139                 {
  140                     for (int j = 1; j < n; ++j)
  141                         {
  142                             if (area[i][j] == 'x')
  143                                 {
  144                                     if (graph[i][j] == bomb)
  145                                         {
  146                                             show_bombs = true;
  147                                             show_mines();
  148                                         }
  149                                 }
  150                         }
  151 
  152                 }
  153             for (int i = 1; i < n; ++i)
  154                 {
  155                     for (int j = 1; j < n; ++j)
  156                         {
  157                             if (area[i][j] == 'x')
  158                                 {
  159                                     printf("%c", graph[i][j]+'0');
  160                                 }
  161                             else
  162                                 printf(".");
  163                         }
  164                     cout << endl;
  165                 }
  166 
  167 
  168             show_bombs = false;
  169             /*output*/
  170             if (num_cases != 0)
  171                 cout << endl;
  172         }
  173 }
  174