刚开始用二维BFS做出了,但是一直WA,最后看到还有一维状态,所以加个状态。。。。
但是更悲剧的事情也紧接着发生了,我没有定义起点和终点是一样的状态,而且我是用-1定义距离无穷大的。所以,很鸡巴的输出了1<<30.
改了之后,顺利A 了。。。
最后心情有点烦。。。
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define MAX_N 11
#define squre 8
int map[MAX_N][MAX_N],step[MAX_N][MAX_N][MAX_N],turn[8]={0,1,0,-1,1,0,-1,0};
int sx,sy,ex,ey,sta;
bool Init(){
cin>>sx>>sy>>ex>>ey;
if(!sx && !sy && !ex && !ey)
return false;
memset(map,-1,sizeof(map));
memset(step,-1,sizeof(step));
sta=1;
for(int k=1;k<=4;k++)
step[sx][sy][k]=0;
for(int i=1;i<=squre;i++)
for(int j=1;j<=squre;j++)
cin>>map[i][j];
return true;
}
void Bfs(){
queue<int> q;
q.push(sx);
q.push(sy);
q.push(sta);
while(!q.empty()){
int tx=q.front();q.pop();
int ty=q.front();q.pop();
int tta=q.front();q.pop();
for(int i=0;i<8;i+=2){
int dx=tx+turn[i];
int dy=ty+turn[i+1];
if(map[dx][dy]<0)
continue;
int dta= (map[dx][dy]*tta)%4+1;
if(step[dx][dy][dta]<0 ||
step[dx][dy][dta] > step[tx][ty][tta]+map[dx][dy]*tta){
step[dx][dy][dta] = step[tx][ty][tta]+map[dx][dy]*tta;
q.push(dx),q.push(dy),q.push(dta);
}
}
}
int min=1<<30;
for(int j=1;j<=4;j++){
if(step[ex][ey][j]>=0)
min=min<step[ex][ey][j]?min:step[ex][ey][j];
}
cout<<min<<endl;
}
int main(){
while(Init()){
Bfs();
}
return 0;
}