跳到主要內容

uva 11513











From Evernote:



uva 11513



bfs,做法可以先用九格基本的狀態衍生出所有的狀態,在用map存起來,之後在把所有的狀態存進去

Input:
2 3 1
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
7 3 9
2 5 1
4 8 6
1 2 3
4 5 6
7 9 8
0

[sourcecode language="cpp"]
//============================================================================
// Name : 9 Puzzle.cpp
// Date : 2013 2013/1/29 下午2:40:42
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 900005
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
map<string,int> dist;
map<string,string> moves;
char q[M][20];
char b_state[20]="123456789";
int front,rear;
void bfs(){
front=rear=0;
char str[20],ctmp[20];
moves[b_state]="";
dist[b_state]=0;
strcpy(q[front++],b_state);
//int i=20;
while(front>rear){
strcpy(str,q[rear++]);
for(int i=0;i<3;i++){
strcpy(ctmp,str);
for(int j=0;j<3;j++){
ctmp[i*3+j]=str[i*3+(j+1)%3];
}
if(!dist.count(ctmp)){
dist[ctmp]=dist[str]+1;
strcpy(q[front++],ctmp);
moves[ctmp]=moves[str]+string(1,'H')+string(1,i+'1');
}
}
for(int i=0;i<3;i++){
strcpy(ctmp,str);
for(int j=0;j<3;j++){
ctmp[i+((j+1)%3)*3]=str[i+j*3];
}
if(!dist.count(ctmp)){
dist[ctmp]=dist[str]+1;
strcpy(q[front++],ctmp);
moves[ctmp]=moves[str]+string(1,'V')+string(1,i+'1');
}
}
// printf("%s %s\n",str,moves[str].c_str());
}

}
int main() {
ios_base::sync_with_stdio(0);
// printf("dfd");
bfs();
char c;
int k=0;
char ans[20],mov[2000];
while(~scanf("%c",&c)&&c!='0'){
if(isdigit(c)){
ans[k++]=c;
}
if(k>=9){
ans[k]='\0';
// printf("%s\n",ans);
if(strcmp(ans,b_state)==0){
printf("0\n");
}
else{
strcpy(mov,moves[ans].c_str());
int len=strlen(mov);
int an=dist[ans];
if(an){
printf("%d ",an);
for(int i=len-1;i>=0;i--){
if(!(i&1)){
printf("%c%c",mov[i],mov[i+1]);
}
}
Pln();
}
else printf("Not solvable\n");
}
k=0;
Set(ans,0);
}
}

}

[/sourcecode]

留言