涉及到的图的题目不会,贴个别人代码先过了,晕,这几天真是烦死了。
/* ID: hubiao cave PROG: prime3 LANG: C++ */ #include <fstream> #include <iostream> #include <cmath> #include <memory.h> using namespace std; const int MAX = 99999; const int DIV[6] = {0,10000, 1000, 100, 10, 1}; bool primes[MAX]; int n, sum; int prime1[10][1000][6]; int nprime1[10]; int prime3[10][1000][6]; int nprime3[10]; int prime15[10][10][100][6]; int nprime15[10][10]; int prime124[10][10][10][100][6]; int nprime124[10][10][10]; void dfs1(); void dfs2(); void dfs3(); void dfs4(); void dfs5(); void dfs6(); void dfs7(); ifstream fin("prime3.in"); ofstream fout("prime3.out"); void addPrime(int s){ int i, k=1, tmp[6]; int num = s; while(num!=0){ tmp[k] = num/DIV[k]; num = num%DIV[k]; k++; } int c =0; for(i=1; i<=5; ++i) c += tmp[i]; if(c == sum){ primes[s]=true; for(i=1; i<=5; ++i){ int t = nprime1[tmp[1]]; prime1[tmp[1]][t][i] = tmp[i]; } nprime1[tmp[1]]++; for(i=1; i<=5; ++i){ int t = nprime3[tmp[3]]; prime3[tmp[3]][t][i] = tmp[i]; } nprime3[tmp[3]]++; bool flag=true; for(i=1; i<=5; ++i){ if(tmp[i]==0){ flag=false; break; } int t = nprime15[tmp[1]][tmp[5]]; prime15[tmp[1]][tmp[5]][t][i] = tmp[i]; } if(flag) nprime15[tmp[1]][tmp[5]]++; for(i=1; i<=5; ++i){ int t = nprime124[tmp[1]][tmp[2]][tmp[4]]; prime124[tmp[1]][tmp[2]][tmp[4]][t][i] = tmp[i]; } nprime124[tmp[1]][tmp[2]][tmp[4]]++; } } int map[6][6]; int ans[1000][6][6]; int cnt=0; bool inline larger_than(int a[6][6], int b[6][6]){ for(int i=1; i<=5; ++i) for(int j=1; j<=5; ++j) if(a[i][j]>b[i][j]) return true; else if(a[i][j]<b[i][j]) return false; else continue; return false; } void addResult(){ int p = ++cnt; while(p!=0 && larger_than(ans[p-1], map)){ memcpy(ans[p], ans[p-1], sizeof(int)*36); p--; } memcpy(ans[p], map, sizeof(int)*36); } int isPrime(int a){ int n = static_cast<int>(sqrt(a)); for(int i=2; i<=n; ++i) if(!(a%i)) return 0; return 1; } int d=0; void initPrime(){ for(int i=10001; i<=99999; ++i) if(isPrime(i)) addPrime(i); } bool check1(int r){ int i,sum=0; for(i=1; i<=5; ++i) sum = sum*10+map[r][i]; if(!primes[sum]) return false; return true; } bool check2(int c){ int i,sum=0; for(i=1; i<=5; ++i) sum = sum*10+map[i][c]; if(!primes[sum]) return false; return true; } void dfs1(){ int t = nprime1[map[1][1]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=2; j<=5; ++j) map[j][j] = prime1[map[1][1]][i][j]; dfs2(); } } void dfs2(){ int t = nprime3[map[3][3]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=1; j<=5; ++j) map[6-j][j] = prime3[map[3][3]][i][j]; dfs3(); } } void dfs3(){ int t = nprime15[map[1][1]][map[1][5]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=2; j<=4; ++j) map[1][j] = prime15[map[1][1]][map[1][5]][i][j]; dfs4(); } } void dfs4(){ int t = nprime124[map[1][2]][map[2][2]][map[4][2]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=3; j<=5; ++j) map[j][2] = prime124[map[1][2]][map[2][2]][map[4][2]][i][j]; dfs5(); } } void dfs5(){ int t = nprime124[map[1][4]][map[2][4]][map[4][4]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=3; j<=5; ++j) map[j][4] = prime124[map[1][4]][map[2][4]][map[4][4]][i][j]; for(int k=1; k<=9; k+=2){ map[5][3]=k; if(check1(5)) dfs6(); } } } void dfs6(){ int t = nprime15[map[1][1]][map[5][1]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=2; j<=4; ++j) map[j][1] = prime15[map[1][1]][map[5][1]][i][j]; for(int k=1; k<=9; k+=2){ map[3][5] = k; if(check1(3)) dfs7(); } } } void dfs7(){ int t = nprime124[map[2][1]][map[2][2]][map[2][4]]; if(t==0) return; else for(int i=0; i<t; ++i){ for(int j=3; j<=5; ++j) map[2][j] = prime124[map[2][1]][map[2][2]][map[2][4]][i][j]; for(int a=0; a<=9; ++a){ map[4][3]=a; if(check2(3)) for(int b=1; b<=9; b+=2){ map[4][5] = b; if(check1(4) && check2(5) ) addResult(); } } } } void output(){ for(int k=0; k<cnt-1; ++k){ for(int i=1; i<=5; ++i){ for(int j=1; j<=5; ++j) fout<<ans[k][i][j]; fout<<endl; } if(cnt==1) return; fout<<endl; } for(int i=1; i<=5; ++i){ for(int j=1; j<=5; ++j) fout<<ans[cnt-1][i][j]; fout<<endl; } } int main(){ //input fin>>sum>>map[1][1]; //init for(int i=1; i<=5; ++i) for(int j=1; j<=5; ++j) ans[0][i][j] = 9; initPrime(); //solve dfs1(); //output output(); return 0; }
#include <iostream> #include <fstream> #define MAX 101 using namespace std; ifstream fi("race3.in"); ofstream fo("race3.out"); int adjl[MAX][MAX]; int ans[MAX][2]; bool used[MAX],tused[MAX]; int N,start,end; void init() { int a=0,i=0; while (a!=-1) { fi >> a; while (a>=0) { adjl[i][ ++adjl[i][0] ]=a; used[a]=true; fi >> a; } i++; } N=i-2; for (i=0;i<=N;i++) { if (adjl[i][0]==0) end=i; if (!used[i]) start=i; } } void dfs3(int i) { int k,j; for (k=1;k<=adjl[i][0];k++) { j=adjl[i][k]; if (!tused[j]) { tused[j]=true; dfs3(j); } } } void dfs2(int i) { int k,j; for (k=1;k<=adjl[i][0];k++) { j=adjl[i][k]; if (!used[j]) { used[j]=true; dfs2(j); } } } void question2() { int i,j,k; for (i=1;i<=N-1;i++) { memset(used,0,sizeof(used)); memset(tused,0,sizeof(tused)); dfs2(i); tused[i]=true; tused[0]=true; dfs3(0); k=1; for (j=0;j<=N;j++) if (j!=i && used[j] && tused[j]) { k=0; break; } if (k) ans[ ++ans[0][1] ][1]=i; } } void dfs1(int i) { int k,j; for (k=1;k<=adjl[i][0];k++) { j=adjl[i][k]; if (!used[j]) { used[j]=true; dfs1(j); } } } void question1() { int i; for (i=1;i<=N-1;i++) { memset(used,0,sizeof(used)); used[i]=true; dfs1(0); if (!used[N]) ans[ ++ans[0][0] ][0]=i; } } void print() { int i; fo << ans[0][0]; for (i=1;i<=ans[0][0];i++) fo <<' ' << ans[i][0]; fo << endl; fo << ans[0][1]; for (i=1;i<=ans[0][1];i++) fo <<' ' << ans[i][1]; fo << endl; fi.close(); fo.close(); } int main() { init(); question1(); question2(); print(); return 0; }