您的位置:首页 > 其它

HDU-1312-Red and Black

2016-04-01 12:20 337 查看

HDU-1312-Red and Black

题目链接:HDU-1312

题目大意:‘@’代表人其实位置,‘.’代表可走格子,‘#’代表‘墙’。问这个人最多能走几个格子。可以走的方向有(上、下、左、右)四个方向。

题目思路:DFS,从‘@’开始搜,如果是可走位置继续搜,如果碰到墙则停止该条路的搜索。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
string s[25];
int vis[25][25];
int w,h;
int ans = 0;
void dfs(int c,int r)
{
if (c >= h || c < 0 || r >= w || r < 0) return;
if (vis[c][r] || s[c][r] == '#') return;
vis[c][r] = 1;
ans++;
dfs(c + 1,r);
dfs(c - 1,r);
dfs(c,r + 1);
dfs(c,r - 1);
}
int main(){
while(cin >> w >> h)
{
if (w == 0 && h == 0) break;
int poi = -1,poj = -1;
ans = 0;
memset(vis,0,sizeof(vis));
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> s[i][j];
if (s[i][j] == '@') poi = i,poj = j;
}
}
dfs(poi,poj);
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs HDU 1312