USACO Section 2.4: Overfencing

这题因为各种琐事耽耽搁搁做了2天,也出了挺多错误,最后出了一个结论:像这种有对neighbor有通路的图形用一个4个位表示4个方向的int进行位运算比较靠谱。

  1 /*

  2 ID: yingzho1

  3 LANG: C++

  4 TASK: maze1

  5 */

  6 #include <iostream>

  7 #include <fstream>

  8 #include <string>

  9 #include <map>

 10 #include <vector>

 11 #include <set>

 12 #include <algorithm>

 13 #include <stdio.h>

 14 #include <queue>

 15 #include <cstring>

 16 #include <cmath>

 17 #include <list>

 18 

 19 using namespace std;

 20 

 21 ifstream fin("maze1.in");

 22 ofstream fout("maze1.out");

 23 

 24 int W, H;

 25 string board[220];

 26 

 27 struct node {

 28     int x, y;

 29     int neigh;

 30     int dis;

 31     node(int a, int b, int c, int d) : x(a), y(b), neigh(c), dis(d) { }

 32     node() : x(0), y(0), neigh(0), dis(4400) { }

 33 } maps[110][40];

 34 

 35 int dfs(node source) {

 36     int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

 37     queue<node> S, T;

 38     S.push(source);

 39     int account = 1;

 40     vector<vector<bool> > visit(H+1, vector<bool>(W+1));

 41     visit[source.x][source.y] = true;

 42     while (!S.empty()) {

 43         while (!S.empty()) {

 44             node tmp = S.front();

 45             S.pop();

 46             for (int i = 0; i < 4; i++) {

 47                 if ((tmp.neigh >> i) & 1 == 1) {

 48                     int xx = tmp.x + dir[i][0];

 49                     int yy = tmp.y + dir[i][1];

 50                     if (xx <= 0 || xx > H || yy <= 0 || yy > W) continue;

 51                     if (!visit[xx][yy]) {

 52                         T.push(maps[xx][yy]);

 53                         maps[xx][yy].dis = min(maps[xx][yy].dis, account+1);

 54                         visit[xx][yy] = true;

 55                     }

 56                 }

 57             }

 58         }

 59         if (!T.empty()) account++;

 60         swap(S, T);

 61     }

 62     return account;

 63 }

 64 

 65 int main()

 66 {

 67     fin >> W >> H;

 68     string tmp;

 69     vector<node> exits;

 70     getline(fin, tmp);

 71     for (int i = 0; i < 2*H+1; i++) {

 72         getline(fin, board[i]);

 73         if (board[i].size() < 2*W+1) board[i] += string(2*W+1-board[i].size(), ' ');

 74     }

 75 

 76     for (int i = 1; i <= H; i++) {

 77         for (int j = 1; j <= W; j++) {

 78             maps[i][j].x = i, maps[i][j].y = j;

 79             if (board[2*i-1-1][2*j-1] != '-') {

 80                 maps[i][j].neigh |= 1; //N 0001

 81                 if (i == 1) exits.push_back(maps[i][j]);

 82             }

 83             if (board[2*i-1+1][2*j-1] != '-') {

 84                 maps[i][j].neigh |= 2; // S 0010

 85                 if (i == H) exits.push_back(maps[i][j]);

 86             }

 87             if (board[2*i-1][2*j-1-1] != '|') {

 88                 maps[i][j].neigh |= 4; // W 0100

 89                 if (j == 1) exits.push_back(maps[i][j]);

 90             }

 91             if (board[2*i-1][2*j-1+1] != '|') {

 92                 maps[i][j].neigh |= 8; // E 1000

 93                 if (j == W) exits.push_back(maps[i][j]);

 94             }

 95         }

 96     }

 97     maps[exits[0].x][exits[0].y].dis = 1, maps[exits[1].x][exits[1].y].dis = 1;

 98     dfs(maps[exits[0].x][exits[0].y]);

 99     dfs(maps[exits[1].x][exits[1].y]);

100     int maxdis = 1;

101     for (int i = 1; i <= H; i++) {

102         for (int j = 1; j <= W; j++) {

103             maxdis = max(maxdis, maps[i][j].dis);

104         }

105     }

106     fout << maxdis << endl;

107 

108     return 0;

109 }

 

你可能感兴趣的:(USACO)