Advertisement
arnab_bhattacharjee

H1

Sep 23rd, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <queue>
  4. using namespace std;
  5. #define SIZE 3
  6. bool isPrime[18]={false,false,true,true,false,true,false,true,false,false,false,true,false,true,false,false,false,true};
  7.  
  8. class TrieNode{
  9.     private:
  10.     static const int ALPHABETSIZE=9;
  11.     public:
  12.     int val;
  13.     TrieNode *edges[ALPHABETSIZE];
  14.     TrieNode(){val=-1; for(int i=0;i<ALPHABETSIZE;i++) edges[i]=NULL;}
  15. };
  16.  
  17. class Trie{
  18.     private:
  19.     TrieNode *root;
  20.     public:
  21.     void insert(string s,int val);
  22.     TrieNode *find(string s);
  23.     Trie(){root=new TrieNode();}
  24. };
  25.  
  26. void Trie::insert(string s,int val){
  27.     TrieNode *tmp=root;
  28.     for(int i=0;s[i];i++){
  29.         if(!tmp->edges[s[i]-'1']){
  30.             TrieNode *newNode=new TrieNode();
  31.             tmp->edges[s[i]-'1']=newNode;
  32.         }
  33.         tmp=tmp->edges[s[i]-'1'];
  34.     }
  35.     tmp->val=val;
  36. }
  37.  
  38. TrieNode *Trie::find(string s){
  39.     TrieNode *tmp=root;
  40.     for(int i=0;s[i];i++){
  41.         if(!tmp->edges[s[i]-'1']) return NULL;
  42.         tmp=tmp->edges[s[i]-'1'];
  43.     }
  44.     return tmp;
  45. }
  46.  
  47. Trie *T=new Trie();
  48.  
  49. class Puzzle{
  50.     public:
  51.     int moves;
  52.     int **matrix;
  53.     Puzzle(int **s){
  54.         moves=0;
  55.         matrix=new int*[SIZE];
  56.         for(int i=0;i<SIZE;i++) matrix[i]=new int[SIZE];
  57.         for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) matrix[i][j]=s[i][j];
  58.     }
  59.     Puzzle(){
  60.         moves=0;
  61.         matrix=new int*[SIZE];
  62.         for(int i=0;i<SIZE;i++) matrix[i]=new int[SIZE];
  63.     }
  64. };
  65. queue<Puzzle *> Q;
  66.  
  67. string getMapping(int **s){
  68.     string str="";
  69.     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++)  str+='0'+s[i][j];
  70.     return str;
  71. }
  72.  
  73. bool isGoal(int **s){
  74.     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) if(s[i][j]!=(i*SIZE+j+1)) return false;
  75.     return true;
  76. }
  77.  
  78. void print(Puzzle *p){
  79.     cout<<p->moves<<endl;
  80.     for(int i=0;i<SIZE;i++){ for(int j=0;j<SIZE;j++) cout<<p->matrix[i][j]<<'\t'; cout<<endl;}
  81.     cout<<endl;
  82. }
  83.  
  84. void solve(){
  85.     while(!Q.empty()){
  86.         //cout<<M.size()<<endl;
  87.         Puzzle *src=Q.front();
  88.         //print(src);
  89.         Q.pop();
  90.         for(int i=0;i<SIZE;i++)
  91.             for(int j=0;j<SIZE;j++){
  92.                 if(i-1>=0 && isPrime[src->matrix[i-1][j]+src->matrix[i][j]]){
  93.                     Puzzle *tmp=new Puzzle(src->matrix);
  94.                     swap(tmp->matrix[i-1][j],tmp->matrix[i][j]);
  95.                     tmp->moves=src->moves+1;
  96.                     //if(isGoal(tmp->matrix)){cout<<tmp->moves<<endl; return;}
  97.                     string img=getMapping(tmp->matrix);
  98.                     TrieNode *t=T->find(img);
  99.                     if(!t){T->insert(img,tmp->moves); Q.push(tmp);}
  100.                 }
  101.                 if(i+1<SIZE && isPrime[src->matrix[i+1][j]+src->matrix[i][j]]){
  102.                     Puzzle *tmp=new Puzzle(src->matrix);
  103.                     swap(tmp->matrix[i+1][j],tmp->matrix[i][j]);
  104.                     tmp->moves=src->moves+1;
  105.                     //if(isGoal(tmp->matrix)){cout<<tmp->moves<<endl; return;}
  106.                     string img=getMapping(tmp->matrix);
  107.                     TrieNode *t=T->find(img);
  108.                     if(!t){T->insert(img,tmp->moves); Q.push(tmp);}
  109.                 }
  110.                 if(j-1>=0 && isPrime[src->matrix[i][j-1]+src->matrix[i][j]]){
  111.                     Puzzle *tmp=new Puzzle(src->matrix);
  112.                     swap(tmp->matrix[i][j-1],tmp->matrix[i][j]);
  113.                     tmp->moves=src->moves+1;
  114.                     //if(isGoal(tmp->matrix)){cout<<tmp->moves<<endl; return;}
  115.                     string img=getMapping(tmp->matrix);
  116.                     TrieNode *t=T->find(img);
  117.                     if(!t){T->insert(img,tmp->moves); Q.push(tmp);}
  118.                 }
  119.                 if(j+1<SIZE && isPrime[src->matrix[i][j+1]+src->matrix[i][j]]){
  120.                     Puzzle *tmp=new Puzzle(src->matrix);
  121.                     swap(tmp->matrix[i][j+1],tmp->matrix[i][j]);
  122.                     tmp->moves=src->moves+1;
  123.                     //if(isGoal(tmp->matrix)){cout<<tmp->moves<<endl; return;}
  124.                     string img=getMapping(tmp->matrix);
  125.                     TrieNode *t=T->find(img);
  126.                     if(!t){T->insert(img,tmp->moves); Q.push(tmp);}
  127.                 }
  128.             }
  129.            
  130.     delete src;
  131.     }
  132. }
  133.  
  134. int main(){
  135.     ios_base::sync_with_stdio(false);
  136.     Puzzle *src=new Puzzle();
  137.     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) src->matrix[i][j]=i*3+j+1;
  138.     Q.push(src);
  139.     T->insert(getMapping(src->matrix),src->moves);
  140.     solve();
  141.     int tests;
  142.     cin>>tests;
  143.     while(tests--){
  144.         Puzzle *tmp=new Puzzle();
  145.         for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) cin>>tmp->matrix[i][j];
  146.         string str=getMapping(tmp->matrix);
  147.         TrieNode *t=T->find(str);
  148.         if(t!=NULL) cout<<t->val<<endl;
  149.         else{cout<<"-1"<<endl;}
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement