Water title bar: While over again (after all water problems), but the code still affixed.
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 int ans,n,k,a[30]; 22 bool vis[30]; 23 bool check(int n) 24 { 25 if (n==1) 26 return false; 27 for (int i=2;i*i<=n;i++){ 28 if (n%i==0) 29 return false; 30 } 31 return true; 32 } 33 void dfs(int pos,int index,int sum) 34 { 35 if (index==k){ 36 if (check(sum)){ 37 ans++; 38 } 39 return ; 40 } 41 if (pos>n) 42 return ; 43 for (int i=pos;i<=n;i++){ 44 if (vis[i]==false){ 45 vis[i]=true; 46 dfs(i+1,index+1,sum+a[i]); 47 vis[i]=false; 48 } 49 } 50 } 51 int main() 52 { 53 ans=0; 54 cin>>n>>k; 55 for (int i=1;i<=n;i++){ 56 cin>>a[i]; 57 } 58 dfs(1,0,0); 59 cout<<ans<<endl; 60 return 0; 61 }
************************************************** *************split line*********************************** ****************************
Here for simplicity FIG time, may start from 0 outermost periphery search, the search to all assigned -1, and the rest after the end of the search is zero on the whole should be painted 2.
Remember the last time output, the point is marked as 0 output by -1.
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 22 int n; 23 struct s{ 24 int x,y,num; 25 }mp[50][50]; 26 const int dir[4][2]={1,0,0,1,-1,0,0,-1}; 27 queue <s> q; 28 void init() 29 { 30 while (!q.empty()) 31 q.pop(); 32 cin>>n; 33 for (int i=1;i<=n;i++){ 34 for (int j=1;j<=n;j++){ 35 cin>>mp[i][j].num; 36 mp[i][j].x=i; 37 mp[i][j].y=j; 38 if ((i==1||i==n||j==1||j==n)&&mp[i][j].num==0) 39 q.push(mp[i][j]); 40 } 41 } 42 return ; 43 } 44 void bfs() 45 { 46 s node,temp; 47 while (!q.empty()){ 48 node=q.front(); 49 mp[node.x][node.y].num=-1; 50 for (int k=0;k<4;k++){ 51 temp.x=node.x+dir[k][0]; 52 temp.y=node.y+dir[k][1]; 53 if (temp.x<1||temp.y<1||temp.x>n||temp.y>n||mp[temp.x][temp.y].num==1) 54 continue; 55 else if (mp[temp.x][temp.y].num==0){ 56 q.push(temp); 57 } 58 } 59 q.pop(); 60 } 61 return ; 62 } 63 void Print() 64 { 65 for (int i=1;i<=n;i++){ 66 for (int j=1;j<=n;j++){ 67 if (mp[i][j].num==-1) 68 cout<<"0 "; 69 else if (mp[i][j].num==0) 70 cout<<"2 "; 71 else 72 cout<<"1 "; 73 } 74 cout<<endl; 75 } 76 } 77 int main() 78 { 79 init(); 80 bfs(); 81 Print(); 82 return 0; 83 }
************************************************** *************split line*********************************** ****************************
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 int n,ans[20]; 22 bool vis[20]; 23 void Print() 24 { 25 for (int i=1;i<=n;i++){ 26 printf("%5d",ans[i]); 27 } 28 cout<<endl; 29 return ; 30 } 31 void dfs(int index) 32 { 33 if (index==n+1){ 34 Print(); 35 return ; 36 } 37 for (int i=1;i<=n;i++){ 38 if (vis[i]==false){ 39 vis[i]=true; 40 ans [index] = i; 41 DFS (index + 1 ); 42 show [i] = false ; 43 } 44 } 45 return ; 46 } 47 int main () 48 { 49 cin >> n; 50 dfs ( 1 ); 51 return 0 ; 52 }
************************************************** *************split line*********************************** ****************************
Here only need to understand the DFS order problem, whether it is first / middle / subsequent arrangement for a complete sub-tree, they are attached to the sequence.
1 #include<iostream>
2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 char s[30],t[30]; 22 int len; 23 void btree(int l,int r,int L,int R) 24 { 25 if (l>r) 26 return ; 27 cout<<t[R]; 28 int cnt=l; 29 while (s[cnt]!=t[R]) 30 cnt++; 31 int temp=cnt-l; 32 btree(l,cnt-1,L,L+temp-1); 33 btree(cnt+1,r,L+temp,R-1); 34 } 35 int main() 36 { 37 cin>>s+1>>t+1; 38 len=strlen(s+1); 39 btree ( 1 , referred to as 1 , only);
40 }
************************************************** *************split line*********************************** ****************************