Seven Puzzle (AOJ 0121 bfs)

2015-07-20 17:09:06 ? ×÷Õß: ? ä¯ÀÀ: 2

?

Seven Puzzle

Time Limit : 1 sec, Memory Limit : 65536 KB

Seven Puzzle

7¥Ñ¥º¥ë¤Ï8¤Ä¤ÎÕý·½ÐΤΥ«?¥É¤È¤³¤ì¤é¤Î¥«?¥É¤¬¤Ô¤¿¤ê¤È?¤Þ¤ë?¤òʹ¤Ã¤ÆÐФ¤¤Þ¤¹¡£¤½¤ì¤¾¤ì¤Î¥«?¥É¤Ï»¥¤¤¤ËÇø?¤Ç¤­¤ë¤è¤¦¤Ë¡¢0,1,2....7¤È·¬ºÅ¤¬¤Ä¤±¤é¤ì¤Æ¤¤¤Þ¤¹¡£?¤Ë¤Ï¡¢?¤Ë2?¡¢ºá¤Ë4?¤Î¥«?¥É¤ò?¤Ù¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

7¥Ñ¥º¥ë¤òʼ¤á¤ë¤È¤­¤Ë¤Ï¡¢¤Þ¤º?¤Ë¤¹¤Ù¤Æ¤Î¥«?¥É¤òÈë¤ì¤Þ¤¹¡£?¤Î¤Ê¤«¤Ç0¤Î¥«?¥É¤À¤±¤Ï¡¢ÉÏÏÂ×óÓÒ¤Ë?½Ó¤¹¤ë¥«?¥É¤ÈλÖäò½»?¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£¤¿¤È¤¨¤Ð¡¢?¤Î×´?¤¬?( a )¤Î¤È¤­¤Ë¡¢0¤Î¥«?¥É¤ÎÓÒ¤Ë?½Ó¤·¤¿¡¢7¤Î¥«?¥É¤ÈλÖäò½»?¤¹¤ì¤Ð¡¢?( b )¤Î×´?¤Ë¤Ê¤ê¤Þ¤¹¡£¤¢¤ë¤¤¤Ï¡¢?( a )¤Î×´?¤«¤é0¤Î¥«?¥É¤ÎϤË?½Ó¤·¤¿2¤Î¥«?¥É¤ÈλÖäò½»?¤¹¤ì¤Ð?( c )¤Î×´?¤Ë¤Ê¤ê¤Þ¤¹¡£¥«?¥É¤ÎλÖäòÈë¤ìÌæ¤¨¤ë²Ù×÷¤Ï¤³¤ì¤À¤±¤¬?¤µ¤ì¤Þ¤¹¡£?( a )¤Î×´?¤Ç0¤Î¥«?¥É¤ÈÉÏÏÂ×óÓÒ¤Ë?½Ó¤¹¤ë¥«?¥É¤Ï7¤È2¤Î¥«?¥É¤À¤±¤Ê¤Î¤Ç¡¢¤³¤ìÒÔÍâ¤ÎλÖäÎÈë¤ìÌæ¤¨¤Ï?¤µ¤ì¤Þ¤»¤ó¡£

¥²?¥à¤ÎÄ¿µÄ¤Ï¡¢¥«?¥É¤ò¤­¤ì¤¤¤ËÕûÁФ·¤Æ?( d )¤Î×´?¤Ë¤¹¤ë¤³¤È¤Ç¤¹¡£×î³õ¤Î×´?¤òÈëÁ¦¤È¤·¡¢¥«?¥É¤ò¤­¤ì¤¤¤ËÕûÁФ¹¤ë¤Þ¤Ç¤Ë¡¢±ØÒª¤Ê×îСÊÖÊý¤ò³öÁ¦¤·¤Æ?Áˤ¹¤ë¥×¥í¥°¥é¥à¤ò×÷³É¤·¤Æ¤¯¤À¤µ¤¤¡£¤¿¤À¤·¡¢ÈëÁ¦¤µ¤ì¤¿¥«?¥É¤Î×´?¤«¤é¤Ï?( d )¤Î×´?¤ËÒÆ¤ë¤³¤È¤Ï¿ÉÄܤǤ¢¤ë¤È¤·¤Þ¤¹¡£

