bzoj1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛 [BFS]

Description奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草。Farmer John在某个时刻看见贝茜在位置 (R1, C1),,恰好T (0 < T <= 15)秒后,FJ又在位置(R2, C2)与贝茜撞了正着。 FJ并不知道在这T秒内贝茜是否曾经到过(R2, C2),他能确定的只是,现在贝茜在那里。 设S为奶牛在T秒内从(R1, C1)走到(R2, C2)所能选择的路径总数,FJ希望有一个程序来帮他计算这个值。每一秒内,奶牛会水平或垂直地移动1单位距离(奶牛总是在移动,不会在某秒内停在它上一秒所在的点)。草地上的某些地方有树,自然,奶牛不能走到树所在的位置,也不会走出草地。 现在你拿到了一张整块草地的地形图,其中’.’表示平坦的草地,’*’表示挡路的树。你的任务是计算出,一头在T秒内从(R1, C1)移动到(R2, C2)的奶牛可能经过的路径有哪些。Input第1行: 3个用空格隔开的整数:N,M,T 第2..N+1行: 第i+1行为M个连续的字符,描述了草地第i行各点的情况,保证 字符是’.’和’‘中的一个 第N+2行: 4个用空格隔开的整数:R1,C1,R2,以及C2Output第1行: 输出S,含义如题中所述Sample Input4 5 6 …*. …*. ….. ….. 1 3 1 5 输入说明: 草地被划分成4行5列,奶牛在6秒内从第1行第3列走到了第1行第5列。Sample Output1 奶牛在6秒内从(1,3)走到(1,5)的方法只有一种(绕过她面前的树)。Solution

是时候刷一波权限题了。 记步的总方案数,然后宽搜即可。 有一个剪枝,设当前步数为step,如果当前坐标到终点的曼哈顿距离大于就剪掉。但是去掉这个剪枝好像还是0ms。。。

;int n, m, T;char map[105][105];int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};int sx, sy, ex, ey;struct Node{int x, y, step;Node() {}Node(int a, int b, int c) : x(a), y(b), step(c) {}};bool check(int x, int y, int step) {if (x < 0 || x >= n || y < 0 || y >= m) return false;if (map[x][y] == ‘*’) return false;if (abs(ex – x) + abs(ey – y) > T – step) return false;return true;}int tot[105][105][18];bool vis[105][105][18];int bfs() {queue<Node> q;tot[sx][sy][0] = 1;vis[sx][sy][0] = 1;q.push(Node(sx, sy, 0));while (!q.empty()) {Node cur = q.front();q.pop();for (int i = 0; i < 4; i++) {int nx = cur.x + dir[i][0];int ny = cur.y + dir[i][1];if (check(nx, ny, cur.step + 1)) {tot[nx][ny][cur.step + 1] += tot[cur.x][cur.y][cur.step];if (cur.step + 1 < T && !vis[nx][ny][cur.step + 1]) {vis[nx][ny][cur.step + 1] = 1;q.push(Node(nx, ny, cur.step + 1));}}}vis[cur.x][cur.y][cur.step] = 0;}return tot[ex][ey][T];}int main() {scanf(“%d %d %d”, &n, &m, &T);for (int i = 0; i < n; i++)scanf(“%s”, map[i]);scanf(“%d %d %d %d”, &sx, &sy, &ex, &ey);sx–, sy–, ex–, ey–;int res = bfs();printf(“%d\n”, res);return 0;}

天不负;卧薪尝胆,三千越甲可吞吴。

bzoj1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛 [BFS]

相关文章:

你感兴趣的文章:

标签云: