POJ 3009 Curling 2.0(简单DFS)
题意:
每一次碰到障碍则在障碍的旁边停下来,并且障碍被击碎。此时可以重新值掷一次冰球。当掷球次数超过 10 次则输出 -1。
思路:
1. 超过 10 次输出 -1 这个剪枝很关键;
2. 主要是要注意些边界条件,初始化的情况,代码最终跑了 250ms,比较差,就不多说了。
#include <iostream>
#include <algorithm>
using namespace std;
const int INFS = 0x3fffffff;
int grid[25][25], row, col, ans;
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
inline bool judge(int x, int y) {
if (0 < x && x <= row && 0 < y && y <= col) {
return true;
}
return false;
}
void dfs(int x, int y, int step) {
if (step > 10)
return ;
for (int i = 0; i < 4; ++i) {
int a = x, b = y, cflag = 0;
while (judge(a, b)) {
a += dir[i][0];
b += dir[i][1];
cflag += 1;
if (grid[a][b] == 3) {
ans = min(ans, step + 1);
return ;
}
if (grid[a][b] == 1)
break ;
}
if (grid[a][b] == 1 && cflag > 1) {
grid[a][b] = 0;
dfs(a-dir[i][0], b-dir[i][1], step+1);
grid[a][b] = 1;
}
}
}
int main() {
while (scanf("%d%d", &col, &row) && col && row) {
int x, y;
memset(grid, 0, sizeof(grid));
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
scanf("%d", &grid[i][j]);
if (grid[i][j] == 2)
x = i, y = j;
}
}
ans = INFS;
dfs(x, y, 0);
if (ans <= 10)
printf("%d\n", ans);
else
printf("-1\n");
}
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------