ÈëÁ¦¥Ç?¥¿¤Ï¡¢1ÐФË8¤Ä¤ÎÊý×Ö¤¬Ó뤨¤é¤ì¤Þ¤¹¡£¤³¤ì¤é¤Ï¡¢×î³õ¤Î×´?¤Î¥«?¥É¤Î?¤Ó¤ò±í¤·¤Þ¤¹¡£?( a )¤ÎÊý×Ö±í?¤Ï0,7,3,4,2,5,1,6¤Ë¡¢?( c )¤Ï2,7,3,4,0,5,1,6¤È¤Ê¤ê¤Þ¤¹¡£

\ \
?( a ) 0,7,3,4,2,5,1,6¤Î?ºÏ ?( b ) 7,0,3,4,2,5,1,6¤Î?ºÏ


\ \
?( c ) 2,7,3,4,0,5,1,6¤Î?ºÏ ?( d ) 0,1,2,3,4,5,6,7(×î?×´?)

Input

1¤ÄÄ¿¤Î¥Ñ¥º¥ë¤Î×´?(ÕûÊý;¿Õ°×ÇøÇФê)
2¤ÄÄ¿¤Î¥Ñ¥º¥ë¤Î×´?(ÕûÊý;¿Õ°×ÇøÇФê)
     :
     :

Ó뤨¤é¤ì¤ë¥Ñ¥º¥ë¤ÎÊý¤Ï1000ÒÔϤǤ¹¡£

Output

1¤ÄÄ¿¤Î¥Ñ¥º¥ë¤Î×´?¤«¤é×î?×´?¤ØÒÆÐФ¹¤ë×îСÊÖÊý(ÕûÊý)
2¤ÄÄ¿¤Î¥Ñ¥º¥ë¤Î×´?¤«¤é×î?×´?¤ØÒÆÐФ¹¤ë×îСÊÖÊý(ÕûÊý)
            :
            :

Sample Input

0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0

Output for the Sample Input

0
1
28
ÌâÒ⣺¸ø³ö0~7µÄÐòÁУ¨2*4£©£¬Ö»ÄÜ0ºÍÖÜΧÊý×Ö½»»»£¬ÎÊ×îÉÙ½»»»¶àÉٴοÉÒÔÊǸø³öµÄÐòÁбä³É0 1 2 3 4 5 6 7 ˼·£ºÏÈËÑÒ»±é°ÑËùÓд𰸱£´æÏÂÀ´¡£ ´ó¿ÞÒòΪһ¾ä»°Î»ÖÃд´íÁ˵±Ê±Ã»×ö³öÀ´¡£¡£ ´úÂ룺
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct St { int x,y; int step; int g[2][4]; }; int dir[4][2]={0,1,0,-1,1,0,-1,0}; int mp[2][4]; map
             
              ans; int a[8]; int here(St s) { int i; int x=0; FRL(i,0,4) x=x*10+s.g[0][i]; FRL(i,0,4) x=x*10+s.g[1][i]; return x; } bool ok(St st) { if (st.x>=0&&st.x<2&&st.y>=0&&st.y<4) return true; return false; } void bfs(int sx,int sy) { int i; queue
              
               Q; while (!Q.empty()) Q.pop(); St st,now; st.x=sx; st.y=sy; st.step=0; memcpy(st.g,mp,sizeof(mp)); ans[here(st)]=0; Q.push(st); while (!Q.empty()) { st=Q.front(); Q.pop(); FRL(i,0,4) { now.x=st.x+dir[i][0]; now.y=st.y+dir[i][1]; if (ok(now)) { memcpy(now.g,st.g,sizeof(st.g)); swap(now.g[st.x][st.y],now.g[now.x][now.y]); int xx=here(now); if (!ans[xx]) { now.step=st.step+1; ans[xx]=now.step; Q.push(now); } } } } } int main() { int i,j; ans.clear(); FRL(i,0,4) mp[0][i]=i; FRL(i,0,4) mp[1][i]=i+4; int sx,sy; bfs(0,0); while (~scanf("%d",&a[0])) { FRE(i,1,7) sf(a[i]); St st; FRL(i,0,4) st.g[0][i]=a[i]; FRL(i,0,4) st.g[1][i]=a[i+4]; int xx=here(st); pf("%d\n",ans[xx]); } return 0; } 
              
             
            
           
          
         
       
      
     
    
   
  



?

?

-->

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ: