这道八数码老过不了 judge终于肯给我TLE了。。。还是不能AC啊
题目链接:【eight puzzle】http://acm.uestc.edu.cn/problem.php?pid=1490&cid=213
我的解题想法:双向BFS+暴力判重(表示不怎么会自己搞hash的孩纸伤不起)
#include<cstdio>
#include<string>
using namespace std;
long int head[2],tail[2];
string que[2][100000];
long int answer[2][200000];
string inst,stand;
int judge,i,j;
char d;
bool flag;
bool avmleft(long int order)
{
if (order==0) return(false);
if (order==3) return(false);
if (order==6) return(false);
return(true);
}
bool avmright(long int order)
{
if (order==2) return(false);
if (order==5) return(false);
if (order==8) return(false);
return(true);
}
bool avmup(long int order)
{
if (order==0) return(false);
if (order==1) return(false);
if (order==2) return(false);
return(true);
}
bool avmdown(long int order)
{
if (order==6) return(false);
if (order==7) return(false);
if (order==8) return(false);
return(true);
}
string mleft(string s,long int pos)
{
string tp;
long int i;
tp.resize(20);
for (i=0;i<=pos-2;i++) tp[i]=s[i];
tp[pos-1]=s[pos];
tp[pos]=s[pos-1];
for (i=pos+1;i<=8;i++) tp[i]=s[i];
tp[i+1]='\0';
return(tp);
}
string mright(string s,long int pos)
{
string tp;
long int i;
tp.resize(20);
for (i=0;i<=pos-1;i++) tp[i]=s[i];
tp[pos]=s[pos+1];
tp[pos+1]=s[pos];
for (i=pos+2;i<=8;i++) tp[i]=s[i];
tp[i+1]='\0';
return(tp);
}
string mup(string s,long int pos)
{
string tp;
long int i;
tp.resize(20);
for (i=0;i<=pos-4;i++) tp[i]=s[i];
tp[pos-3]=s[pos];
for (i=pos-2;i<=pos-1;i++) tp[i]=s[i];
tp[pos]=s[pos-3];
for (i=pos+1;i<=8;i++) tp[i]=s[i];
tp[i+1]='\0';
return(tp);
}
string mdown(string s,long int pos)
{
string tp;
long int i;
tp.resize(20);
for (i=0;i<=pos-1;i++) tp[i]=s[i];
tp[pos]=s[pos+3];
for (i=pos+1;i<=pos+2;i++) tp[i]=s[i];
tp[pos+3]=s[pos];
for (i=pos+4;i<=8;i++) tp[i]=s[i];
tp[i+1]='\0';
return(tp);
}
bool vis(string s,int t)
{
long int i;
for (i=1;i<=tail[t];i++)
if (que[t][i]==s) return(true);
return(false);
}
void print(long int sp)
{
printf("%d\n",sp);
flag=true;
}
void check(int t)
{
long int i;
for (i=1;i<=tail[1-t];i++)
if (que[1-t][i]==que[t][tail[t]])
print(answer[1-t][i]+answer[t][tail[t]]);
}
void exp(int t)
{
long int i,j,pre;
string pres,tp;
head[t]++;pres=que[t][head[t]];
pre=pres.find('x');
if (avmleft(pre))
{
tp=mleft(pres,pre);
if (!vis(tp,t))
{
(tail[t])++;
que[t][tail[t]]=tp;
answer[t][tail[t]]=answer[t][head[t]]+1;
}
check(t);
if (flag) return;
}
if (avmright(pre))
{
tp=mright(pres,pre);
if (!vis(tp,t))
{
tail[t]++;
que[t][tail[t]]=tp;
answer[t][tail[t]]=answer[t][head[t]]+1;
}
check(t);
if (flag) return;
}
if (avmup(pre))
{
tp=mup(pres,pre);
if (!vis(tp,t))
{
tail[t]++;
que[t][tail[t]]=tp;
answer[t][tail[t]]=answer[t][head[t]]+1;
}
check(t);
if (flag) return;
}
if (avmdown(pre))
{
tp=mdown(pres,pre);
if (!vis(tp,t))
{
tail[t]++;
que[t][tail[t]]=tp;
answer[t][tail[t]]=answer[t][head[t]]+1;
}
check(t);
if (flag) return;
}
}
void bfs()
{
flag=false;
head[0]=0;head[1]=0;
tail[0]=1;tail[1]=1;
answer[0][1]=0;
answer[1][1]=0;
que[0][1]=inst;
que[1][1]="12345678x";
while ((head[0]<tail[0])&&(head[1]<tail[1]))
if (tail[1]<tail[0])
{exp(1);
if (flag) return;}
else
{exp(0);
if (flag) return;}
}
int main()
{
inst.resize(20);
stand.resize(20);
while (scanf("%c",&inst[0])!=EOF)
{
scanf("%c",&d);
stand[0]='1';
for (i=1;i<=8;i++)
{
scanf("%c",&inst[i]);
scanf("%c",&d);
stand[i]=i+49;
}
inst[9]=0;
stand[8]='x';
stand[9]=0;
if (inst==stand) {print(0); continue;}
judge=0;
for (i=0;i<=8;i++)
for (j=0;j<i;j++)
{
if ((inst[j]<inst[i])&&(inst[j]!='x')&&(inst[i]!='x'))
judge++;
}
if (judge%2!=0) {printf("unsolvable\n");continue;}
bfs();
}
return 0;
}
总有一天会AC的!!!!!!!!
浙公网安备 33010602011771号