3126Prime Path

怎么今天都是compile error 呢,奇怪啊

我的代码

#include "iostream"
#include "string.h"
#include "queue"
#include "math.h"
#include "algorithm"
using namespace std;
struct Point{
  char list[5];
  int step;
};
int chang(char list[5]){
  int i,total=0;
  for(i=0;i<4;i++){
    int a=(int)(list[i]-'0');
    int b=pow(10,3-i);
    total+=a*b;
  }
  return total;
}
void chang1(int a,char list[5]){
  for(int i=3;i>=0;i--){
    list[i]='0'+a%10;
    a=a/10;
  }
  list[4]='\0';
}
int main(){
  int set[10000],num[10000],i,j,ncase,set1[10000];
  char list1[5],list2[5];
  memset(set,0,sizeof(set));
  memset(set1,0,sizeof(set1));
  for(i=2;i<10000;i++){
    if(set[i]==0){
     for(j=2*i;j<10000;j=j+i){
      set[j]=1;
     }
    }
  }
  cin>>ncase;
  while(ncase--){
    int a,b,flag=0;
    cin>>a>>b;
    chang1(a,list1);
    chang1(b,list2);
    //cout<<list1<<' '<<list2<<endl;system("pause");
    Point pt,tem;
    strcpy(pt.list,list1);pt.step=0;
    queue<Point> q;
    q.push(pt);
    memset(set1,0,sizeof(set1));
    while(!q.empty()){
      tem=q.front();
      if(strcmp(tem.list,list2)==0){cout<<tem.step<<endl;flag=1;break;}
      //cout<<tem.list<<endl;
      q.pop();
      for(i=0;i<4;i++){
        if(i==0)j=1;
        else j=0;
        for(;j<10;j++){
          Point ans;
          int total;
          strcpy(ans.list,tem.list);
          ans.list[i]='0'+j;
          total=chang(ans.list);
          if(set1[total])continue;
          set1[total]=1;
          ans.step=tem.step+1;
          //cout<<"***"<<ans.list<<' '<<set[total]<<endl;system("pause");
          if(set[total]==0){
            q.push(ans);
          }
        }
      }
    }
   if(!flag)cout<<"Impossible"<<endl;
  }
}

人家的代码

// POJ3126.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <queue>
using namespace std;

int main(int argc, char* argv[])
{
    //init prime table
    bool prime[10001];
    memset(prime,1,sizeof (prime));
    prime[0] = false;prime[1] = false;
    for (int i = 2; i < 5000; ++i)
    {
        int k = 2;
        int next;
        while((next = (k++) * i) < 10000)
            prime[next] = false;
    }

    int cases;
    cin >> cases;
    int beg, end;
    int step;
    int visited[10001];
    int base[4]= {1, 10, 100, 1000};
    for (int c = 0; c < cases; ++c)
    {
        scanf("%d %d", &beg, &end);
        memset(visited, -1, sizeof(visited));
        queue<int> q;
        q.push(beg);
        visited[beg] = 0;
        step = -1;

        while (!q.empty())
        {
            int cur = q.front();
            q.pop();
            if (cur == end)
            {
                step = visited[cur];
                break;
            }
            
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    int l = cur / (base[i] * 10);
                    int r = cur % base[i];
                    int num = l * base[i] * 10 + j * base[i] + r;
                    if (num > 1000 && num != cur && prime[num] == true && visited[num] == -1)
                    {
                        q.push(num);
                        visited[num] = visited[cur] + 1;
                    }
                }
            }
        }

        if (step == -1)cout << "Impossible\n";
        else cout << step << endl;
    }

    return 0;
}

 

 

 

posted @ 2013-09-30 18:54  龙城星  阅读(243)  评论(0编辑  收藏  举报