G - Mad Veterinarian

来源:互联网 发布:手机号定位软件免费 编辑:程序博客网 时间:2024/05/16 06:19
/*UVALive6178题目和hdu4490一样,貌似数据强度不是一个档次。或者spj有问题?= =据说以前AC过的代码现在不能AC了?囧写了好长时间,跪了20发,感觉状态上界可以用数学求?= =数据范围也不告诉。。这样真的好么?想不通了....水过了hdu...构造了一个简单估价函数。唯一需要注意的一点是为个数零的动物不可能继续转换。(也就是不可能找别人借,然后填上)*///#pragma comment(linker, "/STACK:167772160")#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <set>#include <map>#include <vector>#include <queue>#include <ctime>#include <cmath>#include <cassert>//#include <fstream>#include <stack>#include <cctype>#define MP make_pair#define PB push_back#define SZ(x) (int)x.size()#define INF 1<<29#define pii pair<int,int>#define pll pair<LL,LL>#define vi vector<int>#define L(x) x<<1#define R(x) x<<1|1#if(_WIN32||__WIN32__)    #define LL __int64    #define ll I64#else    #define LL long long#endif//#define Localusing namespace std;map<vi,int>Map;int dir[4][4],Max_dir[4];vi goal;const string s1="ABC";const string s2="abc";struct Node{vi statu;string path;double A_star;void h(){double ret=0;for(int i=0;i<SZ(statu);i++){if(Max_dir[i]>0)ret+=1.0*abs(goal[i]-statu[i])/Max_dir[i];elseret+=abs(goal[i]-statu[i]);}A_star=ret+Map[statu];}bool friend operator <(const Node a,const Node b){return a.A_star>b.A_star;}};int BFS(vi st,vi ed,string& path){Map.clear();Node now=(Node){st,""};now.h();priority_queue<Node>q;q.push(now);int count=0;while(!q.empty()){if(count++>=6000)return -1;now=q.top();q.pop();bool flag=false;for(int i=0;i<3;i++)if(now.statu[i]>50)flag=true;if(flag)continue;if(now.statu==ed){path=now.path;return true;}for(int d=0;d<2;d++){for(int i=0;i<3;i++){Node next=now;if(d==0){next.statu[i]-=1;if(next.statu[0]<0||next.statu[1]<0||next.statu[2]<0)continue;for(int k=0;k<3;k++)next.statu[k]+=dir[i][k];if(Map.count(next.statu))continue;Map[next.statu]=Map[now.statu]+1;next.path+=s1[i];next.h();q.push(next);}else{for(int k=0;k<3;k++)next.statu[k]-=dir[i][k];if(next.statu[0]<0||next.statu[1]<0||next.statu[2]<0)continue;next.statu[i]+=1;if(Map.count(next.statu))continue;Map[next.statu]=Map[now.statu]+1;next.path+=s2[i];next.h();q.push(next);}}}}return -1;}int main(){int T;scanf("%d",&T);while(T--){int Case,n;scanf("%d %d",&Case,&n);memset(Max_dir,0,sizeof(Max_dir));for(int i=0;i<3;i++)for(int j=0;j<3;j++){scanf("%d",&dir[i][j]);Max_dir[j]=max(dir[i][j],Max_dir[j]);}printf("%d %d\n",Case,n);for(int i=1;i<=n;i++){int __;scanf("%d",&__);vi st,ed;int x,y,z;scanf("%d %d %d",&x,&y,&z);st.PB(x),st.PB(y),st.PB(z);scanf("%d %d %d",&x,&y,&z);ed.PB(x),ed.PB(y),ed.PB(z);goal=ed;printf("%d ",__);string s;if(BFS(st,ed,s)==-1)puts("NO SOLUTION");elsecout<<Map[ed]<<" "<<s<<endl;}}    return 0;}/*21 20 1 01 1 11 1 01 0 1 1 0 1 12 1 0 0 2 0 02 20 3 40 0 50 0 32 2 0 0 0 0 41 2 0 0 0 0 511 20 1 01 0 10 0 21 1 1 4 2 0 02 2 0 0 0 0 2*/

0 0