标签:des style blog http java color
先上题目:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11243 Accepted Submission(s): 3022
Special Judge
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 #define MAX 400002 7 using namespace std; 8 typedef struct Node{ 9 int maze[3][3]; 10 int y,x; 11 int hash; 12 int h,g; 13 bool isok(){ 14 return (0<=y && y<3 && 0<=x && x<3); 15 } 16 17 bool operator < (const Node a) const{ 18 if(h>a.h) return 1; 19 if(h==a.h && g>a.g) return 1; 20 return 0; 21 } 22 23 }Node; 24 Node s; 25 const int cantor[9] = {1,1,2,6,24,120,720,5040,40320}; 26 const int target = 322560; 27 const int cy[]={0,0,1,-1}; 28 const int cx[]={1,-1,0,0}; 29 int vis[MAX]; 30 int pre[MAX]; 31 priority_queue<Node> p; 32 bool check(Node c){ 33 int a[9]; 34 int k=0,sum=0; 35 for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=c.maze[i][j]; 36 for(int i=0;i<k;i++) for(int j=i+1;j<k;j++) if(a[i] && a[j] && a[i]>a[j]) sum++; 37 return !(sum&1); 38 } 39 40 int getHash(Node c){ 41 int a[9]; 42 int k=0,h=0,sum=0; 43 for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=c.maze[i][j]; 44 for(int i=0;i<k;i++){ 45 sum=0; 46 for(int j=0;j<i;j++){ 47 if(a[i]<a[j]) sum++; 48 } 49 h+=sum*cantor[i]; 50 } 51 return h; 52 } 53 54 int getH(Node c){ 55 int res=0; 56 for(int i=0;i<3;i++) for(int j=0;j<3;j++){ 57 res+=abs(i-(c.maze[i][j]-1)/3)+abs(j-(c.maze[i][j]-1)%3); 58 } 59 return res; 60 } 61 62 void astar(){ 63 while(!p.empty()) p.pop(); 64 memset(vis,-1,sizeof(vis)); 65 memset(pre,-1,sizeof(pre)); 66 Node u,v; 67 s.h = getH(s); 68 p.push(s); 69 vis[s.hash]=-2; 70 while(!p.empty()){ 71 u = p.top(); 72 p.pop(); 73 for(int i=0;i<4;i++){ 74 v=u;v.y+=cy[i];v.x+=cx[i]; 75 if(!v.isok()) continue; 76 swap(v.maze[v.y][v.x],v.maze[u.y][u.x]); 77 if(!check(v)) continue; 78 v.hash = getHash(v); 79 if(vis[v.hash]!=-1) continue; 80 vis[v.hash]=i; pre[v.hash]=u.hash; 81 v.g++; 82 v.h=getH(v); 83 p.push(v); 84 if(v.hash == target) return ; 85 86 } 87 } 88 89 } 90 91 bool scan(){ 92 char ch[100]; 93 if(!gets(ch)) return 0; 94 int k=0,l=strlen(ch); 95 for(int i=0;i<3;i++) for(int j=0;j<3;j++){ 96 while(k<l && ch[k]==‘ ‘) k++; 97 if(ch[k]==‘x‘){ 98 s.maze[i][j]=0; 99 s.y=i; s.x=j; 100 } 101 else s.maze[i][j]=ch[k]-‘0‘; 102 k++; 103 } 104 return 1; 105 } 106 107 void printPath(){ 108 string ss; 109 ss.clear(); 110 int next = target; 111 while(pre[next]!=-1){ 112 if(vis[next]==0) ss+=‘r‘; 113 else if(vis[next]==1) ss+=‘l‘; 114 else if(vis[next]==2) ss+=‘d‘; 115 else ss+=‘u‘; 116 next = pre[next]; 117 } 118 for(int i=(int)ss.length()-1;i>=0;i--) putchar(ss[i]); 119 puts(""); 120 } 121 122 int main() 123 { 124 //freopen("data.txt","r",stdin); 125 while(scan()){ 126 if(!check(s)){ 127 puts("unsolvable"); 128 }else{ 129 s.hash = getHash(s); 130 if(s.hash == target){ 131 puts(""); 132 }else{ 133 astar(); 134 printPath(); 135 } 136 } 137 } 138 return 0; 139 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #define MAX 400002 7 using namespace std; 8 9 const int cantor[] = {1,1,2,6,24,120,720,5040,40320}; 10 const int target = 322560; 11 int vis[MAX],pre[MAX]; 12 int cy[]={0,1,0,-1}; 13 int cx[]={1,0,-1,0}; 14 typedef struct Node{ 15 int maze[3][3]; 16 int h,g,y,x; 17 int hash; 18 19 bool isok(){ 20 return (0<=y && y<3 && 0<=x && x<3); 21 } 22 23 bool islegal(){ 24 int a[9],k=0,sum=0; 25 for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=maze[i][j]; 26 for(int i=0;i<k;i++) for(int j=i+1;j<k;j++) if(a[i] && a[j] && a[i]>a[j]) sum++; 27 return !(sum&1); 28 } 29 30 bool operator < (const Node o)const{ 31 return h==o.h ? g>o.g : h>o.h; 32 } 33 }Node; 34 Node s; 35 priority_queue<Node> q; 36 37 int getHash(Node e){ 38 int a[9],k=0,res=0; 39 for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=e.maze[i][j]; 40 for(int i=0;i<k;i++){ 41 int t=0; 42 for(int j=0;j<i;j++) if(a[i]<a[j]) t++; 43 res+=t*cantor[i]; 44 } 45 return res; 46 } 47 48 int getH(Node e){ 49 int res=0; 50 for(int i=0;i<3;i++) for(int j=0;j<3;j++){ 51 res+=abs(i-(e.maze[i][j]-1)/3)+abs(j-(e.maze[i][j]-1)%3); 52 } 53 return res; 54 } 55 56 void astar(){ 57 Node u,v; 58 while(!q.empty()) q.pop(); 59 memset(vis,-1,sizeof(vis)); 60 memset(pre,-1,sizeof(pre)); 61 s.g=0; 62 s.h=getH(s); 63 q.push(s); 64 vis[s.hash]=-2; 65 while(!q.empty()){ 66 u=q.top(); 67 q.pop(); 68 //cout<<u.hash<<endl; 69 for(int i=0;i<4;i++){ 70 v=u;v.y+=cy[i];v.x+=cx[i]; 71 if(v.isok()){ 72 swap(v.maze[v.y][v.x],v.maze[u.y][u.x]); 73 if(v.islegal()){ 74 v.hash = getHash(v); 75 if(vis[v.hash]==-1){ 76 vis[v.hash]=i; 77 pre[v.hash]=u.hash; 78 v.g++; v.h=getH(v); 79 q.push(v); 80 if(v.hash == target) return ; 81 } 82 } 83 84 } 85 } 86 } 87 } 88 89 void printPath(){ 90 string ss=""; 91 int next=target; 92 while(pre[next]!=-1){ 93 switch(vis[next]){ 94 case 0:ss+=‘r‘;break; 95 case 1:ss+=‘d‘;break; 96 case 2:ss+=‘l‘;break; 97 case 3:ss+=‘u‘;break; 98 } 99 next=pre[next]; 100 } 101 for(int i=(int)ss.length()-1;i>=0;i--) putchar(ss[i]); 102 putchar(‘\n‘); 103 } 104 105 bool get(){ 106 char ss[100]; 107 if(gets(ss)){ 108 int k=0,l=strlen(ss); 109 for(int i=0;i<3;i++) for(int j=0;j<3;j++){ 110 while(k<l && ss[k]==‘ ‘) k++; 111 if(ss[k]==‘x‘){ 112 s.maze[i][j]=0; 113 s.y=i; s.x=j; 114 }else s.maze[i][j]=ss[k]-‘0‘; 115 k++; 116 } 117 return 1; 118 } 119 return 0; 120 121 } 122 123 int main() 124 { 125 //freopen("data.txt","r",stdin); 126 while(get()){ 127 if(s.islegal()){ 128 s.hash = getHash(s); 129 if(s.hash == target) puts(""); 130 else{ 131 astar(); 132 printPath(); 133 } 134 }else puts("unsolvable"); 135 } 136 return 0; 137 }
HDU - 1043 - Eight / POJ - 1077 - Eight,布布扣,bubuko.com
HDU - 1043 - Eight / POJ - 1077 - Eight
标签:des style blog http java color
原文地址:http://www.cnblogs.com/sineatos/p/3840370.html