您的位置:首页 > 其它

2017上海市高校程序设计邀请赛_C

2017-07-03 12:19 204 查看
problem list

C 神奇怪兽在哪里



签到题

要求很宽泛,路径可以重叠

dfs遍历解决

#include <bits/stdc++.h>
using namespace std;
typedef long long           LL ;
typedef unsigned long long ULL ;
const int    maxn = 1000 + 10  ;
const int    inf  = 0x3f3f3f3f ;
const int    npos = -1         ;
const double eps  = 1e-20      ;

int n, m, si, sj, a[maxn][maxn], v[maxn][maxn];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
char c[maxn], d[4]={'U','L','D','R'};
bool bound(int x, int y){
return (0<x)&&(x<=n)&&(0<y)&&(y<=m);
}
void dfs(int x, int y){
v[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(bound(nx,ny) && !a[nx][ny] && !v[nx][ny]){
printf("%c",d[i]);
dfs(nx,ny);
printf("%c",d[(i+2)%4]);
}
}
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d %d",&n,&m)){
for(int i=1;i<=n;i++){
scanf("%s",c+1);
for(int j=1;j<=m;j++){
a[i][j]=(c[j]=='*');
v[i][j]=0;
if('P'==c[j]){
si=i;
sj=j;
}
}
}

dfs(si,sj);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