|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <queue> |
| 8 | +#include <cstring> |
| 9 | +#include <climits> |
| 10 | +#include <cstdio> |
| 11 | +using namespace std; |
| 12 | +typedef pair<int,int> pi; |
| 13 | +typedef vector<int> vi; |
| 14 | +typedef vector<vi> vvi; |
| 15 | +typedef vector<string> vs; |
| 16 | +typedef vector<vs> vvs; |
| 17 | +struct state |
| 18 | +{ |
| 19 | + int x, y, t, b; |
| 20 | + state(int x, int y, int t, int b) : x(x), y(y), t(t), b(b) {} |
| 21 | +}; |
| 22 | +bool operator<(state a, state b){ |
| 23 | + if(a.t != b.t) return a.t > b.t; |
| 24 | + return a.b < b.b; |
| 25 | +} |
| 26 | + |
| 27 | +class BombMan{ |
| 28 | + public: |
| 29 | + int shortestPath(vector <string> maze, int bombs) { |
| 30 | + int n = maze.size(), m = maze[0].size(); |
| 31 | + int dx[4] = {1, 0, -1, 0}; |
| 32 | + int dy[4] = {0, 1, 0, -1}; |
| 33 | + priority_queue<state> Q; |
| 34 | + int bx = 0, by = 0, ex = 0, ey = 0; |
| 35 | + for(int i = 0; i < n; i++) |
| 36 | + for(int j = 0; j < m; j++) |
| 37 | + if(maze[i][j] == 'B'){ |
| 38 | + bx = i; |
| 39 | + by = j; |
| 40 | + } |
| 41 | + else if(maze[i][j] == 'E'){ |
| 42 | + ex = i; |
| 43 | + ey = j; |
| 44 | + } |
| 45 | + cout<<"bx = "<<bx<<" by = "<<by<<endl; |
| 46 | + cout<<"ex = "<<ex<<" ey = "<<ey<<endl; |
| 47 | + |
| 48 | + vvi visited(n, vi(m, 0)); |
| 49 | + Q.push(state(bx, by, 0, bombs)); |
| 50 | + while(!Q.empty()){ |
| 51 | + state p = Q.top(); |
| 52 | + Q.pop(); |
| 53 | + int x = p.x, y = p.y, t = p.t, b = p.b; |
| 54 | + if(visited[x][y]) continue; |
| 55 | + // cout<<x<<" "<<y<<" "<<t<<" "<<b<<endl; |
| 56 | + if(x == ex && y == ey) return t; |
| 57 | + visited[x][y] = 1; |
| 58 | + for(int k = 0; k < 4; k++){ |
| 59 | + int xx = x+dx[k], yy = y+dy[k]; |
| 60 | + if(0 <= xx && xx < n && 0 <= yy && yy < m){ |
| 61 | + if(maze[xx][yy] == '#'){ |
| 62 | + if(b > 0) Q.push(state(xx, yy, t+3, b-1)); |
| 63 | + } |
| 64 | + else Q.push(state(xx, yy, t+1, b)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | +// BEGIN CUT HERE |
| 72 | + public: |
| 73 | + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } |
| 74 | + private: |
| 75 | + template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
| 76 | + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
| 77 | + void test_case_0() { string Arr0[] = {".....B.", |
| 78 | + ".#####.", |
| 79 | + ".#...#.", |
| 80 | + ".#E#.#.", |
| 81 | + ".###.#.", |
| 82 | + "......."} |
| 83 | +; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 8; verify_case(0, Arg2, shortestPath(Arg0, Arg1)); } |
| 84 | + void test_case_1() { string Arr0[] = {"B.#.#.#...E"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = -1; verify_case(1, Arg2, shortestPath(Arg0, Arg1)); } |
| 85 | + void test_case_2() { string Arr0[] = {"..#####", |
| 86 | + "B.#####", |
| 87 | + "..#####", |
| 88 | + "#######", |
| 89 | + "####...", |
| 90 | + "####.E."} |
| 91 | +; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 17; verify_case(2, Arg2, shortestPath(Arg0, Arg1)); } |
| 92 | + void test_case_3() { string Arr0[] = {".#.#.#.#B#...#.#...#.#...#.#...#.#...#.#.#.......", |
| 93 | + ".#.#.#.#.#.###.###.#.###.#.#.###.###.#.#.#.###.##", |
| 94 | + ".#.#.#...#.#.#.#.#.#...#.....#.#.#...#...#.#.#...", |
| 95 | + ".#.#.###.#.#.#.#.#.###.#.#####.#.###.###.#.#.###.", |
| 96 | + ".............#.#...#...#.....#.#.#...#.#.#.....#.", |
| 97 | + "##.#######.###.#.#####.#.#####.#.###.#.#.#.#.####", |
| 98 | + ".#.#.....#...#...#.#...#...#.#.#...#...#...#.....", |
| 99 | + ".#######.#.#####.#.#.#.#.###.#.###.#.#####.#.####", |
| 100 | + ".#.#.#.#...#.#.#.#.#.#.......#...#.#...#.#.#.....", |
| 101 | + ".#.#.#.###.#.#.#.#.#####.#####.###.###.#.#.######", |
| 102 | + ".....#...#.#...#...#...#...#...#...#.#.#.........", |
| 103 | + "####.###.#.###.###.#.###.#.#.###.###.#.#.########", |
| 104 | + ".......#.........#.#.#.#.#.#.#.#.........#...#...", |
| 105 | + ".#.###.#########.#.#.#.#.###.#.#####.#.#.#.###.##", |
| 106 | + ".#.#.........#.#.#.#.#.....#.#.#.....#.#.........", |
| 107 | + "############.#.#.#.#.#.#####.#.#.################", |
| 108 | + ".#...........#...#.#.#.#...#.#.#...#.#.#.....#...", |
| 109 | + ".#####.#####.###.#.#.#.#.###.#.#.###.#.#.#####.##", |
| 110 | + ".......#...#.#.#.....#...#...#.#.#.#.#...........", |
| 111 | + "##########.#.#.#####.#.###.###.#.#.#.#.##########", |
| 112 | + ".....#...#.....#.#...#.......#.#...#.......#.....", |
| 113 | + "##.#.###.#.###.#.#.#.#.#####.#.#.###.#######.####", |
| 114 | + "...#...#...#.......#.....#.#...#...#.......#.....", |
| 115 | + "####.#.#.#########.#.###.#.#####.###.#.#######.##", |
| 116 | + ".#...#...#.........#.#.....#.........#.#.#.#.....", |
| 117 | + ".#####.#.#.###.#######.#.###.#.#########.#.#.####", |
| 118 | + ".......#.#.#...#.......#.....#.#.#.......#.#.#.#.", |
| 119 | + "########.#.#.#.#####.#.###.#.###.#.#######.#.#.#.", |
| 120 | + ".........#.#.#.#.....#...#.#.........#.#.........", |
| 121 | + "################.#.#.#.#.#.#.#.#######.#.########", |
| 122 | + ".................#.#.#.#.#.#.#...........#.......", |
| 123 | + "########.#####.#.###.#.#.#####.###.#.#####.###.##", |
| 124 | + ".........#...#.#...#.#.#...#.....#.#.........#...", |
| 125 | + ".#####.#####.#.###.#.###.#.#.#.#.#.#####.#.###.#.", |
| 126 | + ".#.....#.........#.#.#...#.#.#.#.#.#.....#...#.#.", |
| 127 | + "####.#####.###.#.#.#.#.#.#.###.###.#.#.#.#.#####.", |
| 128 | + ".....#.....#.#.#.#.#.#.#.#.#...#...#.#.#.#...#...", |
| 129 | + "####.#.#.###.#.#.###.#.###.#.#.#####.#.#.#.######", |
| 130 | + ".....#.#.#.#...#...#.#...#.#.#...#...#.#.#.......", |
| 131 | + "##########.#.#.#.#####.###.#.#.###.#.###.#####.##", |
| 132 | + "...#.#...#...#.#.....#.#...#.#...#.#.#.......#...", |
| 133 | + ".#.#.#.#.#.#.#.#.#.#.###.#.#########.###.#.#.#.#.", |
| 134 | + ".#.#...#...#.#.#.#.#...#.#...#.......#...#.#.#.#.", |
| 135 | + "##.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.########", |
| 136 | + ".......#.#...#.#.#.#.#.#.....#.#.#...#.#.........", |
| 137 | + "####.#######.#.#####.#.###.#.#.###.#.#.#.########", |
| 138 | + "E..#.......#.#.....#.#.#.#.#.#.#...#.#.#.........", |
| 139 | + "##.#.#.#.###.###.###.###.#.#.###.#.#.#.#.#######.", |
| 140 | + ".....#.#...#.#.....#.#.....#...#.#.#.#.#.....#..."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 76; verify_case(3, Arg2, shortestPath(Arg0, Arg1)); } |
| 141 | + |
| 142 | +// END CUT HERE |
| 143 | + |
| 144 | +}; |
| 145 | + |
| 146 | +// BEGIN CUT HERE |
| 147 | +int main(){ |
| 148 | + |
| 149 | + BombMan ___test; |
| 150 | + ___test.run_test(-1); |
| 151 | +} |
| 152 | +// END CUT HERE |
0 commit comments