Skip to content

Commit

Permalink
fix BUAAOJ237 again
Browse files Browse the repository at this point in the history
  • Loading branch information
GoatGirl98 committed Jan 13, 2024
1 parent 8673d77 commit 720d929
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ void dfs(int x, int y, int st_x, int st_y, int ed_x, int ed_y, int turn, int dir
flag = 1;
if (flag)
return;
if (turn == 2) // important prune : can't turn anymore and couldn't get to goal
{
if (x != ed_x && y != ed_y)
return;
if (x == ed_x)
if (!((y > ed_y && dir == 1) || (y < ed_y && dir == 3)))
return;
if (y == ed_y)
if (!((x > ed_x && dir == 0) || (x < ed_x && dir == 2)))
return;
}
if ((x != st_x || y != st_y) && a[x][y])
return;
for (int i = 0; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
// prune : dir can't turn 180 degree
if (!(dir >= 0 && (i - dir == 2 || dir - i == 2)) && inside(nx, ny))
dfs(nx, ny, st_x, st_y, ed_x, ed_y, (dir < 0 || dir == i) ? turn : turn + 1, i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ YES

## 思路

不超过2步,DFS就完事了
此题搬运自 HDU1175, 不超过2步,DFS就完事了。但是此题的数据不够强,很多可以优化的细节并不能体现出来。

需要注意,dfs时要么判断当前数是否访问过,要么就要判断不能直接掉头(比如上一步是向上走这一步就向下走),这样的话就不需要一个访问数组了,因为转完两次是不可能掉头回到已经访问过的数的。
首先在下一步dfs时判断不能直接掉头(比如上一步是向上走这一步就向下走),这样的话就不需要一个访问数组了,因为转完两次是不可能掉头回到已经访问过的数的。另外最关键的一个剪枝在于已经转弯过两次,当前的横纵坐标没有一个和终点一致,或者横纵坐标之一和终点一致但是方向反了,都需要直接剪枝。这样可以在原题链接中将5000多毫秒运行时间直接剪枝到40多毫秒。感兴趣的可以去 [这里](http://acm.hdu.edu.cn/showproblem.php?pid=1175) 提交一下

# `J` microhhh的回文词(难)

Expand Down

0 comments on commit 720d929

Please sign in to comment.