问题 A: 魔法鲜花 http://www.jzoj.cn/problem.php?cid=5707&pid=0
#include<bits/stdc++.h> using namespace std; int s,t; int vis[100007]; struct node{ int num; string path; }; string ABC="ABC"; int main(){ memset(vis,-1,sizeof(vis)); cin>>s>>t; if(s==t){ cout<<0; return 0; } queue<node> q; while(!q.empty())q.pop(); q.push({s,""}); vis[s]=0; while(!q.empty()){ int frontn=q.front().num; string frontp=q.front().path; q.pop(); for(int i=0;i<3;i++){ int x=frontn; if(i==0)x+=1; if(i==1&&x>10)x-=10; if(i==2)x*=2; if(x>0&&x<=100000&&vis[x]==-1){ q.push({x,frontp+ABC[i]}); vis[x]=vis[frontn]+1; if(x==t){ cout<<vis[x]<<endl; cout<<frontp+ABC[i]; return 0; } } } } return 0; } /*换了一种路径记录方式*/
问题 E: 最少点路径 http://www.jzoj.cn/problem.php?cid=5707&pid=4
#include<bits/stdc++.h> using namespace std; int N,s,t; int a[13][13]; bool vis[13]; struct node{ int w; string path; }; queue<node> q; int main(){ cin>>N; for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ cin>>a[i][j]; } } cin>>s>>t; while(!q.empty())q.pop(); string st="";st+=char(s+48); q.push({s,st}); vis[s]=1; while(!q.empty()){ int x=q.front().w; string pa=q.front().path; q.pop(); //cout<<x<<endl; for(int i=1;i<=N;i++){ string p=pa+'-'+char(i+48); if(a[x][i]==1&&!vis[i]){ q.push({i,p}); vis[i]=1; if(i==t){ cout<<p<<endl; return 0; } } } } return 0; }
问题 F: 八数码问题 http://www.jzoj.cn/problem.php?cid=5707&pid=5
#include<bits/stdc++.h> using namespace std; /* 0 1 2 3 4 5 6 7 8 */ string start,finish; struct node{ string a; int b,k; }; int mov[4]={-3,3,1,-1}; set<string> vis; string change(string str,int cz,int kk){ int ch=kk+mov[cz]; if(ch<0||ch>8||cz==3&&kk==3||cz==3&&kk==6||cz==2&&kk==2||cz==2&&kk==5)return "NO"; else{ string strr=str; swap(strr[kk],strr[ch]); return strr; } } int main(){ cin>>start; cin>>finish; if(start==finish){//特判!!! cout<<0; return 0; } queue<node> q; while(!q.empty())q.pop(); int z=0;while(start[z]!='0')z++; q.push({start,0,z}); vis.insert(start); while(!q.empty()){ string fronta=q.front().a; int frontb=q.front().b; int frontk=q.front().k; q.pop(); for(int i=0;i<4;i++){ string str=change(fronta,i,frontk); if(str!="NO"&&vis.count(str)==0){ q.push({str,frontb+1,frontk+mov[i]}); vis.insert(str); if(str==finish){ cout<<frontb+1; return 0; } } } } cout<<"impossible"; return 0; }