復健-搜索(10/14)
代碼水平下降嚴重,注意:Simple is Beautiful
10/14
缺:POJ 3984 HDU 1241 HDU 1495 HDU 2612
---POJ1321(n皇后衍生)
模板題 1A
1 #define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 10 32 char a[maxn][maxn]; 33 int n,k,sum; 34 int vis[maxn]; 35 void dfs(int depth,int num){ 36 if(n-depth+1<num) return; 37 if(num==0) {sum++; return;} 38 FOR(i,1,n) 39 if(a[depth][i]=='#'&&vis[i]==0) {vis[i]=1;dfs(depth+1,num-1);vis[i]=0;} 40 dfs(depth+1,num); 41 return; 42 } 43 int main(){ 44 #ifdef LOCAL 45 freopen("input.txt","r",stdin); 46 freopen("output.txt","w",stdout); 47 #endif 48 while(true){ 49 50 cin>>n>>k; 51 //cout<<n<<k; 52 if(n==-1&&k==-1) break; 53 FOR(i,1,n) 54 FOR(j,1,n) 55 cin>>a[i][j]; 56 FOR(i,1,n) 57 vis[i]=0; 58 sum=0; 59 dfs(1,k); 60 cout<<sum<<endl; 61 } 62 #ifdef LOCAL 63 fclose(stdin); 64 fclose(stdout); 65 #endif 66 //system(pause); 67 return 0; 68 }
改進:
定義個char[][]然後讀入時候可以直接%s,char[]。一次讀一行
---POJ2251(三維走迷宮)
模板題 3OLE 1A
*「將bfs的深度與棧分離,以便進行剪枝,用vis儲存(要初始化區別0與未賦值!!!),一舉兩用」
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 40 32 #define maxm 40 33 #define maxd 40 34 char a[maxd][maxn][maxm]; 35 //int vis[maxd][maxn][maxm]; 36 int dd[]={0,0,0,0,1,-1}; 37 int di[]={1,-1,0,0,0,0}; 38 int dj[]={0,0,1,-1,0,0}; 39 int n,m,k,si,sj,sd,ans; 40 bool flag; 41 struct dot{ 42 int d1,d2,d3,d4; 43 }; 44 void bfs(int de,int i,int j){ 45 queue<dot> Q; 46 dot kkk; 47 kkk.d1=de; 48 kkk.d2=i; 49 kkk.d3=j; 50 kkk.d4=0; 51 Q.push(kkk); 52 int xx,yy,zz; 53 int xx1,yy1,zz1,dd1; 54 while(!Q.empty()){ 55 xx1=(Q.front()).d1; 56 yy1=(Q.front()).d2; 57 zz1=(Q.front()).d3; 58 dd1=(Q.front()).d4; 59 Q.pop(); 60 FOR(i,0,5){ 61 xx=xx1+dd[i]; 62 yy=yy1+di[i]; 63 zz=zz1+dj[i]; 64 if(1<=xx&&xx<=k) 65 if(1<=yy&&yy<=n) 66 if(1<=zz&&zz<=m){ 67 if(a[xx][yy][zz]=='E'){flag=true;ans=dd1+1;return;} 68 if(a[xx][yy][zz]=='.'){ 69 a[xx][yy][zz]='#'; 70 kkk.d1=xx; 71 kkk.d2=yy; 72 kkk.d3=zz; 73 kkk.d4=dd1+1; 74 //cout<<xx<<' '<<yy<<' '<<zz<<' '<<dd1+1<<endl; 75 //0system("pause"); 76 Q.push(kkk); 77 } 78 } 79 } 80 } 81 return; 82 } 83 int main(){ 84 #ifdef LOCAL 85 freopen("input.txt","r",stdin); 86 freopen("output.txt","w",stdout); 87 #endif 88 char sp; 89 while(true){ 90 //scanf("%d%d%d",&k,&n,&m); 91 cin>>k>>n>>m; 92 if(k==0) return 0; 93 //cout<<'*'<<k<<n<<m; 94 FOR(p,1,k){ 95 FOR(i,1,n) 96 FOR(j,1,m) 97 {/*vis[p][i][j]=0;*//*scanf("%c",&a[p][i][j]);*/cin>>a[p][i][j]; if(a[p][i][j]=='S'){sd=p;si=i;sj=j;}} 98 //cin>>sp; 99 } 100 flag=false; 101 a[sd][si][sj]='#'; bfs(sd,si,sj); 102 //if(flag) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); 103 if(flag) 104 cout << "Escaped in " << ans << " minute(s)." << endl; 105 else 106 cout << "Trapped!" << endl; 107 } 108 #ifdef LOCAL 109 fclose(stdin); 110 fclose(stdout); 111 #endif 112 //system(pause); 113 return 0; 114 }
改進:輸入輸出一律同標準,最好scanf
---POJ 3278(一個遞歸)
加强模板 1TLE 3MLE 4WA 1A
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define maxn 100010 23 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 24 // X_INIT=0 mem_Macro 25 #define INF (1<<29) 26 #define MOD 1000000007ll 27 #define FOR(i,j,k) for(int i=j;i<=k;i++) 28 #define FORD(i,j,k) for(int i=j;i>=k;i--) 29 #define LL long long 30 #define SZ(x) int(x.size()) 31 #define pb push_back 32 //int a[maxn]; 33 int n,k; 34 int vis[maxn]; 35 void bfs(){ 36 queue< pair<int,int> > Q; 37 pair<int,int> kkk; 38 kkk.first=n; 39 kkk.second=0; 40 Q.push(kkk); 41 while(true){ 42 int o=(Q.front()).first; 43 int p=(Q.front()).second; 44 Q.pop(); 45 if(!vis[o]){ 46 vis[o]=1; 47 if(o==k){cout<<p; return;} 48 if(o*2<=maxn){kkk.first=o*2;kkk.second=p+1; Q.push(kkk);} 49 if(o+1<=maxn){kkk.first=o+1;kkk.second=p+1; Q.push(kkk);} 50 if(o-1>=0){kkk.first=o-1;kkk.second=p+1; Q.push(kkk);}} 51 } 52 return; 53 } 54 int main(){ 55 #ifdef LOCAL 56 freopen("input.txt","r",stdin); 57 freopen("output.txt","w",stdout); 58 #endif 59 cin>>n>>k; 60 bfs(); 61 #ifdef LOCAL 62 fclose(stdin); 63 fclose(stdout); 64 #endif 65 //system(pause); 66 return 0; 67 }
改進:先要確定我們需要剪枝的是那一部分,再下if 而這題我們需要剪掉的是賦值部分,應在賦值部分對vis[]進行判斷
*偶然看到的神仙代碼:https://vjudge.net/solution/22398005
---POJ 3279(奶牛黑白棋)
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 20 32 int m,n; int ansx; 33 int dx[]={-1,1,0,0,0}; 34 int dy[]={0,0,-1,1,0}; 35 bool a[maxn][maxn],c[maxn][maxn]; 36 bool tmp[maxn]; 37 int tmp2[maxn][maxn]; 38 int sum,ans; 39 void color(int xx,int yy){ 40 int xxx,yyy; 41 FOR(i,0,4){ 42 xxx=xx+dx[i]; 43 yyy=yy+dy[i]; 44 if(1<=xxx&&xxx<=m) 45 if(1<=yyy&&yyy<=n) 46 {if(a[xxx][yyy])a[xxx][yyy]=0; else a[xxx][yyy]=1;} 47 } 48 49 } 50 int check(){ 51 FOR(i,1,n) 52 if(a[m][i]==1) return 0; 53 return 1; 54 } 55 56 int dfs(int depth){ 57 if (depth==m) return check(); 58 int sum=0; 59 FOR(i,1,n) 60 if(a[depth-1][i]==1) {color(depth,i);sum++;tmp2[depth][i]=1;} 61 int ans=dfs(depth+1); 62 FOR(i,1,n) 63 if(tmp2[depth][i]) color(depth,i); 64 if (ans==-1) return -1; else return ans+sum; 65 } 66 void pri(int x){ 67 FOR(j,0,n-1) 68 tmp[n-j]=(x>>j)&1; 69 FOR(i,1,n-1) 70 cout<<tmp[i]<<' '; 71 cout<<tmp[n]<<endl; 72 FOR(j,1,n) 73 if(tmp[j]) color(1,j); 74 75 FOR(i,2,m){ 76 FOR(j,1,n-1) if (a[i-1][j]) {color(i,j);cout<<1<<' ';} else cout<<0<<' '; 77 if (a[i-1][n]) {color(i,n);cout<<1<<endl;} else cout<<0<<endl; 78 } 79 //FOR(i,1,n-1) 80 //cout<<0<<' '; 81 //cout<<0<<endl; 82 } 83 int main(){ 84 #ifdef LOCAL 85 freopen("input.txt","r",stdin); 86 freopen("output.txt","w",stdout); 87 #endif 88 cin>>m>>n; 89 FOR(i,1,m) 90 FOR(j,1,n) 91 cin>>c[i][j]; 92 int s=(1<<n)-1; 93 int min=1000000000; 94 int minn; 95 bool flag=false; 96 FOR(i,0,s){ 97 sum=0; 98 FOR(j,0,n-1) 99 tmp[n-j]=(i>>j)&1; 100 FOR(j,1,m) 101 FOR(k,1,n) 102 a[j][k]=c[j][k]; 103 //FOR(j,1,n) 104 //cout<<tmp[j]; 105 //cout<<"********"; 106 //cout<<endl; 107 108 FOR(j,1,n) if(tmp[j]==1) {color(1,j);sum++;} 109 FOR(j,2,m) 110 FOR(k,1,n) 111 if(a[j-1][k]) {color(j,k);sum++;} 112 113 //ansx=sum+dfs(2); 114 //cout<<i<<' '<<ansx-sum<<endl; 115 116 if(check()){ 117 if(min>sum) {min=sum; minn=i;} 118 flag=true; 119 } 120 //FOR(j,1,n) 121 //if(tmp[j]) color(1,j); 122 /*FOR(j,1,m) 123 { 124 FOR(k,1,n) 125 cout<<a[j][k]; 126 cout<<endl;}*/ 127 //if(flag) pri(minn); 128 } 129 FOR(j,1,m) 130 FOR(k,1,n) 131 a[j][k]=c[j][k]; 132 //cout<<min<<' '<<minn; 133 if(flag) pri(minn); 134 else cout<<"IMPOSSIBLE"<<endl; 135 #ifdef LOCAL 136 fclose(stdin); 137 fclose(stdout); 138 #endif 139 //system(pause); 140 return 0; 141 }
*偶然看到的神仙代碼:https://vjudge.net/solution/21754430
*改進:我們根據保存的路徑就可以直接推斷圖中反轉的狀態,省去了修改原圖的麻煩
---POJ 1426(01構造數(模意義))
當帶模dp做了,wa到飛(必須保證每次轉移不能覆蓋當前操作)不然會無限風怒風怒,GG
提前處理個10^n的模p
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 1000 32 int n,j; 33 int dp[maxn],q[maxn],m[maxn]; 34 void outr(int x){ 35 cout<<1; 36 FOR(i,1,x-1) 37 cout<<0; 38 cout<<endl; 39 } 40 void outy(){ 41 //cout<<"*"; 42 int jj=0; 43 int kk=0; 44 vector<int> xx; 45 //xx.pb(1); 46 do{ 47 kk++; 48 xx.pb(dp[jj]); 49 //cout<<jj<<' '<<q[jj]<<' '<<dp[jj]<<endl; 50 jj=q[jj]; 51 }while(jj!=0); 52 //cout<<kk; 53 //xx.pb(1); 54 //kk++; 55 //FOR(i,0,kk-1) 56 //cout<<xx[i]<<' '; 57 //cout<<endl; 58 //cout<<kk; 59 FOR(i,0,kk-2){ 60 cout<<1; 61 //cout<<xx[i]-xx[i+1]-1<<endl; 62 FOR(jj,1,xx[i]-xx[i+1]-1) 63 cout<<0; 64 } 65 if(xx[kk-1]!=1) {cout<<1; FORD(i,xx[kk-1]-1,1) cout<<0;} else cout<<1; 66 cout<<endl; 67 } 68 void zy(int aa,int bb,int cc){ 69 if(aa==0) if(j<=3) return; 70 dp[aa]=bb; 71 q[aa]=cc; 72 return; 73 } 74 int main(){ 75 #ifdef LOCAL 76 freopen("input.txt","r",stdin); 77 freopen("output.txt","w",stdout); 78 #endif 79 n=201; 80 while(true){ 81 cin>>n; 82 //n--; 83 if(n==0) return 0; 84 m[1]=1; 85 int f=-1; 86 FOR(i,2,100) 87 {m[i]=(m[i-1]*10)%n;if(m[i]==0&&i>3) {f=i;break;}} 88 //cout<<f<<endl; 89 if(f!=-1) outr(f); 90 else{ 91 FOR(i,0,210) 92 {dp[i]=0;q[i]=0;} 93 j=0; 94 while(true){ 95 j++; 96 FORD(i,n,1) 97 if(dp[i]!=0) if(dp[i]!=j) if(dp[(i+m[j])%n]==0) zy((i+m[j])%n,j,i); 98 if(dp[m[j]]==0) if(dp[0]!=j) zy(m[j],j,0); 99 if(q[0]!=0) {outy();break;} 100 //if(dp[i]!=0) if(dp[(i+m[j])%n]==0){dp[(i+m[j])%n]=j;q[(i+m[j])%n]=i;} 101 //if(dp[m[j]]==0){dp[m[j]]=j;q[m[j]]=0;} 102 //if(q[0]!=0){ if(dp[0]>3){outy();break;}else{dp[0]=0;q[0]=0;}} 103 }} 104 } 105 106 #ifdef LOCAL 107 fclose(stdin); 108 fclose(stdout); 109 #endif 110 //system(pause); 111 return 0; 112 }
*偶然看到的神仙代碼:https://vjudge.net/solution/22468052
所以說哪來的這些神仙,慌都不慌直接搜索的。。。
---POJ 3126(改变数位从一个素数到另一个素数)
BFS什麼時候都能直接跳出真方便
1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 #define FOR(i,j,k) for(int i=j;i<=k;i++) 10 #define FORD(i,j,k) for(int i=j;i>=k;i--) 11 #define LL long long 12 #define SZ(x) x.size() 13 int a[]={1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8053,8059,8069,8081,8087,8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,9949,9967,9973}; 14 int p[10000],vis[10000]; 15 int k,n,m,s,ans; 16 int bfs() 17 { 18 queue<int> q1,q2; 19 q1.push(n); 20 q2.push(1); 21 while(q1.front()!=m) 22 { 23 int fr=q1.front(); 24 int fv=q2.front(); 25 int shi=1; 26 vis[fr]=1; 27 FOR(i,1,4) 28 { 29 ans=fr/(shi*10)*(shi*10)+fr%shi; 30 if(i!=4) s=0; else s=1; 31 FOR(j,s,9) if(p[ans+shi*j]) if(!vis[ans+shi*j]) {q1.push(ans+shi*j); q2.push(fv+1);vis[ans+shi*j]=1;} 32 shi*=10; 33 } 34 q1.pop(); 35 q2.pop(); 36 } 37 return q2.front()-1; 38 } 39 int main() 40 { 41 FOR(i,0,1060) 42 p[a[i]]=1; 43 cin>>k; 44 FOR(i,1,k) 45 { 46 cin>>n>>m; 47 FOR(i,1000,9999) 48 vis[i]=0; 49 cout<<bfs()<<endl; 50 } 51 return 0; 52 }
---POJ 3087(洗牌)
爲啥有人會拿搜索過這題。。。絕了。。。顯然操作是個環,所以就直接模擬了
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 int n,c,l,flag,sum; 32 char s1[220],s2[220],s3[220],t[220]; 33 bool check1(){ 34 FOR(i,1,2*l) 35 if(s3[i]!=t[i]) return false; 36 return true; 37 } 38 bool check2(){ 39 FOR(i,1,2*l) 40 if(s1[i]!=t[i]) return false; 41 return true; 42 } 43 int main(){ 44 #ifdef LOCAL 45 freopen("input.txt","r",stdin); 46 freopen("output.txt","w",stdout); 47 #endif 48 cin>>n; 49 FOR(i,1,n){ 50 cin>>l; 51 FOR(j,1,2*l) 52 cin>>s1[j]; 53 FOR(j,1,2*l){ 54 s2[j]=s1[j];t[j]=s2[j];} 55 FOR(j,1,2*l) 56 cin>>s3[j]; 57 sum=0; 58 flag=true; 59 do{ 60 FOR(j,1,2*l) 61 s2[j]=t[j]; 62 sum++; 63 FOR(j,1,l){ 64 t[2*j-1]=s2[l+j]; 65 t[2*j]=s2[j]; 66 } 67 //FOR(j,1,2*l) 68 //cout<<s2[j]; 69 //cout<<endl; 70 //FOR(j,1,2*l) 71 //cout<<t[j]; 72 //cout<<endl; 73 if(check1()) {flag=false;break;} 74 if(check2()) break; 75 }while(true); 76 if(!flag) cout<<i<<' '<<sum<<endl; 77 else cout<<i<<' '<<-1<<endl; 78 } 79 #ifdef LOCAL 80 fclose(stdin); 81 fclose(stdout); 82 #endif 83 //system(pause); 84 return 0; 85 }
對了,還是要罵一句,傻逼題面,wqnmlgb
---POJ 3414(倒水問題)
是大模擬誒,思路清晰就好了
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 110 32 int a,b,c; 33 int t,t1,t2; 34 int vis[maxn+1][maxn+1]; 35 pair<int,int>pre[maxn+1][maxn+1]; 36 string doi[maxn+1][maxn+1]; 37 38 struct node{ 39 int l,r; 40 int depth; 41 }; 42 queue<node> Q; 43 void putin(node x){ 44 x.depth++; 45 Q.push(x); 46 vis[x.l][x.r]=x.depth; 47 x.depth--; 48 return; 49 } 50 void prin(int xx,int yy){ 51 cout<<vis[xx][yy]<<endl; 52 stack<string> SS; 53 int pxx; 54 int pyy; 55 while(xx!=0||yy!=0){ 56 SS.push(doi[xx][yy]); 57 pxx=pre[xx][yy].first; 58 pyy=pre[xx][yy].second; 59 xx=pxx; 60 yy=pyy; 61 //cout<<xx<<' '<<yy<<endl; 62 } 63 //int o; 64 while(!SS.empty()) 65 { 66 cout<<SS.top()<<endl; 67 SS.pop(); 68 //cout<<++o; 69 } 70 return; 71 } 72 int check(){ 73 FOR(i,0,b) 74 if(vis[c][i]!=INF){prin(c,i);return 1;} 75 FOR(i,0,a) 76 if(vis[i][c]!=INF){prin(i,c);return 1;} 77 return 0; 78 } 79 void bfs(){ 80 node kkk;kkk.l=0;kkk.r=0;kkk.depth=0; 81 vis[a][b]=0; 82 while(!Q.empty()) 83 Q.pop(); 84 Q.push(kkk); 85 while(!Q.empty()){ 86 kkk=Q.front(); 87 Q.pop(); 88 //fill 89 if(kkk.l!=a){t=kkk.l;kkk.l=a; 90 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t;pre[kkk.l][kkk.r].second=kkk.r;doi[kkk.l][kkk.r]="FILL(1)";} 91 kkk.l=t;} 92 if(kkk.r!=b){t=kkk.r;kkk.r=b; 93 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=kkk.l;pre[kkk.l][kkk.r].second=t;doi[kkk.l][kkk.r]="FILL(2)";} 94 kkk.r=t;} 95 //drop 96 if(kkk.l!=0){t=kkk.l;kkk.l=0; 97 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t;pre[kkk.l][kkk.r].second=kkk.r;doi[kkk.l][kkk.r]="DROP(1)";} 98 kkk.l=t;} 99 if(kkk.r!=0){t=kkk.r;kkk.r=0; 100 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=kkk.l;pre[kkk.l][kkk.r].second=t;doi[kkk.l][kkk.r]="DROP(2)";} 101 kkk.r=t;} 102 //pour 103 if(kkk.r!=0) 104 { 105 if(kkk.l+kkk.r<=a){t1=kkk.l;t2=kkk.r;kkk.l+=kkk.r;kkk.r=0; 106 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t1;pre[kkk.l][kkk.r].second=t2;doi[kkk.l][kkk.r]="POUR(2,1)";} 107 kkk.l=t1;kkk.r=t2; 108 } 109 else {t1=kkk.l;t2=kkk.r;kkk.l=a;kkk.r=t1+t2-a; 110 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t1;pre[kkk.l][kkk.r].second=t2;doi[kkk.l][kkk.r]="POUR(2,1)";} 111 kkk.l=t1;kkk.r=t2; 112 } 113 } 114 if(kkk.l!=0) 115 { 116 if(kkk.l+kkk.r<=b){t1=kkk.l;t2=kkk.r;kkk.r+=kkk.l;kkk.l=0; 117 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t1;pre[kkk.l][kkk.r].second=t2;doi[kkk.l][kkk.r]="POUR(1,2)";} 118 kkk.l=t1;kkk.r=t2; 119 } 120 else {t1=kkk.l;t2=kkk.r;kkk.r=b;kkk.l=t1+t2-b; 121 if(vis[kkk.l][kkk.r]==INF) {putin(kkk);pre[kkk.l][kkk.r].first=t1;pre[kkk.l][kkk.r].second=t2;doi[kkk.l][kkk.r]="POUR(1,2)";} 122 kkk.l=t1;kkk.r=t2; 123 } 124 } 125 //check 126 if(check()) return; 127 } 128 cout<<"impossible"<<endl; 129 return; 130 } 131 int main(){ 132 #ifdef LOCAL 133 freopen("input.txt","r",stdin); 134 freopen("output.txt","w",stdout); 135 #endif 136 cin>>a>>b>>c; 137 FOR(i,0,maxn) 138 FOR(j,0,maxn) 139 //for(int i=0;i<=maxn;i++) 140 //for(int j=0;j<=maxn;j++) 141 vis[i][j]=INF; 142 bfs(); 143 //cout<<vis[3][4]; 144 #ifdef LOCAL 145 fclose(stdin); 146 fclose(stdout); 147 #endif 148 //system(pause); 149 return 0; 150 }
*偶然看到的神仙代碼: https://vjudge.net/solution/22126182
啊,系神仙,我愛了(
---FZU 2150(雙起點bfs)
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 200 32 #define maxm 15 33 int dx[]={0,0,1,-1}; 34 int dy[]={1,-1,0,0}; 35 int k,n,m,minn,ans; 36 char a[maxm][maxm],c[maxm][maxm]; 37 int vis[maxm][maxm]; 38 queue<pair<int,int> > Q; 39 int check(){ 40 int max=0;/* 41 FOR(i,1,n){ 42 FOR(j,1,m) 43 cout<<c[i][j]; 44 cout<<endl;} 45 FOR(i,1,n){ 46 FOR(j,1,m) 47 cout<<vis[i][j]; 48 cout<<endl;}*/ 49 FOR(i,1,n) 50 FOR(j,1,m) 51 { 52 if(c[i][j]=='#') return -1; 53 if(max<vis[i][j]) max=vis[i][j]; 54 } 55 return max; 56 57 } 58 void init(){ 59 FOR(i,1,n) 60 FOR(j,1,m) 61 c[i][j]=a[i][j]; 62 FOR(i,1,n) 63 FOR(j,1,m) 64 vis[i][j]=-1; 65 while(!Q.empty()) Q.pop(); 66 return; 67 } 68 int pbb(pair<int,int> l){ 69 int xx,yy; 70 FOR(i,0,3) 71 { 72 xx=l.first+dx[i]; 73 yy=l.second+dy[i]; 74 if(1<=xx&&xx<=n) 75 if(1<=yy&&yy<=m) 76 if(c[xx][yy]=='#') 77 if(vis[xx][yy]==-1) {c[xx][yy]='.'; vis[xx][yy]=vis[l.first][l.second]+1;Q.push(make_pair(xx,yy));} 78 } 79 } 80 int bfs(pair<int,int> x1,pair<int,int> x2){ 81 init(); 82 pair<int,int> kkk; 83 Q.push(x1); 84 Q.push(x2); 85 vis[x1.first][x1.second]=0; 86 vis[x2.first][x2.second]=0; 87 c[x1.first][x1.second]='.'; 88 c[x2.first][x2.second]='.'; 89 while(!Q.empty()){ 90 kkk=Q.front(); 91 Q.pop(); 92 pbb(kkk); 93 } 94 return check(); 95 } 96 int main(){ 97 #ifdef LOCAL 98 freopen("input.txt","r",stdin); 99 freopen("output.txt","w",stdout); 100 #endif 101 cin>>k; 102 vector<pair<int,int> > aa; 103 104 FOR(i,1,k){ 105 cin>>n>>m; 106 aa.clear(); 107 FOR(j,1,n) 108 FOR(k,1,m) 109 { 110 cin>>a[j][k]; 111 if(a[j][k]=='#') aa.pb(make_pair(j,k)); 112 } 113 minn=100010; 114 ans=-1; 115 if(aa.size()==1) {cout<<"Case "<<i<<": "<<0<<endl;continue;} 116 for(vector<pair<int,int> >::iterator i2=aa.begin();i2!=aa.end();i2++) 117 for(vector<pair<int,int> >::iterator i3=i2+1;i3!=aa.end();i3++){ 118 //if(i3==i2) continue; 119 ans=bfs(*i2,*i3); 120 if(ans!=-1&&ans<minn) minn=ans; 121 } 122 //if(minn!=100010) printf("Case %d: %d\n",i,minn); else printf("Case %d: %d\n",i,-1); 123 if(minn!=100010) cout<<"Case "<<i<<": "<<minn<<endl; else cout<<"Case "<<i<<": "<<-1<<endl; 124 } 125 #ifdef LOCAL 126 fclose(stdin); 127 fclose(stdout); 128 #endif 129 //system(pause); 130 return 0; 131 }
可以提前處理掉需要燒掉的格子數,在搜索時記錄選定起點能夠燒掉格子的數量進行比對,這樣就可以把check()的環節剪掉
---Uva 11624
1 //#define LOCAL 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <fstream> 6 #include <string> 7 #include <vector> 8 #include <deque> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 #include <algorithm> 14 #include <functional> 15 #include <utility> 16 #include <bitset> 17 #include <cmath> 18 #include <cstdlib> 19 #include <ctime> 20 #include <cstdio> 21 using namespace std; 22 #define X_mem(x,y,z) (X_mem[x][y][z] ? X_mem[x][y][z] :X_mem[x][y][z]=mem(x,y,z)) 23 // X_INIT=0 mem_Macro 24 #define INF (1<<29) 25 #define MOD 1000000007ll 26 #define FOR(i,j,k) for(int i=j;i<=k;i++) 27 #define FORD(i,j,k) for(int i=j;i>=k;i--) 28 #define LL long long 29 #define SZ(x) int(x.size()) 30 #define pb push_back 31 #define maxn 1010 32 int dx[]={-1,1,0,0}; 33 int dy[]={0,0,1,-1}; 34 int k,n,m,xx,yy,ei,ej,si,sj,ans,cnn; 35 bool flag; 36 int vis[maxn][maxn],visj[maxn][maxn]; 37 char ma[maxn][maxn]; 38 struct node{ 39 int x,y,depth; 40 }; 41 node kkk,t; 42 queue<node> Q; 43 void init(){ 44 FOR(i,0,n-1) 45 FOR(j,0,m-1){ 46 vis[i][j]=-1; 47 visj[i][j]=-1; 48 } 49 while(!Q.empty()) Q.pop(); 50 return; 51 } 52 void pbb(node cnt){ 53 FOR(i,0,3){ 54 //cout<<xx<<yy<<endl; 55 xx=cnt.x+dx[i]; 56 yy=cnt.y+dy[i]; 57 if(0<=xx&&xx<=n-1) 58 if(0<=yy&&yy<=m-1) 59 if(ma[xx][yy]=='.') 60 if(vis[xx][yy]==-1) { 61 vis[xx][yy]=cnt.depth+1; 62 t.x=xx; 63 t.y=yy; 64 t.depth=cnt.depth+1; 65 Q.push(t); 66 } 67 } 68 } 69 void bfsf(){ 70 while(!Q.empty()){ 71 kkk=Q.front(); 72 Q.pop(); 73 pbb(kkk); 74 } 75 } 76 /* 77 int check(){ 78 FOR(i,0,n-1) 79 if(visj[i][0]!=-1) return visj[i][0]; 80 FOR(i,0,n-1) 81 if(visj[i][m-1]!=-1) return visj[i][m-1]; 82 FOR(i,0,m-1) 83 if(visj[0][i]!=-1) return visj[0][i]; 84 FOR(i,0,m-1) 85 if(visj[n-1][i]!=-1) return visj[n-1][i]; 86 return -1; 87 }*/ 88 void pbbj(node cnt){ 89 FOR(i,0,3){ 90 xx=cnt.x+dx[i]; 91 yy=cnt.y+dy[i]; 92 if(0<=xx&&xx<=n-1) 93 if(0<=yy&&yy<=m-1) 94 if(ma[xx][yy]=='.') 95 if(visj[xx][yy]==-1&&(cnt.depth+1<vis[xx][yy]||vis[xx][yy]==-1)) 96 { 97 visj[xx][yy]=cnt.depth+1; 98 t.x=xx; 99 t.y=yy; 100 t.depth=cnt.depth+1; 101 if(xx==0||xx==n-1||yy==0||yy==m-1){flag=true;ei=xx;ej=yy;vis[xx][yy]=cnt.depth+1;} 102 Q.push(t); 103 } 104 } 105 } 106 int bfsj(int a,int b){ 107 kkk.x=a;kkk.y=b;kkk.depth=0; 108 flag=false; 109 visj[a][b]=0; 110 Q.push(kkk); 111 if(a==0||a==n-1||b==0||b==m-1) return 0; 112 while(!Q.empty()){ 113 kkk=Q.front(); 114 Q.pop(); 115 pbbj(kkk); 116 //cnn=check(); 117 if(flag) return visj[ei][ej]; 118 } 119 return -1; 120 } 121 122 int main(){ 123 #ifdef LOCAL 124 freopen("input.txt","r",stdin); 125 freopen("output.txt","w",stdout); 126 #endif 127 std::ios::sync_with_stdio(false); 128 //cin>>k; 129 scanf("%d",&k); 130 FOR(i,1,k){ 131 //cin>>n>>m; 132 scanf("%d%d",&n,&m); 133 //cout<<n<<m; 134 init(); 135 FOR(j,0,n-1) 136 scanf("%s",&ma[j]); 137 FOR(j,0,n-1) 138 FOR(k,0,m-1){ 139 //cin>>ma[j][k]; 140 //ma[j][k]=getchar(); 141 if(ma[j][k]=='F'){kkk.x=j;kkk.y=k;kkk.depth=0;Q.push(kkk);vis[j][k]=0;} 142 //{si=j;sj=k;} 143 if(ma[j][k]=='J') {si=j;sj=k;} 144 } 145 bfsf(); 146 ans=bfsj(si,sj); 147 if(ans!=-1) cout<<ans+1<<endl; else cout<<"IMPOSSIBLE"<<endl; 148 /*FOR(i,0,n-1){ 149 FOR(j,0,m-1) 150 cout<<visj[i][j]; 151 cout<<endl;} */ 152 } 153 #ifdef LOCAL 154 fclose(stdin); 155 fclose(stdout); 156 #endif 157 //system(pause); 158 return 0; 159 }
可能有多個火點
*偶然看到的神仙代碼:https://vjudge.net/solution/22247485
非常自信的寫法,換了我是做不來的