标签:
脑筋急转弯
1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 110<<2; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 //void fre() { 28 // freopen("in.txt","r",stdin); 29 //} 30 int vis[20]; 31 int ans[5]; 32 int main(){ 33 int n; 34 string s; 35 cin>>n; 36 cin>>s; 37 clc(vis,0); 38 clc(ans,0); 39 for(int i=0;i<s.length();i++) vis[s[i]-‘0‘]=true; 40 for(int i=0;i<=9;i++){ 41 if(!vis[i]) continue; 42 if(i==1||i==2||i==3) ans[1]=1; 43 if(i==3||i==6||i==9||i==0) ans[2]=1; 44 if(i==9||i==0||i==7) ans[3]=1; 45 if(i==7||i==4||i==1||i==0) ans[4]=1; 46 } 47 printf("%s\n",ans[1]&ans[2]&ans[3]&ans[4]?"YES":"NO"); 48 return 0; 49 }
从i到j的花费是fabs(j-i)
现在每个点有一条路ai,可以使得i走到ai的花费为1
现在问你从1走到i点的花费是多少
搜索i点前后两个点
1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 200010; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 //void fre() { 28 // freopen("in.txt","r",stdin); 29 //} 30 int a[N]; 31 int ans[N]; 32 int vis[N]; 33 int n; 34 struct Node{ 35 int x,w; 36 Node(int a,int b):x(a),w(b){} 37 }; 38 void bfs(){ 39 queue<Node>q; 40 q.push(Node(1,0)); 41 while(!q.empty()){ 42 Node f=q.front(); 43 q.pop(); 44 if(vis[f.x]) continue; 45 vis[f.x]=true; 46 ans[f.x]=f.w; 47 if(!vis[a[f.x]]) 48 q.push(Node(a[f.x],f.w+1)); 49 if(!vis[f.x+1]&&(f.x+1)<=n) 50 q.push(Node(f.x+1,f.w+1)); 51 if(!vis[f.x-1]&&(f.x-1)>=0) 52 q.push(Node(f.x-1,f.w+1)); 53 } 54 } 55 int main(){ 56 // int n; 57 cin>>n; 58 for(int i=1;i<=n;i++) 59 scanf("%d",&a[i]); 60 bfs(); 61 for(int i=1;i<=n;i++){ 62 printf("%d%c",ans[i],i!=n?‘ ‘:‘\n‘ ); 63 } 64 return 0; 65 }
Codeforces Round #361 (Div. 2)
标签:
原文地址:http://www.cnblogs.com/ITUPC/p/5655991.html