Crop hybridization
Title Description
Crop hybridization is an important step in crop cultivation. There are ^ crops (numbered ^ 1 ^ to ^ n) known, and the time from sowing to maturity of ^ i ^ crop is ^ Ti. Crops can be hybridized in pairs, and the longer of the two kinds of time is taken as the hybridization time. If the planting time of crop A is 5 days and that of crop B is 7 days, the time of AB hybridization is 7 days. Crop hybridization will produce fixed crops, and the new crops still belong to one of N crops.
At the beginning, it has seeds of {M crops (unlimited quantity, which can support multiple crosses). Multiple hybridization processes can be carried out at the same time. Ask how many days it takes to get a given target seed.
If there are four kinds of crops ABCD, their maturity time is 5 days, 7 days, 3 days and 8 days. Initially, there are seeds of AB crops, the target seed is D, and the known hybridization condition is a × B → C,A × C → D. The shortest hybridization process is:
Day 1 to 7 (time of crop b), A × B → C.
Day 8 to 12 (time of crop A), A × C → D.
It takes 12 days to get the seeds of crop D.
Enter description
The first line of the input contains four integers N,M,K,T, N ¢ represents the total number of crop species (numbers ¢ 1 ¢ to ¢ N), M ¢ represents the number of crop seed types initially owned, K ¢ represents the number of schemes that can be crossed, and T ¢ represents the number of target seeds.
The second line contains N ^ integers, where the ^ i ^ integer represents the planting time ^ Ti (1 ≤ Ti ≤ 100) of the ^ i ^ crop.
The third line contains # M # integers, which respectively represent the owned seed type Kj (1 ≤ Kj ≤ M), and Kj # is different.
Lines 4 to # K + 3, each line contains three integers # A, B and C, indicating that the seeds of # C # crops can be obtained by hybridization between # A # crops and # B # crops.
Among them, 1 ≤ N ≤ 2000, 2 ≤ M ≤ N,1 ≤ K ≤ 10 ^ 5, 1 ≤ T ≤ N ensure that the target seed can be obtained by hybridization.
Output description
Output an integer indicating the shortest hybridization time to obtain the target seed.
input
6 2 4 6 5 3 4 6 4 9 1 2 1 2 3 1 3 4 2 3 5 4 5 6
output
16
Example description
From day 1 to day 5, the crops No. 1 and No. 2 were hybridized to obtain the crop seeds No. 3.
From day 6 to day 10, the crops No. 1 and No. 3 were hybridized to obtain the crop seeds No. 4.
From day 6 to day 9, the crops No. 2 and No. 3 were hybridized to obtain the crop seeds No. 5.
From day 11 to day 16, the crops No. 4 and No. 5 were hybridized to obtain the crop seeds No. 6.
It takes 16 days in total.
#include<iostream> using namespace std; //Crop hybridization int N,M,K,T;//Total number of crop types, number of owned types, hybridization scheme, seed number int t[2002];//Time required for hybridization bool have[2002];//Obtained seeds struct father{//Parental records of hybrid plants int num; int u[2002]; int v[2002]; int limtime; }fa[2002]; bool dfs(int x){ if(fa[x].num==0) return false;//Cannot be obtained by hybridization for(int i=1;i<=fa[x].num;i++){//Traverse the hybrid parents of x to obtain the minimum time int u=fa[x].u[i]; int v=fa[x].v[i]; if(!have[u]){ if(!dfs(u)) continue; } if(!have[v]){ if(!dfs(v)) continue; } if(!fa[x].limtime)//Get c, there is only one hybridization scheme fa[x].limtime=max(fa[u].limtime,fa[v].limtime)+max(t[u],t[v]);//Initialization: the hybridization time of the larger one of the parents, plus the growth time of the larger one of the parents else//Get c, there are many hybridization schemes, and the one with the least time is selected fa[x].limtime=min(fa[x].limtime, max(fa[u].limtime, fa[v].limtime) + max(t[u], t[v]));//Take the minimum value } have[x] = true;//x set to available return true; } int main() { cin>>N>>M>>K>>T; for(int i=1;i<=N;i++){ cin>>t[i]; } for(int i=1;i<=M;i++){ int tmp; cin>>tmp; have[tmp]=true; } for(int i=1;i<=K;i++){ int a,b,c; cin>>a>>b>>c; int tem=++fa[c].num;//The number of schemes of c can be obtained by hybridization if(t[a]<t[b]) swap(a,b);//Ensure that the u array is on the larger side fa[c].u[tem]=a; fa[c].v[tem]=b; } dfs(T); cout<<fa[T].limtime; return 0; }
Injured queen
Title Description
There is an n × N , chess board (the square diagram of n , rows and N , columns). Please place n , injured chess queens in the board. Requirements:
- Any two queens are not in the same line.
- Any two queens are not in the same row.
- If two queens are on the same 45 degree slash, the difference in line numbers between the two queens is at least 3.
How many display schemes are there.
Enter description
The first line of input contains an integer n.
Where, 1 ≤ n ≤ 10.
Output description
Output an integer representing the answer.
input
4
output
2
#include<iostream> using namespace std; //Injured queen #include<algorithm> int n,ans,a[10]={0};//n*n chessboard bool check(int r,int c){//Judge whether row r and column c are available for(int i=1;i<r;i++){//Judge whether there are columns in the first r-1 row that conflict with column c if(a[i]==c) return false;//Column c exists and cannot be if((a[i]+i==r+c)&&(r-i)<3) return false;//Diagonal (row and column are the same) if((r-i==c-a[i])&&(r-i)<3) return false;//Diagonal (same difference between rows and columns) } return true; } void dfs(int r){ if(r==n+1){//After the first r line, the answer is + 1 ans++; return ; } for(int i=1;i<=n;i++){//Judge whether columns 1 to n are available in turn if(check(r,i)){//Row r and column i are available a[r]=i; dfs(r+1); a[r]=0;//Reset is easy to try new solutions } } } int main() { cin>>n; dfs(1); cout<<ans<<endl; return 0; }
global warming
Title Description
You have a NxN # pixel picture of a sea area, "." Indicates the sea and "#" indicates the land, as shown below:
.......
.##....
.##....
....##.
..####.
...###.
.......
One piece of land connected in the four directions of "up, down, left and right" (as shown in the picture, the purple one and the orange one are two islands) forms an island. For example, there are two islands in the above picture.
As the sea level rises due to global warming, scientists predict that a pixel of the edge of the island will be submerged by the sea in the coming decades. Specifically, if a land pixel is adjacent to the ocean (there is an ocean in the four adjacent pixels above, below, left and right), it will be submerged. (only the red ones will not be submerged)
For example, the sea area in the above figure will look like the following in the future:
.......
.......
.......
.......
....#..
.......
.......
Please calculate: according to the prediction of scientists, how many islands in the picture will be completely submerged.
Enter description
The first line contains an integer N (1 ≤ N ≤ 1000).
The following n rows and N columns represent a picture of the sea area.
The picture ensures that the pixels in row 1, column 1, row # N and column # n are oceans.
Output an integer to represent the answer.
input
7 ....... .##.... .##.... ....##. ..####. ...###. .......
output
1
If one island is surrounded by '#', it will not be flooded, and the rest is what you want.
#include <iostream> using namespace std; //global warming const int N=1005; char mp[N][N];//Sea area map int vis[N][N]; bool flag;//flag==0: indicates that the island is submerged, = = 1: indicates that it is not submerged int dirc[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//direction void dfs(int i,int j,int vis[N][N]){ if(mp[i-1][j]=='#'&&mp[i+1][j]=='#'&&mp[i][j-1]=='#'&&mp[i][j+1]=='#'/ / if there is land in all four directions, the point will not be submerged flag=1; for(int k=0;k<4;k++){//Four directions if(mp[i+dirc[k][0]][j+dirc[k][1]]=='#'&&vis[i+dirc[k][0]][j+dirc[k][1]]==0){ vis[i+dirc[k][0]][j+dirc[k][1]]=1; dfs(i+dirc[k][0],j+dirc[k][1],vis); } } } int main() { int n,ans=0; cin>>n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>mp[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(mp[i][j]=='#'&&vis[i][j]==0){ flag=0;//initialization vis[i][j]=0; dfs(i,j,vis); if(flag==0) ans++; } } } cout<<ans<<endl; return 0; }
Children's worship circle
Title Description
There are # children in the class, and everyone has a child they admire most (or themselves).
In a game, children need to sit in a circle. Each child has his favorite child on his right hand.
How many people in the circle who meet the conditions?
The children are numbered 1,2,3,... N.
Enter description
Enter the first line, an integer N (3 < N < 10 ^ 5).
The next line contains N # integers separated by spaces
Output description
It is required to output an integer to represent the number of people who meet the conditions.
input
9 3 4 2 5 3 8 4 6 9
output
4
Example explanation
As shown in the figure below, the worship relationship is indicated by arrows, and red indicates that it is not in the circle.
Obviously, the largest circle is the circle composed of [2,4,5,3].
#include<iostream> using namespace std; //Children's worship circle #include<cstring> int main() { int n; cin>>n; int a[n+1],vis[n+1];//The a subscript indicates the ith child, and the value indicates the number of children they worship for(int i=1;i<=n;i++){ cin>>a[i]; } int cnt,maxn=0,j; for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); cnt=0; if(vis[i]==0){ for(j=i;vis[j]==0;j=a[j]){//Let the adored children become the next target vis[j]=1;//Mark as 1 cnt++;//Length + 1 } if(a[j]!=a[i]) cnt=0;//If the person whom the last child worships is not the "first", it is not a ring maxn=max(maxn,cnt); } } cout<<maxn<<endl; return 0; }
Jumping grasshopper (bfs)
Title Description
As shown in the figure below: there are # 9 # plates arranged in # 1 # circle. Eight of the plates contained eight grasshoppers, and one was empty. We numbered these grasshoppers 1 ~ 8 clockwise.
Each grasshopper can jump to an adjacent empty plate, or it can jump over an adjacent grasshopper to an empty plate with a little force.
Please calculate that if you want to change the formation of grasshoppers to be arranged counterclockwise and keep the position of the empty disk unchanged (i.e. 1 − 8 transposition, 2 − 7 transposition,...), How many jumps does it take at least?
Regularization problem
Title Description
Consider a simple regular expression:
A regular expression consisting only of x () |.
Xiao Ming wants to find the length of the longest string that this regular expression can accept.
For example ((xx|xxx) x| (x|xx)) the longest string that XX can accept is xxxxxx, with a length of 6.
Enter description
A regular expression consisting of x() |. The input length shall not exceed 100 to ensure legality.
Output description
The length of the longest string that this regular expression can accept.
input
((xx|xxx)x|(x|xx))xx
output
6
(1) First look at the first bracket and find that there are nested brackets inside. Find the innermost bracket. Within the bracket is a or operation. ((xx|xxx)x|(x|xx))xx, get: (xxxx|(x|xx))xx
(2) Continue with the innermost parentheses. (xxxx|(x|xx))xx, get: (xxxx|xx)xx
(3) Continue with the last parenthesis. (xxxx|xx)xx, get: xxxxxx, end, get a string with length of 6.
Brackets have the highest priority, followed by | (or) brackets are a whole. Starting from the innermost bracket, take the longest reservation on both sides of |
#include <iostream> using namespace std; //Regularization problem string str; int pos=0; int dfs(){ int ans=0,tmp=0; while(pos<str.length()){ if(str[pos]=='('){//In case of left parenthesis, continue recursion (equivalent to stacking) until the last layer of parentheses pos++; tmp+=dfs(); }else if(str[pos]==')'){//Encounter the closing parenthesis, and return recursively, which is equivalent to out of the stack pos++; break; }else if(str[pos]=='x'){//x direct count encountered tmp++; pos++; }else{//When |, take the maximum value pos++; ans=max(ans,tmp); tmp=0; } } ans=max(ans,tmp); return ans; } int main() { cin>>str; cout<<dfs()<<endl; return 0; }