贪心(4)及复习DFS

题目是来自POJ的1328及 1979 常规打卡 不好意思 今天全是WA 想了半天不知道错在哪了? 没调试出来

1979题的代码:#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define Max 50
using namespace std;
int w,h;
char mass[Max][Max];
int sx,sy;
void DFS(int ,int);
int visited[Max][Max];
const int movex[4]={0,0,1,-1};
const int movey[4]={-1,1,0,0};
int sum=0;
int main()
{
int i,j;
while(scanf("%d%d",&w,&h)!=EOF)
{
memset(visited,0,sizeof(visited));
if(w==0&h==0)
{
return 0;
}
for( i=0;i<w;++i)
{
for( j=0;j<h;++j)
{
cin>>mass[i][j];
}
}
for( i=0;i<w;++i)
{
for( j=0;j<h;++j)
{
if(mass[i][j]=='@')
{
sx=i;
sy=j;
visited[sx][sy]=1;
break;
}

}
}
DFS(sx,sy);
int cnt=0;
for( i=0;i<w;++i)
{
for( j=0;j<h;++j)
{
if(visited[i][j])
cnt++;

}
}
printf("%d\n",cnt);
//sum=0;
//sum=0;
}

}
void DFS(int i,int j)
{
visited[i][j]=true;
int x;
int y;
int k;
for(k=0;k<4;k++)
{
x=i+movex[k];
y=j+movey[k];
if(0<=x&&x<w&&0<=y&&y<h&&visited[x][y]==false&&mass[x][y]=='.')
{
visited[x][y]=1;
DFS(x,y);
}
}

}



1328错误的代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define Max 50000
//#define ZERO 1e-8
int n,d;
using namespace std;
pair<double,double>p[Max];
double x,y;
bool cmp(pair<double,double>a,pair<double,double>b){
if(a.second==b.second)return a.first>b.first;
else return a.second<b.second;
}
int main()
{
int k=0;
while(1){
k++;
cin>>n>>d;
if(n==0&&d==0)
{
return 0;
}
int wrong=0;
for(int i=0;i<n;i++)
{
scanf("%lf %lf",&x,&y);
if(y>d)
{
wrong=1;
}
p[i].first=x-sqrt(d*d-y*y);
p[i].second=x+sqrt(d*d-y*y);
}
if(wrong)
{
printf("Case %d: -1",k);
continue;
}
sort(p,p+n,cmp);
int ans=p[0].second;
int cnt=1;
for(int i=1;i<n;i++)
{
if(ans<p[i].first)
{
ans=p[i].second;
cnt++;
}

}
printf("Case %d: %d",k,cnt);

}

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值