HDU 1584 Spider cards

HDU 1584 Spider cards (dfs)

Here Insert Picture Description
Topic link: http: //acm.hdu.edu.cn/showproblem.php pid = 1584?

Because spider cards are small cards in large cards above, so you can start looking for small cards, 10 cards will be able to finish 9th sort rows, the current is greater than the minimum number of steps equal to the number of steps that have been found unnecessary.
Every row does not find a good brand, to find his change of position discharged, because there is only one method can be ranked, so after finding to exit the current cycle, otherwise I might be wrong

#include<bits/stdc++.h>
using namespace std;
int num[15],vis[15],minn;
inline void dfs(int v,int step){
	if(step>=minn)return;
	if(v==9){
		minn=step;
		return;
	}
	for(int i=1;i<=10;i++){
		if(vis[i]==0){
			vis[i]=1;
			for(int j=i+1;j<=10;j++){
				if(vis[j]==0){
					dfs(v+1,step+abs(num[i]-num[j]));
					break;
				}
			}
			vis[i]=0;
		}
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		for(int i=0;i<=9;i++){
			int ans;
			scanf("%d",&ans);
			num[ans]=i;
			vis[i]=0;
		}
		minn=100000;
		dfs(0,0);
		printf("%d\n",minn);
	}
	return 0;
}
Published 35 original articles · won praise 3 · Views 901

Guess you like

Origin blog.csdn.net/weixin_43823753/article/details/104479694