计蒜客 Minimum Distance in a Star Graph 2017icpc南宁赛区 字符串bfs
2017-09-27 21:50
531 查看
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;
}
题意:又是一长串阅读理解。总之,给你两个数字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;
}
相关文章推荐
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
- 2017 icpc 南宁赛区 J. Minimum Distance in a Star Graph(bfs)
- 2017 ACM-ICPC南宁网络赛: J. Minimum Distance in a Star Graph(BFS)
- 2017南宁网络赛J.Minimum Distance in a Star Graph(bfs求最短路)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(J.Minimum Distance in a Star Graph)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J.Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- 计蒜客 Minimum Distance in a Star Graph 思维水题
- Minimum Distance in a Star Graph(南宁网络赛)
- Minimum Distance in a Star Graph BFS/康托展开 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- Minimum Distance in a Star Graph
- ???Minimum Distance in a Star Graph
- 计蒜客 2017icpc南宁赛区 The Heaviest Non-decreasing Subsequence Problem 最长不递减子序列
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
- hihocoder1586 Minimum 2017icpc北京赛区 线段树区间最值
- BFS in Graph and BFS in Matrix