HDOJ - 4414 (Finding crosses)

思路 : 直接枚举

  1 /*
  2 PROG:   Finding crosses
  3 ID  :   
  4 LANG:   C++
  5 */
  6 //#pragma warnning (diaable : 4530)
  7 //#pragma warnning (disable : 4786)
  8 
  9 #include <set>
 10 #include <map>
 11 #include <list>
 12 #include <stack>
 13 #include <queue>
 14 #include <cmath>
 15 #include <string>
 16 #include <vector>
 17 #include <utility>
 18 #include <cmath>
 19 #include <cstdio>
 20 #include <cstdlib>
 21 #include <cstring>
 22 #include <iostream>
 23 #include <fstream>
 24 #include <algorithm>
 25 
 26 using namespace std;
 27 
 28 #define DEBUG 0
 29 
 30 const int MAXN = 52;
 31 const int dx[] = {-1, 0, 1, 0};
 32 const int dy[] = {0, 1, 0, -1};
 33 
 34 int N;
 35 char graph[MAXN][MAXN];
 36 
 37 void readGraph() {
 38     for (int ix = 0; ix < N; ++ix)
 39         scanf("%s", graph[ix]);
 40 }
 41 
 42 bool isNotOkey(int x, int y) {
 43     return (x < 0 || y < 0 || x >= N || y >= N || graph[x][y] != '#');
 44 }
 45 
 46 int countGrids(int x, int y, int dir) {
 47     int ax, ay, ret = 0;
 48     
 49     while (!isNotOkey(x, y)) {
 50         ax = x + dx[(dir + 1) % 4];
 51         ay = y + dy[(dir + 1) % 4];
 52         if (!isNotOkey(ax, ay)) return 0;
 53 
 54         ax = x + dx[(dir + 3) % 4];
 55         ay = y + dy[(dir + 3) % 4];
 56         if (!isNotOkey(ax, ay)) return 0;
 57         
 58         ++ret, x += dx[dir], y += dy[dir];
 59     }// End of while
 60     return ret;
 61 }
 62 
 63 void Solve() {
 64     int cross = 0, grids[4] = {0};
 65     
 66     for (int ix = 0; ix < N; ++ix) {
 67         for (int jx = 0; jx < N; ++jx) {
 68             if (graph[ix][jx] == '#') {
 69                 memset(grids, 0, sizeof(grids));  // record the grids
 70                 for (int kx = 0; kx < 4; ++kx) {
 71                     int nx = ix + dx[kx];
 72                     int ny = jx + dy[kx];
 73                     grids[kx] = countGrids(nx, ny, kx);
 74                 }
 75                 if (grids[0] && grids[0] == grids[1]
 76                     && grids[0] == grids[2] && grids[0] == grids[3]) {
 77                     ++cross;
 78                 }
 79             }// End of if
 80         }
 81     }// End of for
 82     printf("%d\n", cross);
 83 }
 84 
 85 int main() {
 86 #if DEBUG
 87     freopen("E:\\hdoj_4414.in", "r", stdin);
 88     freopen("E:\\hdoj_4414.out", "w", stdout);
 89 #endif
 90 
 91     while (~scanf("%d", &N), N != 0) {
 92         readGraph();
 93         Solve();
 94     }// End of while
 95     return 0;
 96 }
 97 /*
 98 7
 99 ooo#ooo
100 ooo#ooo
101 o######
102 ooo#ooo
103 ooo#ooo
104 ooo#ooo
105 ooooooo
106 0
107 
108 Run ID    Submit Time    Judge Status    Pro.ID    Exe.Time    Exe.Memory    Code Len.    Language    Author
109 6847615    2012-10-01 15:29:57    Accepted    4414    0MS    232K    1833 B    C++    Maxwell
110 */
posted @ 2012-10-01 15:36  Maxwell:My Blog  阅读(303)  评论(0编辑  收藏  举报