码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1010: Tempter of the Bone

时间:2017-08-18 14:36:09      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:ios   return   ack   auth   hdu   store   href   while   tor   


///@link http://acm.hdu.edu.cn/showproblem.php?pid=1010
///@author Sycamore
///@date Aug, 18

//fast DFS with pruning
#include<bits/stdc++.h>

using namespace std;
struct coord
{
int x, y;
} s, d;
int n, m, t, cnt, dir[4][2] = {{1, 0},
{-1, 0},
{0, 1},
{0, -1}};
char maze[7][7];

bool in_maze(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
}

bool dfs(coord cur, int dep)
{
//cout<<cur.x<<‘ ‘<<cur.y<<‘ ‘<<dep<<endl; debug

//base cases
if (dep == t && cur.x == d.x && cur.y == d.y)return true;
if (dep >= t)return false;

for (int i = 0; i < 4; i++)
{
if (in_maze(cur.x + dir[i][0], cur.y + dir[i][1]))
if (maze[cur.x + dir[i][0]][cur.y + dir[i][1]] != ‘X‘)
{
maze[cur.x][cur.y] = ‘X‘;//avoids revisits
if (dfs(coord{cur.x + dir[i][0], cur.y + dir[i][1]}, dep + 1))
return true;
maze[cur.x][cur.y] = ‘.‘;//restores the path
}

}
return false;
}

void solve()
{
if (((abs(s.x - d.x) + abs(s.y - d.y)) & 1) != (t & 1) || cnt < t)
{
cout << "NO\n";
return;
}
if (dfs(coord{s.x, s.y}, 0))cout << "YES\n";
else cout << "NO\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n >> m >> t && (n || m || t))
{
cnt = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> maze[i][j];
if (maze[i][j] == ‘S‘)s = coord{i, j};
else if (maze[i][j] == ‘D‘)d = coord{i, j};
else if (maze[i][j] == ‘.‘)cnt++;
}

solve();
}

return 0;
}

 

HDU 1010: Tempter of the Bone

标签:ios   return   ack   auth   hdu   store   href   while   tor   

原文地址:http://www.cnblogs.com/zjnu/p/7389078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!