OJ_1091 棋盘游戏

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值