UVA 657 - The die is cast

求高手!又是Runtime Error...

只是为啥捏..

 

View Code
  1 #include <iostream>
  2 #include <algorithm>
  3 using namespace std;
  4 
  5 const int LENGTH = 50;
  6 
  7 struct Position{
  8     int row;
  9     int col;
 10 };
 11 
 12 void SearchBoundry(char pixel[][LENGTH], bool visit[][LENGTH],
 13                    int x, int y, int w, int h,
 14                    int& left, int& right, int&top, int& bottom) {
 15     int front = 0, rear = 1;
 16     Position pos[1000];
 17     pos[0].row = x;
 18     pos[0].col = y;
 19     visit[x][y] = true;
 20     while (front < rear){
 21         int row = pos[front].row, col = pos[front].col;
 22         front++;
 23 
 24         left = min(left, col);
 25         right = max(right, col);
 26         top = min(top, row);
 27         bottom = max(bottom, row);
 28 
 29         for (int i = max(0, row-1); i <= min(row+1, h); i++){
 30             for (int j = max(0, col-1); j <= min(col+1, w); j++){
 31                 if (!((i == (row-1) || i == (row+1)) &&
 32                       (j == (col-1) || j == (col+1))) &&
 33                     !visit[i][j] &&
 34                     (pixel[i][j] == '*' || pixel[i][j] == 'X')){
 35 
 36                     visit[i][j] = true;
 37                     pos[rear].row = i;
 38                     pos[rear].col = j;
 39                     rear++;
 40                 }
 41             }
 42         }
 43     }
 44 }
 45 
 46 int CountDice(char pixel[][LENGTH], int& left, int& right,
 47               int& top, int& bottom) {
 48     int result = 0;
 49 
 50     for (int i = top; i <= bottom; i++){
 51         for (int j = left; j <= right; j++){
 52             if (pixel[i][j] == '*')
 53                 pixel[i][j] = '.';
 54             if (pixel[i][j] == 'X'){
 55                 result++;
 56                 int front = 0, rear = 1;
 57                 Position pos[250];
 58                 pos[0].row = i;
 59                 pos[0].col = j;
 60                 pixel[i][j] = '.';
 61                 while (front < rear){
 62                     int row = pos[front].row, col = pos[front].col;
 63                     front++;
 64 
 65                     for (int i = max(top, row-1); i <= min(row+1, bottom); i++){
 66                         for (int j = max(left, col-1); j <= min(col+1, right); j++){
 67                             if (!((i == (row-1) || i == (row+1)) &&
 68                                   (j == (col-1) || j == (col+1))) &&
 69                                 pixel[i][j] == 'X'){
 70 
 71                                 pixel[i][j] = '.';
 72                                 pos[rear].row = i;
 73                                 pos[rear].col = j;
 74                                 rear++;
 75                             }
 76                         }
 77                     }
 78                 }
 79             }
 80         }
 81     }
 82 
 83     return result;
 84 }
 85 
 86 int main(int argc, char *argv[]){
 87     char pixel[LENGTH][LENGTH];
 88     bool visit[LENGTH][LENGTH];
 89 
 90     int w, h, count = 0;
 91     while (cin >> w >> h && w+h){
 92         cout << "Throw " << ++count << endl;
 93         bool first = true;
 94 
 95         for (int i = 0; i < LENGTH; i++)
 96             for (int j = 0; j < LENGTH; j++){
 97                 pixel[i][j] = ' ';
 98                 visit[i][j] = false;
 99             }
100 
101         for (int i = 0; i < h; i++)
102             cin >> pixel[i];
103 
104         for (int i = 0; i < h; i++){
105             for (int j = 0; j < w; j++){
106                 if (pixel[i][j] == '*' || pixel[i][j] == 'X'){
107                     int left = j, right = j, top = i, bottom = i;
108                     SearchBoundry(pixel, visit, i, j, w, h,
109                                   left, right, top, bottom);
110 
111                     int dice = CountDice(pixel, left, right,
112                                          top, bottom);
113 
114                     if (first)
115                         first = false;
116                     else
117                         cout << " ";
118                     cout << dice;
119                 }
120             }
121         }
122         cout << endl;
123 
124     }
125     return 0;
126 }

 

posted @ 2013-01-21 02:16  frankdj  阅读(165)  评论(0)    收藏  举报