这是我的90分代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int goal[7][7]={
{0,0,0,0,0,0},
{0,1,1,1,1,1},
{0,0,1,1,1,1},
{0,0,0,2,1,1},
{0,0,0,0,0,1},
{0,0,0,0,0,0}
};
int t;
int pd;
char ch;
int x,y;
int map[6][6];
const int dx[]={0,1,1,2,2,-2,-2,-1,-1};
const int dy[]={0,2,-2,1,-1,1,-1,2,-2};
int work() {
int cnt = 0;
for(int i = 1;i <= 5;i++) {
for(int j = 1;j <= 5;j++) {
if(map[i][j] != goal[i][j]) {
cnt++;
}
}
}
return cnt;
}
void A_star(int step,int x,int y,int maxdep,int pre) {
if(step == maxdep) {
if(work() == 0) pd = 1;
return ;
}
if(pd)
return ;
for(int i = 1;i <= 8;i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 1 || xx > 5 || yy < 1 || yy > 5 || pre + i == 9)
continue;
int tt = work();
swap(map[x][y],map[xx][yy]);
if(tt + step <= 15 && pd == 0)
A_star(step + 1,xx,yy,maxdep,i);
swap(map[x][y],map[xx][yy]);
}
}
int main() {
cin >> t;
while(t--) {
pd = 0;
for(int i = 1;i <= 5;i++) {
for(int j = 1;j <= 5;j++) {
cin >> ch;
if(ch == '*') {
map[i][j] = 2;
x = i;
y = j;
}
else
map[i][j] = ch - '0';
}
}
if(work() == 0) {
cout << 0 << endl;
continue;
}
bool flag = false;
for(int k = 1;k <= 15;k++) {
A_star(0,x,y,k,-1);
if(pd) {
cout << k << endl;
goto THERE;
}
}
cout << -1 << endl;
THERE:;
}
return 0;
}
但是后来我在检查的时候发现这一句
if(tt + step <= 15 && pd == 0)
中的15应该改成maxdep,但是改了之后连样例都过不了(都输出-1)提交也是变成0分,但是正解不就应该是maxdep吗QAQ