计蒜客 Minimum Distance in a Star Graph 2017icpc南宁赛区 字符串bfs

https://nanti.jisuanke.com/t/17317

题意:又是一长串阅读理解。总之,给你两个数字a,b。操作:a的第一位数字可与其它位交换。现在求最小的操作次数使a=b。

题解:bfs,顺便记录已经出现过的数字(剪枝)。

代码:

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>

#include<iomanip>
#include<iostream>

#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 9 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
int n;

struct node{
	string a;
	int len;
}a,b,temp;

set<string> s;

int bfs(){
	queue<node> q;
	q.push(a);
	s.insert(a.a);
	while(!q.empty()){
		a=q.front();q.pop();
		if(a.a==b.a){
			return a.len;
		}
		for(int i=1;i<n;i++){
			temp.a=a.a;
			temp.len=a.len+1;
			swap(temp.a[0],temp.a[i]);
			if(s.find(temp.a)==s.end()){
				s.insert(temp.a);
				q.push(temp);
			}
		}
	}
	return 0;
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=5;i++){
		s.clear();
		a.len=b.len=0;
		cin>>a.a>>b.a;
		printf("%d\n",bfs());
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To parse a complex, densely nested object graph into a more straightforward model for use in another domain, you can use a technique called "object mapping" or "object transformation". This involves mapping the complex object graph to a simpler model that is more appropriate for the other domain. Here are the general steps for object mapping: 1. Define the target model: Define the simpler model that you want to map to. This model should have only the properties that are relevant to the other domain. 2. Define the mapping rules: Define the rules for mapping the properties from the complex object graph to the properties in the target model. This may involve combining or filtering properties, renaming properties, or converting data types. 3. Implement the mapping: Implement the mapping rules using a mapping library or manually. One popular mapping library for Java is MapStruct, but there are many others available. Here's an example of object mapping using MapStruct: Suppose you have a complex object graph representing a library with many books, authors, and publishers. You want to map this to a simpler model that only includes the title, author name, and publisher name for each book. 1. Define the target model: ``` public class BookInfo { private String title; private String authorName; private String publisherName; // getters and setters } ``` 2. Define the mapping rules: ``` @Mapper public interface LibraryMapper { @Mapping(source = "book.title", target = "title") @Mapping(source = "book.author.name", target = "authorName") @Mapping(source = "book.publisher.name", target = "publisherName") BookInfo mapBookToBookInfo(Book book); } ``` 3. Implement the mapping: ``` LibraryMapper mapper = Mappers.getMapper(LibraryMapper.class); List<BookInfo> bookInfos = library.getBooks().stream() .map(mapper::mapBookToBookInfo) .collect(Collectors.toList()); ``` This will map the complex object graph to a simpler model that is more appropriate for use in another domain.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值