标签:style blog http color io os ar java for
2 12X453786 12345678X 564178X23 7568X4123
Case 1: 2 dd Case 2: 8 urrulldr
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 3; 18 struct sta{ 19 int x,y; 20 sta(int a = 0,int b = 0){ 21 x = a; 22 y = b; 23 } 24 }; 25 char mp[maxn][maxn]; 26 sta s[11]; 27 int nowx,nowy; 28 int h(){ 29 int tmp = 0; 30 for(int i = 0; i < 9; i++){ 31 int x = i/3,y = i%3; 32 if(mp[x][y] == ‘X‘) continue; 33 tmp += abs(x - s[mp[x][y]-‘0‘].x) + abs(y - s[mp[x][y]-‘0‘].y); 34 } 35 return tmp; 36 } 37 int ans[200],limit; 38 const int dir[4][2] = {1,0,0,-1,0,1,-1,0}; 39 const char d[4] = {‘d‘,‘l‘,‘r‘,‘u‘}; 40 bool ok; 41 int IDAstar(int x,int y,int p,int cur){ 42 int bound = INF,tmp; 43 int hv = h(); 44 if(cur + hv > limit) return cur + hv; 45 if(hv == 0) {ok = true;return cur;} 46 for(int i = 0; i < 4; i++){ 47 if(i == p) continue; 48 int tx = x + dir[i][0]; 49 int ty = y + dir[i][1]; 50 if(tx < 0 || tx >= 3 || ty < 0 || ty >= 3) continue; 51 swap(mp[x][y],mp[tx][ty]); 52 ans[cur] = i; 53 int nbound = IDAstar(tx,ty,3-i,cur+1); 54 if(ok) return nbound; 55 bound = min(bound,nbound); 56 swap(mp[x][y],mp[tx][ty]); 57 } 58 return bound; 59 } 60 int main() { 61 int t,cs = 1; 62 char ch; 63 scanf("%d",&t); 64 getchar(); 65 while(t--){ 66 for(int i = 0; i < 9; i++){ 67 ch = getchar(); 68 if(ch == ‘X‘){ 69 nowx = i/3; 70 nowy = i%3; 71 } 72 mp[i/3][i%3] = ch; 73 } 74 getchar(); 75 for(int i = 0; i < 9; i++){ 76 ch = getchar(); 77 if(ch == ‘X‘) continue; 78 s[ch-‘0‘] = sta(i/3,i%3); 79 } 80 getchar(); 81 limit = h(); 82 ok = false; 83 while(!ok) limit = IDAstar(nowx,nowy,-10,0); 84 printf("Case %d: %d\n",cs++,limit); 85 for(int i = 0; i < limit; i++) 86 putchar(d[ans[i]]); 87 putchar(‘\n‘); 88 } 89 return 0; 90 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4003439.html