UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的)。

分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdlib>
#define Mod 1000000007
#define SMod 10007
#define INint 2147483647
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define ll long long
using namespace std;
#define N 500007 struct node
{
ll cantor,cost;
int pos;
bool operator <(const node& a)const
{
return cost > a.cost;
}
}S,E; priority_queue<node> que;
ll fact[] = {,,,,,,,,,,};
int dx[] = {,-,,-};
int a[],b[],Can[N][],vis[N];
ll ans,ch,cv; ll Cantor(int *a)
{
int i,j;
ll cnt;
ll res = ;
for(i=;i<;i++)
{
cnt = ;
for(j=i+;j<;j++)
if(a[i] > a[j])
cnt++;
res += cnt*fact[-i];
}
return res;
} void getcantor(ll cantor,int *a)
{
for(int i=;i<;i++)
Can[cantor][i] = a[i];
} void geta(ll cantor)
{
for(int i=;i<;i++)
a[i] = Can[cantor][i];
} void bfs()
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis));
int i,j;
que.push(S);
//vis[S.cantor] = 1;
while(!que.empty())
{
node tmp = que.top();
que.pop();
ll cantor = tmp.cantor;
ll cost = tmp.cost;
int pos = tmp.pos;
if(vis[cantor])
continue;
vis[cantor] = ;
if(cost >= ans)
continue;
if(cantor == E.cantor)
{
ans = min(ans,cost);
break;
}
geta(cantor);
for(int k=;k<;k++)
{
int v = (pos+dx[k]+)%;
swap(a[v],a[pos]);
ll newcantor = Cantor(a);
if(vis[newcantor])
{
swap(a[v],a[pos]);
continue;
}
getcantor(newcantor,a);
swap(a[v],a[pos]);
node now;
now.cantor = newcantor;
now.pos = v;
if(k < )
now.cost = cost + ch;
else
now.cost = cost + cv;
if(now.cost >= ans)
continue;
//vis[newcantor] = 1;
que.push(now);
}
}
} int main()
{
int i,j;
while(scanf("%lld%lld",&ch,&cv) && (ch||cv))
{
for(i=;i<;i++)
{
scanf("%d",&a[i]);
if(a[i] == )
S.pos = i;
}
S.cantor = Cantor(a);
S.cost = ;
for(i=;i<;i++)
scanf("%d",&b[i]);
E.cantor = Cantor(b);
getcantor(S.cantor,a);
getcantor(E.cantor,b);
ans = (1LL<<);
bfs();
printf("%lld\n",ans);
}
return ;
}
上一篇:同级、父子级div定位


下一篇:HTML+CSS+JavaScript+sql+数据库题目