2253Frogger

1.sort函数-----

2.cmpile error-----

3.C++输出指定位数的小数

4.runtime error:

我的代码用了SPFA算法,不过runtime error了,可能是数组越界,死循环。但是我不知道怎么错的

#include "iostream"
#include "queue"
#include "string.h"
#include "algorithm"
#include "cmath"
#include <iomanip>
using namespace std;
struct{
  int x,y;
}stone[210];

struct node{
  int s,e,l;
}longth[20000];
bool cmp(node x,node y){
 return x.l<y.l;
}
int pos=0;
float longest;
int set[20000];

bool SPFA(){
 int i,flag;
 queue<int>Q;
 memset(set,0,sizeof(set));
 Q.push(1);
 set[1]=1;
 while(!Q.empty()){
       flag=Q.front();
       Q.pop();
        for(i=0;i<pos;i++){
           if(longth[i].s==flag&&longth[i].l<=longest&&!set[longth[i].e]){
             Q.push(longth[i].e);
             set[longth[i].e]=1;
           }
        }
    }
  if(set[2])return 1;
  else return 0;
}


int main(){
  int num,i,j,Case=1;
  float d;
  while(cin>>num&&num){
    pos=0;
    for(i=1;i<=num;i++){
    cin>>stone[i].x>>stone[i].y;
    }
    for(i=1;i<num;i++){
      for(j=i+1;j<=num;j++){
        longth[pos].s=i;longth[pos].e=j;longth[pos].l=(stone[i].x-stone[j].x)*(stone[i].x-stone[j].x)+(stone[i].y-stone[j].y)*(stone[i].y-stone[j].y);
        pos++;
        longth[pos].s=j;longth[pos].e=i;longth[pos].l=longth[pos-1].l;
        pos++;
      }
    }
    sort(longth,longth+pos,cmp);
    for(i=0;i<pos;i++){
       longest=longth[i].l;
       if(SPFA()){
           d=sqrt(longest);
           cout<<"Scenario #"<<Case++<<endl<<"Frog Distance = "<<fixed<<setprecision(3)<<d<<endl;goto l1;}
    }
l1:;
  }
}

网上的答案,用了dijkstra算法

#include<iostream>
#include <cmath>
#include <iomanip>
using namespace std;

#define MaxNum 1e8 
int main()
{
    int n, start, tempS;
    bool visited[205];
    double dist[205];
    double  minDist, tempDist;
    int cases = 0;
    struct  
    {
        int x, y;
    }p[205];

    while (cin >> n && n)
    {
        cases++;
        memset(visited, 0, sizeof(bool) * 205);
        fill(&dist[0], &dist[204], MaxNum);

        for (int i = 0; i < n; i++)
            cin >> p[i].x >> p[i].y;

        visited[0] = true;
        dist[0] = 0;
        start = 0;

        for (int i = 0; i < n; i++)
        {
            minDist = MaxNum;
            for (int j = 0; j < n; j++)
            {
                if (!visited[j])
                {
                    tempDist = pow(double(p[start].x - p[j].x), 2);
                    tempDist += pow(double(p[start].y - p[j].y), 2);
                    tempDist = sqrt(tempDist);
                    tempDist = max(tempDist, dist[start]);
                    dist[j] = min(dist[j], tempDist);

                    if (minDist > dist[j])
                    {
                        minDist = dist[j];
                        tempS = j;
                    }
                }
            }
            start = tempS;
            visited[start] = true;
            if (start == 1)
                break;
        }
        if (cases != 1)
            cout << "\n";
        cout << "Scenario #" << cases << endl;
        cout << "Frog Distance = " << fixed << setprecision(3) << dist[1] << endl;
    }
    return 0;
}
posted @ 2013-08-11 22:31  龙城星  阅读(168)  评论(0编辑  收藏  举报