#include <iostream>
using namespace std;
const int N=6;
const int D=4;
const int INF=100000000;
int direct[4][2] =
{
{1, 0}, {-1, 0}, {0, 1}, {0, -1}// 右,左,上,下
};
int g[N][N]={0};
int visit[N][N]={0};
int mincost=0;
bool checkInBound(int x,int y)
{
if(x>=0&&y>=0&&x<N&&y<N)
return true;
return false;
}
void dfs(int sx,int sy,int ex,int ey,int cost,int state)
{
if(sx==ex&&sy==ey){
// cout<<cost<<endl;
if(cost<mincost)
mincost=cost;
return;
}
if(cost>mincost)return;
int mx;
int my;
for(int i=0;i<D;i++)
{
mx=sx+direct[i][0];
my=sy+direct[i][1];
if(visit[mx][my]==0&&checkInBound(mx,my))
{
int onecost=g[mx][my]*state;
visit[mx][my]=1;
dfs(mx,my,ex,ey,cost+onecost,onecost%4+1);
visit[mx][my]=0;
}
}
}
void func()
{
int n;
cin>>n;
while(n--)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
cin>>g[i][j];
visit[i][j]=0;
}
}
int sx,sy,ex,ey;
cin>>sx>>sy>>ex>>ey;
mincost=INF;
dfs(sx,sy,ex,ey,0,1);
cout<<mincost<<endl;
}
}
int main(int argc, char *argv[])
{
//printf("Hello, world\n");
func();
return 0;
}
模拟+DFS
-
题目描述:
-
有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:
1、只能沿上下左右四个方向移动
2、总代价是没走一步的代价之和
3、每步(从a,b到c,d)的代价是c,d上的值与其在a,b上的状态的乘积
4、初始状态为1每走一步,状态按如下公式变化:(走这步的代价%4)+1。
-
输入:
-
第一行有一个正整数n,表示有n组数据。
每组数据一开始为6*6的矩阵,矩阵的值为大于等于1小于等于10的值,然后四个整数表示起始坐标和终止坐标。
-
输出:
-
输出最小代价。
-
样例输入:
-
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 5 5
-
样例输出:
-
23