题目描述:

有一个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

做这个题需要用到深度优先搜索或广度优先搜索
对于深度优先搜索而言,其基本思路是从起始点出发,遍历4个方向,一直走下去,直到终点。但要注意如何去剪枝。这里把花费作为参数传入到函数中,作为剪枝的条件。代码如下
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define inf 0x3f3f3f3f int graph[][];
int dir[][] = { {,},{,},{,-},{-,} };
bool isVisit[][]; int startx, starty, endx, endy;
int ans; void dfs(int nowx, int nowy, int state, int sum) {
if(sum > ans) {
return;
}
if(nowx == endx && nowy == endy) {
ans = sum;
}
for(int i = ; i < ; i++) {
int tempx = nowx + dir[i][];
int tempy = nowy + dir[i][];
if(tempx >= && tempx <= && tempy >= && tempy <= && isVisit[tempx][tempy] == false) {
int cost = state * graph[tempx][tempy];
int stateNext = (cost%) + ;
isVisit[tempx][tempy] = true;
dfs(tempx, tempy, stateNext, sum + cost);
isVisit[tempx][tempy] = false;
} }
} int main(int argc, char const *argv[])
{ int n;
//freopen("input.txt","r",stdin);
scanf("%d",&n);
while(n--) {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
scanf("%d",&graph[i][j]);
isVisit[i][j] = false;
}
}
scanf("%d %d %d %d",&startx, &starty, &endx, &endy);
ans = inf;
dfs(startx, starty,,);
printf("%d\n", ans);
} return ;
}

对于广度优先搜索,需要一个队列将每一层可以到达的点加入到队列中,每一个个点最多只有4种状态。我们用一个数组存这四种状态中每一种状态的最小代价,遍历到终点时四种代价之中的最小者则为最小花费

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std; int graph[][];
int dir[][] = { {,},{,},{,-},{-,} };
int opt[][][]; int startx, starty, endx, endy;
int ans; struct Node
{
int x;
int y;
int state;
int cost;
}; queue <Node> Q; void bfs(Node start) {
Q.push(start); int tempx, tempy, cost;
while(!Q.empty()) {
Node now = Q.front();
Q.pop();
for(int i = ; i < ; i++) {
tempx = now.x + dir[i][];
tempy = now.y + dir[i][];
if(tempx >= && tempx <= && tempy >= && tempy <= ) {
cost = now.state * graph[tempx][tempy];
int nextState = cost % +;
if(now.cost + cost < opt[tempx][tempy][nextState] && now.cost + cost < opt[endx][endy][nextState]) {
opt[tempx][tempy][nextState] = now.cost + cost;
Node add;
add.x = tempx;
add.y = tempy;
add.state = nextState;
add.cost = now.cost + cost;
// printf("%d %d %d\n",tempx, tempy, add.cost);
Q.push(add);
}
}
}
}
} int main(int argc, char const *argv[])
{ int n;
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d",&n);
while(n--) {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
scanf("%d",&graph[i][j]);
for(int k = ; k <= ; k++) {
opt[i][j][k] = inf;
}
}
}
scanf("%d %d %d %d",&startx, &starty, &endx, &endy);
ans = inf;
Node start;
start.x = startx, start.y = starty,start.state = , start.cost = ;
bfs(start);
int min = inf;
for(int i = ; i <= ; i++) {
if(min > opt[endx][endy][i]) {
min = opt[endx][endy][i];
}
}
printf("%d\n", min);
} return ;
}

九度oj 题目1091:棋盘游戏的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. 项目在cocos 2.23移植到cocos 3.1.0所出现的bug

    在建项目时一定要注意选择源代码!而不是预编译库 "extensions/ExtensionMacros.h”: No such file 项目右键-属性-配置属性-c/c++ - 常规-附加 ...

  2. 快速排序的一种Java实现

    快速排序是笔试和面试中很常见的一个考点.快速排序是冒泡排序的升级版,时间复杂度比冒泡排序要小得多.除此之外,快速排序是不稳定的,冒泡排序是稳定的. 1.原理 (1)在数据集之中,选择一个元素作为&qu ...

  3. JBOSS连接池默认连接数是多少?在哪个配置文件有这个默认的连接数?

    如果你用的是是4.x的Jboss的话,请参考:docs/dtd/jboss-ds_1_0.dtd,相信你很容易就能找到控制最大/最小连接数的选项,应该是诸如:max-pool-size/min-poo ...

  4. Thread and Peocess

    Thread and Peocess pthread_create() 原型: int pthread_create(pthread_t* thread, pthread_attr_t* attr, ...

  5. URAL 1776 Anniversary Firework (概率,区间DP)

    坑,一开始以为,分成两半的时候去最大那个就行了, 实际上这样是不对的,因为有可能出现小的一半的时间比大的要长, 因为还和等待次数有关,且转移的时候需要用到次数更小的状态, 所以状态定义为二维,dp[i ...

  6. BC div2补题以及 复习模除 逆元__BestCoder Round #78 (div.2)

    第一题没话说 智商欠费 加老柴辅导终于过了 需要在意的是数据范围为2的63次方-1 三个数相加肯定爆了 四边形的定义 任意边小于其余三边之和 换句话说就是 最长边小于其余三边之和 这样的话问题转化为 ...

  7. 数组、Math、JOSN总结

    json对象: 1.数组有length属性[尽量使用for循环] 2.而json没有length属性[可以使用for...in...循环] 3.for in 不能遍历页面中的节点对象. for ( v ...

  8. HTML之网页的基本介绍

    一.web的基本介绍 web就是world wide web的缩写,称之为全球广域网,俗称WWW 可以将web理解成一种当前的互联网,对于我们来说更多的就是网站服务 网站我们可以理解成是由很多网页组合 ...

  9. 有C++特色的极乐净土

    闲的没事瞎打的 在win7下会走调,需要将win7的beep系统文件改成xp的,且主机装有蜂鸣器才能正常收听. beep文件的度盘地址(不过应该没人为了听个这个去改系统文件)(P.S.如果想要尝试,尽 ...

  10. Bootstrap教程简介

    Bootstrap,来自Twitter,是目前最受欢迎的前端框架. Bootstrap是基于HTML. CSS. JAVASCRIPT的,它简洁灵活,使得Web开发更加便捷. 为什么要使用Bootst ...